Feature Flags Explained
- Feature flags decouple deployment from release — ship code dark, turn it on when ready.
- Use consistent hashing for percentage rollouts — same user always gets the same experience.
- Kill switches are flags with an immediate-off capability — essential for production safety.
A feature flag (or feature toggle) is a conditional in your code that controls whether a feature is active. This lets you deploy code to production with the feature off, then turn it on without a new deployment. Flags enable canary releases (roll out to 1% of users first), kill switches, A/B tests, and trunk-based development.
Basic Flag Implementation
# Package: io.thecodeforge.python.devops # Simplest possible feature flag — environment variable import os def get_recommendations(user_id: int): if os.getenv('ENABLE_ML_RECOMMENDATIONS', 'false') == 'true': return ml_recommendations(user_id) # new ML-based system else: return rule_based_recommendations(user_id) # old system # Better: percentage rollout — test on a fraction of users import hashlib def is_flag_enabled(flag_name: str, user_id: int, percentage: float) -> bool: """Consistently assign users to buckets using hash — same user always gets same result.""" hash_input = f'{flag_name}:{user_id}'.encode() hash_val = int(hashlib.md5(hash_input).hexdigest(), 16) bucket = (hash_val % 100) + 1 # 1-100 return bucket <= percentage # Roll out to 5% of users def get_checkout_flow(user_id: int): if is_flag_enabled('new_checkout', user_id, 5.0): return new_checkout_flow(user_id) return old_checkout_flow(user_id)
Feature Flag Service — LaunchDarkly SDK Pattern
# Using a feature flag service (LaunchDarkly, Unleash, Flagsmith) import ldclient from ldclient.config import Config ldclient.set_config(Config(sdk_key='your-sdk-key')) client = ldclient.get() # Evaluate a flag for a specific user def get_dashboard(user): context = { 'key': str(user.id), 'name': user.name, 'email': user.email, 'plan': user.subscription_plan, # target premium users 'country': user.country # GDPR rollout by country } # Flag evaluated with user context — targeting rules in dashboard if client.variation('new-dashboard-v2', context, default=False): return render_new_dashboard(user) return render_old_dashboard(user) # Targeting rules set in LaunchDarkly UI: # - 100% of users with plan='premium' # - 10% of users in country='US' # - 0% of all others (gradually increase)
🎯 Key Takeaways
- Feature flags decouple deployment from release — ship code dark, turn it on when ready.
- Use consistent hashing for percentage rollouts — same user always gets the same experience.
- Kill switches are flags with an immediate-off capability — essential for production safety.
- Short-lived flags for releases; long-lived flags for A/B tests and operational controls.
- Flag debt is real — remove flags after rollout is complete or the experiment ends.
Interview Questions on This Topic
- QWhat is a feature flag and what problems does it solve?
- QHow do you ensure a user consistently gets the same experience with a percentage rollout flag?
- QWhat is flag debt?
Frequently Asked Questions
What is the difference between a canary release and a feature flag?
A canary release routes a percentage of traffic to a new deployment at the infrastructure level (load balancer rules, Kubernetes traffic splitting). A feature flag controls feature visibility at the application level within a single deployment. Feature flags are more granular — you can target specific users, plans, or countries. Often both are used together.
What is flag debt and how do you manage it?
Flag debt accumulates when flags are never removed after their purpose is served — code becomes littered with old conditionals. Manage it by setting a TTL when creating a flag, adding JIRA tickets to clean up flags after rollout, and doing periodic flag audits. A good rule: any flag that has been at 100% rollout for more than 2 weeks should be removed.
Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.