Skip to content
Home DevOps Feature Flags Explained

Feature Flags Explained

Where developers are forged. · Structured learning · Free forever.
📍 Part of: CI/CD → Topic 11 of 14
Feature flags explained — what they are, types of flags, implementation patterns, canary releases, A/B testing with flags, and popular tools like LaunchDarkly and Unleash.
⚙️ Intermediate — basic DevOps knowledge assumed
In this tutorial, you'll learn
Feature flags explained — what they are, types of flags, implementation patterns, canary releases, A/B testing with flags, and popular tools like LaunchDarkly and Unleash.
  • 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.
✦ Plain-English analogy ✦ Real code with output ✦ Interview questions
Quick Answer

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

Example · PYTHON
1234567891011121314151617181920212223242526
# 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)
▶ Output
# Same user_id always gets the same flag result — consistent experience

Feature Flag Service — LaunchDarkly SDK Pattern

Example · PYTHON
1234567891011121314151617181920212223242526
# 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)
▶ Output
# Flag targeting controlled in dashboard — no code change to adjust rollout

🎯 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.

🔥
Naren Founder & Author

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.

← PreviousSemantic Versioning ExplainedNext →Release Management Best Practices
Forged with 🔥 at TheCodeForge.io — Where Developers Are Forged