Bird
Raised Fist0
Microservicessystem_design~7 mins

Feature flags in Microservices - System Design Guide

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Problem Statement
Deploying new features directly to all users can cause widespread failures or degrade user experience if bugs exist. Rolling back a faulty feature often requires a full redeploy, causing downtime and operational overhead.
Solution
Feature flags allow turning features on or off dynamically without redeploying code. By wrapping new functionality in conditional checks controlled via configuration, teams can release features to subsets of users, test in production safely, and quickly disable problematic features.
Architecture
User App
(Microservice)
Feature Flag

This diagram shows a microservice querying a feature flag service to decide whether to enable or disable a feature. An admin console updates flags dynamically.

Trade-offs
✓ Pros
Enables safe, gradual rollout of features to reduce risk of failures.
Allows quick disabling of problematic features without redeploying.
Supports A/B testing and targeted feature exposure for different user groups.
Improves developer productivity by decoupling deployment from release.
✗ Cons
Adds complexity to codebase with conditional logic scattered across services.
Requires robust flag management to avoid stale or forgotten flags causing technical debt.
Potential performance overhead if flag checks are not efficiently implemented or cached.
Use when deploying features frequently in microservices with multiple user segments or when you need fast rollback capabilities. Recommended for systems with over 1000 daily active users or multiple deployment environments.
Avoid when the system is very simple with infrequent releases or when feature toggling overhead outweighs benefits, such as in small internal tools with few users.
Real World Examples
Netflix
Uses feature flags to roll out new streaming features gradually to subsets of users, minimizing impact of bugs.
Uber
Employs feature flags to enable or disable payment methods dynamically across regions without redeploying services.
LinkedIn
Uses feature flags to conduct A/B testing on new UI components and control feature exposure per user segment.
Code Example
The before code always uses the new payment gateway, risking failures if it has bugs. The after code checks a feature flag to decide which gateway to use, allowing dynamic control without redeploying.
Microservices
### Before (no feature flag) ###
def process_payment(user, amount):
    # Always use new payment gateway
    result = new_payment_gateway.charge(user, amount)
    return result


### After (with feature flag) ###
FEATURE_FLAGS = {"use_new_gateway": True}

def process_payment(user, amount):
    if FEATURE_FLAGS.get("use_new_gateway", False):
        result = new_payment_gateway.charge(user, amount)
    else:
        result = old_payment_gateway.charge(user, amount)
    return result
OutputSuccess
Alternatives
Blue-Green Deployment
Deploys two separate production environments and switches traffic between them for releases, rather than toggling features dynamically.
Use when: Choose when you want full environment isolation and instant rollback without conditional code.
Canary Releases
Gradually shifts traffic to new versions of services instead of toggling features within the same codebase.
Use when: Choose when you want to test entire new service versions with a subset of users.
Summary
Feature flags let you turn features on or off dynamically without redeploying code.
They help safely release, test, and rollback features in microservices at scale.
Proper management is essential to avoid complexity and technical debt.

Practice

(1/5)
1. What is the main purpose of using feature flags in microservices?
easy
A. To increase database storage capacity
B. To enable or disable features without deploying new code
C. To improve network bandwidth
D. To encrypt communication between services

Solution

  1. Step 1: Understand feature flags concept

    Feature flags allow toggling features on or off dynamically without changing the codebase.
  2. Step 2: Identify the main benefit in microservices

    This helps in testing, gradual rollout, and quick disabling of features without redeployment.
  3. Final Answer:

    To enable or disable features without deploying new code -> Option B
  4. Quick Check:

    Feature flags = toggle features dynamically [OK]
Hint: Feature flags toggle features without code changes [OK]
Common Mistakes:
  • Confusing feature flags with database scaling
  • Thinking feature flags improve network speed
  • Assuming feature flags handle encryption
2. Which of the following is the correct way to check a feature flag named new_ui_enabled in a microservice?
easy
A. featureFlags.enable('new_ui_enabled')
B. if (featureFlags.check('new_ui_enabled') == false) { /* use new UI */ }
C. if (featureFlags.isEnabled('new_ui_enabled')) { /* use new UI */ }
D. featureFlags.remove('new_ui_enabled')

