SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
  • Deployment Strategies Overview
  • CI/CD Pipelines
  • Rolling Deployments
  • Blue-Green Deployments
  • Canary Releases
  • Feature Flags
  • A/B Testing Infrastructure
  • Schema Migrations
  • Rollbacks and Immutable Infrastructure
  • Multi-Region and Active-Active
  • Infrastructure as Code (Terraform)
  • Auto-Scaling Policies
  • Zero-Downtime Deployment
  • Chaos Engineering
  • Shadow Traffic and Dark Launches
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Operations and Security/Deployment Patterns

Feature Flags

Turn features on or off with a switch, without deploying new code.

⏱ 13 min readIntermediateInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

A feature flag (also called a feature toggle) is a switch in your code that turns a feature on or off while the app is running, without deploying new code.

  • You can ship code that is hidden, and turn it on later for a few users, then for everyone.
  • If something goes wrong, you flip the switch off. This is a kill switch, and it is much faster than a rollback.
  • Flags must be cleaned up after the rollout, or they pile up as confusing "flag debt".

02Concept: What is it?

Normally, deploying code and showing a feature to users happen at the same moment. Feature flags separate the two. You deploy the code with the new feature switched off. Later, you release the feature by changing the flag. The deploy is a technical step. The release is a business decision.

In the code, a flag is just an if: if the flag is on for this user, show the new way, otherwise show the old way. The flag value comes from a flag service or a config store, so you can change it at run time. A flag can be on for everyone, off for everyone, or on for some users: a percentage, a group, a country, or a named list.

Deploy first, release later
πŸš€Deploycode ships, flag OFF
→↓later
🚩Flag servicerules and percentages
→↓decides per user
πŸ§‘Some usersnew feature
πŸ§‘Other usersold feature
The new code is in production the whole time. The flag decides who sees it.
🧠Stable bucketing
To roll out to 20% of users, do not pick people at random on each request. Use a stable hash of the user id (and the flag name). Then the same user always gets the same answer, and when you raise 20% to 50%, everyone already on stays on.

03Real-Life Example

🍡The Light Switch in a New Room
A builder finishes a new room in a house and installs the lights, but leaves the main switch off. The wiring is already in place. On the day of the party, the owner flips the switch and the room lights up, with no builders and no new work. If a bulb flickers, the owner flips it off again. The owner can even give one guest a key to see the room early.
The new room
Feature flags
Wiring installed in advance
Code deployed to production with the flag off
The light switch
The feature flag
Flipping it on party day
Releasing the feature without a deployment
Flipping it off if it flickers
A kill switch
A key for one guest
Targeting a named user or group
Opening the room to a few guests first
A percentage rollout
Old switches nobody remembers
Flag debt: old flags left in the code
How a flag gets a value for one user
πŸ›‘Kill switch?if ON, flag is OFF
→↓
🎯Targetinggroup, country, list
→↓
πŸ”’Percentagehash(user) mod 100
→↓
βœ…ResultON or OFF

04Why Do We Need It?

πŸ”ŒRelease without deploying
Turn a feature on or off in seconds. No build, no deploy, no restart.
πŸ›‘Instant kill switch
A bad feature can be switched off for everyone faster than any rollback.
πŸ“ˆGradual rollout
Show it to 1%, 10%, 50% of users and watch the metrics before going to 100%.
🌿Merge early, avoid long branches
Unfinished work can live in the main branch behind a flag, so big changes stay small and safe to merge.
πŸ§ͺExperiments
Flags can split users into groups, so you can run A/B tests.
πŸŽ›οΈControl for non-engineers
Product and support teams can turn features on for a customer or a market, in line with rules.
Deploying is for engineers. Releasing is for the business. A flag lets you do them at different times, and undo a release in seconds.

05Where Do We Use It?

πŸ†•New feature rollouts
A new checkout, search or design that you want to expose slowly.
πŸ—οΈTrunk-based development
Teams merge small changes often, hiding unfinished work behind flags.
πŸ›‘οΈOperational flags
Switches that turn off heavy or risky parts under load, such as recommendations or a slow dependency.
πŸ’³Entitlements
Premium features for paying customers, or a feature for one customer on request.
πŸ”„Migrations
Move traffic from an old system to a new one, step by step, with a flag on the read or write path.
πŸ§ͺExperiments
Show different versions to different groups and measure which one is better.

06Types / Parts / How It Works

Kinds of flags

KindPurposeHow long it lives
Release flagHide a feature until it is ready, then roll it outShort: days to weeks. Remove after 100%.
Experiment flagSplit users into groups to measure an effect (A/B test)Short: until the test ends.
Ops flag (kill switch)Turn off a risky or expensive feature during troubleLong: kept as a safety control.
Permission flagGive a feature to certain plans, customers or regionsLong: part of the product.

Where the rules are decided

Two common designs
Server-side evaluation
πŸ–₯️App serverholds the rules in memory
↑rule updates (stream or poll)
🚩Flag service
Client-side (browser or app)
πŸ“±Clientasks for its own flag values
↑one request at start
🚩Flag servicesends only this user's results
Rules stay on your servers Do not send secret rules to browsers
Most services evaluate flags locally from a cached copy of the rules, so a request does not wait for the flag service.

Rolling out by percentage

Compute hash(flagName + userId) mod 100. If the result is less than the rollout percentage, the flag is on. The same user always lands in the same bucket, so the experience does not flip-flop. Including the flag name in the hash makes different flags pick different groups of users, so the same unlucky 10% do not get every new feature.

VisualizerPercentage rollout, targeting and a kill switchRaise the rollout percentage and see that users already on stay on. Follow one user through the decision. Try targeting rules, the kill switch, and the same seed for every flag.
Targeting rule

Targeting: everyone is eligible. Only the percentage decides.

How are users bucketed?
The 100 users (fuchsia = sees the new checkout, dark = old checkout, ring = the user you follow)
How the decision is made for user 7
  1. Kill switch: off, keep going
  2. Targeting: everyone is eligible
  3. Bucket: hash("new-checkout:7") mod 100 = 43, and 43 β‰₯ 25
  4. Result: OFF, shows the old checkout
Users with flag ON
24
of 100 (100 eligible)
Kept after change
11/11
on at 10%, still on at 25%
Overlap with another flag
7
about 6 expected if independent
Flag debt
in use
remove finished flags
24 of 100 users see the new checkout. User 7 has bucket 43, so it is OFF for them (on when bucket < 25).

The users are a fixed list of 100 (every 5th is in the beta group, countries rotate). The hash is FNV-1a, so results are the same every time. Raise the percentage and watch that users already on stay on. Switch to the same seed for all flags, then change the percentage of two flags: they pick the same users, so one group gets all the experiments. With only 100 users the numbers wobble around the target.

07How to Use It / Steps / Implementation

Using flags well

  1. 1
    Create the flag with a safe default
    Choose what happens if the flag service is down or the flag is missing. For a new feature the default is almost always OFF.
  2. 2
    Wrap the new code path
    Put an if around the change, and keep the old path working. Keep the branch small and at one place.
  3. 3
    Deploy with the flag off
    Ship the code. Nothing changes for users yet.
  4. 4
    Turn it on for yourselves
    Enable it for developers, then employees, to test in production safely.
  5. 5
    Roll out in steps
    1%, 10%, 50%, 100%, while you watch errors, latency and business metrics (like a canary release).
  6. 6
    Turn off fast if needed
    If a number goes bad, flip the flag off. Investigate afterwards.
  7. 7
    Remove the flag
    After 100% is stable, delete the flag, the old code and the tests for it. Put a reminder date on the flag when you create it.
πŸ’‘Name, owner and expiry
Give every flag a clear name, an owner and an expiry date. A list of 300 unknown flags is a hazard, because nobody dares to remove any of them.
⚠️Test both paths, and the combinations
With n flags there are 2 to the power n combinations. You cannot test all of them. Keep flags independent, short-lived and few, and always test both the on and the off path of each one.

08Key Terms to Know

TermMeaning in easy words
Feature flag / toggleA switch in the code that turns a feature on or off at run time
Kill switchA flag whose job is to turn something off quickly in an emergency
Rollout percentageThe share of users who get the flag ON
BucketingPutting each user into a fixed bucket (0 to 99) by hashing their id
Targeting ruleA condition for who gets the flag: group, country, plan or a list
Flag serviceThe system that stores flags and their rules (home-made or a vendor)
EvaluationWorking out whether the flag is on for one user right now
Default valueWhat the code uses if the flag cannot be read
Dark launchDeploying code that runs in production but is hidden from users
Flag debtOld flags that stay in the code after the rollout is finished
Trunk-based developmentEveryone merges small changes into the main branch often, using flags to hide unfinished work

09Pros, Cons and Trade-offs