Solution

  1. Step 1: Identify correct method to check flag status

    Checking if a feature flag is enabled usually uses a method like isEnabled returning true or false.
  2. Step 2: Analyze options

    if (featureFlags.isEnabled('new_ui_enabled')) { /* use new UI */ } correctly checks if the flag is enabled before using the feature. if (featureFlags.check('new_ui_enabled') == false) { /* use new UI */ } incorrectly uses check and false condition. featureFlags.enable('new_ui_enabled') and featureFlags.remove('new_ui_enabled') modify flags, not check them.
  3. Final Answer:

    if (featureFlags.isEnabled('new_ui_enabled')) { /* use new UI */ } -> Option C
  4. Quick Check:

    Check flag with isEnabled() = if (featureFlags.isEnabled('new_ui_enabled')) { /* use new UI */ } [OK]
Hint: Use isEnabled() to check feature flags status [OK]
Common Mistakes:
  • Using enable() or remove() to check flags
  • Checking flag with wrong method or negation
  • Confusing flag check with flag update
3. Consider this pseudocode snippet in a microservice:
if (featureFlags.isEnabled('beta_feature')) {
  return 'Beta feature active';
} else {
  return 'Beta feature inactive';
}
What will be the output if the flag beta_feature is set to false?
medium
A. Beta feature inactive
B. Beta feature active
C. Error: flag not found
D. No output

Solution

  1. Step 1: Understand flag value effect on condition

    The condition checks if beta_feature is enabled (true). If false, it goes to else branch.
  2. Step 2: Determine output when flag is false

    Since the flag is false, the else block executes returning 'Beta feature inactive'.
  3. Final Answer:

    Beta feature inactive -> Option A
  4. Quick Check:

    Flag false triggers else = Beta feature inactive [OK]
Hint: False flag runs else block output [OK]
Common Mistakes:
  • Assuming false flag runs if block
  • Expecting error when flag is false
  • Ignoring else branch output
4. A developer wrote this code to disable a feature using a feature flag:
if (featureFlags.isEnabled('dark_mode')) {
  disableDarkMode();
}
Why might this code not work as intended?
medium
A. It disables dark mode when the flag is enabled, which is opposite logic
B. The method disableDarkMode() does not exist
C. Feature flags cannot control UI features
D. The flag name should be 'enable_dark_mode'

Solution

  1. Step 1: Analyze the condition logic

    The code disables dark mode if the flag is enabled, which is opposite of expected behavior (usually enabled flag means enable feature).
  2. Step 2: Identify correct logic for disabling feature

    To disable dark mode when flag is false, the condition should check if flag is disabled or negate the check.
  3. Final Answer:

    It disables dark mode when the flag is enabled, which is opposite logic -> Option A
  4. Quick Check:

    Enabled flag should enable, not disable [OK]
Hint: Enabled flag usually means feature ON, not OFF [OK]
Common Mistakes:
  • Confusing enable and disable logic
  • Assuming flag controls only backend features
  • Using wrong flag names without checking
5. You want to roll out a new payment feature gradually using feature flags in a microservices system. Which design approach is best to ensure minimal impact and easy rollback?
hard
A. Disable all other microservices during rollout to avoid conflicts
B. Deploy new code with feature always enabled and monitor logs manually
C. Hardcode feature flag values in each microservice and update code to change flags
D. Use a centralized feature flag service with percentage rollout and fallback to old payment service

Solution

  1. Step 1: Understand gradual rollout with feature flags

    Gradual rollout means enabling feature for a small percentage of users first, then increasing over time.
  2. Step 2: Identify scalable and safe design

    A centralized flag service allows dynamic control and percentage rollout. Fallback ensures quick rollback if issues arise.
  3. Step 3: Evaluate other options

    Deploy new code with feature always enabled and monitor logs manually lacks control and rollback ease. Hardcode feature flag values in each microservice and update code to change flags requires code changes for flag updates, not scalable. Disable all other microservices during rollout to avoid conflicts is disruptive and unnecessary.
  4. Final Answer:

    Use a centralized feature flag service with percentage rollout and fallback to old payment service -> Option D
  5. Quick Check:

    Centralized flags + gradual rollout = Use a centralized feature flag service with percentage rollout and fallback to old payment service [OK]
Hint: Centralized flags + gradual rollout = safe deployment [OK]
Common Mistakes:
  • Hardcoding flags causing frequent deployments
  • Disabling services unnecessarily during rollout
  • Ignoring rollback strategies