βœ… Pros
  • Release without deploying
  • Instant kill switch
  • Gradual rollouts and early testing in production
  • Smaller, safer merges
  • Different users can get different behaviour
⚠️ Cons
  • More code paths to test and understand
  • Flag debt if old flags are not removed
  • The flag service becomes a dependency
  • A wrong rule can expose a feature by mistake
  • Hard to debug when behaviour depends on who the user is

Trade-offs

ChoiceWe gainWe lose
Build a flag system yourselfFull control and low cost at the startYou must build rules, auditing, a UI and a fast SDK
Use a vendor serviceReady UI, audit logs, experiments, SDKsCost, and another outside dependency
Evaluate locally with cached rulesNo extra delay per request, works if the flag service is downChanges take a moment to arrive
Ask the flag service on every requestAlways up to dateSlower, and a failure there hurts every request
Flag per featureFine controlMany flags to track and remove
Long-lived flagsPermanent controls (kill switches, plans)Code that is always more complex

10Common Problems and Mistakes

Common mistakes

  1. Never removing flags. The code fills with dead branches, and nobody knows what is safe to delete.
  2. Random picks per request. A user sees the new page, then the old one on the next click.
  3. Using the same hash for every flag. The same group of users gets all the experiments, and the results are skewed.
  4. A bad default. The flag service fails and a half-finished feature turns on for everyone.
  5. Hidden dependencies between flags. Flag B only works if flag A is on, and nobody wrote it down.
  6. Secrets in client flags. Sending all rules to the browser leaks targeting lists and plans.
  7. No audit log. A flag changed at 2 a.m. and nobody knows who did it, or why the site broke.

Things to watch

🧹Flag debt
Track count and age of flags. Remove the flag when the rollout ends.
πŸ”€Combinations
Many flags create many states. Keep them independent, and test the main paths.
πŸ—„οΈData written under a flag
If the new path writes new data, turning the flag off must still work with that data.
πŸ”Who can change flags
Flag changes are production changes. Use permissions, reviews for risky flags and an audit log.

11Real-World Examples

🧰Flag platforms
LaunchDarkly, Unleash, Flagsmith, Split and others offer rule engines, SDKs and audit logs. OpenFeature is an open standard API for flags.
πŸ—οΈTrunk-based teams
Teams that merge to the main branch many times a day often rely on flags to hide unfinished work.
πŸ›‘Incident response
During an outage, teams turn off expensive features (recommendations, live counters) to protect the core.
πŸ§ͺExperiment platforms
A/B testing systems are built on the same idea: bucket users, then compare results.
πŸ“±Mobile apps
Apps cannot be updated instantly, so flags let you turn features on or off for installed versions.
πŸ”„System migrations
A flag moves reads, then writes, from an old database to a new one step by step.
πŸ“Note
Product names and features change over time. Check the current documentation before you choose a tool.
Mini case: a kill switch saves the day
🚩Flag at 20%new recommendations
→↓load rises
🚨Alertdatabase slow
→↓
πŸ›‘Kill switchflag OFF in seconds
→↓
βœ…Recoveredno redeploy needed
The feature was already deployed. Only the flag changed.

12Related Topics to Read Next

Canary Releases→

Both grow exposure step by step, one by traffic, the other by user.

A/B Testing Infrastructure→

Flags and bucketing are the base of experiments.

Deployment Strategies Overview→

See how flags fit with other release methods.

Blue-Green Deployments→

A whole-system switch, compared with a per-feature switch.

Rollbacks and Immutable Infrastructure→

A flag-off is the fastest rollback of all.

Consistent Hashing→

Another use of hashing to give stable, even placement.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • βœ”What: a run-time switch that turns a feature on or off, with no deployment.
  • βœ”Idea: deploy first (flag off), release later (flag on). They are separate steps.
  • βœ”Rollout: hash(flag name + user id) mod 100 < percentage. Stable, and earlier users stay on.
  • βœ”Targeting: by group, country, plan or a named list, then the percentage.
  • βœ”Kill switch: flips a feature off for everyone in seconds. Faster than a rollback.
  • βœ”Kinds: release, experiment, ops, permission. Release and experiment flags are short-lived.
  • βœ”Watch: flag debt, combinations, safe defaults, audit logs, secrets in client flags.
  • βœ”One line: β€œWire the room first, then flip the switch when you are ready, and flip it off if the bulb flickers.”
← Previous
Canary Releases
Next β†’
A/B Testing Infrastructure