Feature Flags in Practice
Why feature flags exist, how they decouple deploying code from releasing it, and what separates an env var flag from a real targeting service.
Feature Flags in Practice
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Deploying code and releasing a feature are two separate events once you use flags — the code can be live in production, dormant behind a flag, for days before anyone outside your team sees it.
- A gradual rollout lets you expose a new feature to 1% of users, watch for problems, and widen it, instead of betting the whole user base on one all-or-nothing release.
- A feature flag is a kill switch: turning a bad feature off is an instant config change, not a rollback, a redeploy, and a wait.
- An environment variable is a blunt on/off switch that's the same for every request in a given deploy; a real flag service can target a single user, a percentage of traffic, or a named cohort, and change its answer without a restart.
The problem: deploying and releasing used to be the same event
Without feature flags, shipping a feature means one thing: you merge the code, deploy it, and the instant that deploy finishes, every user hits the new behavior. That’s fine for a small, low-risk change. It’s a genuine liability for anything bigger — a rewritten checkout flow, a new recommendation algorithm, anything where “this breaks in a way we didn’t anticipate” is a real possibility and “break for 100% of users at once” is the worst version of that outcome.
A feature flag is a conditional wrapped around new code, checked at runtime, that decides whether a given request sees the old behavior or the new one:
if (flags.isEnabled('new-checkout-flow', { userId: req.user.id })) {
return renderNewCheckout(req, res);
}
return renderLegacyCheckout(req, res);
The code for both paths is live in production the moment you deploy. Whether anyone actually experiences the new path is a separate decision, made afterward, by whoever controls the flag. That separation — deploying code versus releasing a feature — is the entire reason flags exist, and everything else in this lesson is a consequence of it.
What that separation actually buys you
Gradual rollouts. Instead of flipping a flag from fully off to fully on, you flip it to “on for 5% of users,” watch your error rates and metrics for a while, and widen it to 25%, then 100%, only if nothing looks wrong. The targeting is usually done by hashing something stable about the user — their ID — into a number and checking whether it falls under the current rollout percentage, so the same users stay in the new path consistently across requests instead of flickering between old and new behavior on every page load. This is functionally the same idea as a canary deployment at the infrastructure level — expose a small slice of traffic to the new thing first — except it happens inside the application instead of at the load balancer, which means you get far more precise control over exactly who’s in the canary group.
A kill switch. If something does go wrong once a feature is live, turning it back off doesn’t require a rollback. A rollback means redeploying an older build, which takes however long your deploy pipeline takes — and if the bad behavior is actively hurting users, every one of those minutes matters. Flipping a flag back to off is a config change that the next request picks up immediately, with no build and no deploy in between. The risky code is still sitting there, unexecuted; you’ve just stopped routing traffic to it.
Decoupling engineering work from business timing. A feature can be code-complete and sitting in production for days or weeks before a product manager, a marketing launch date, or a specific customer contract says it’s time to turn it on. Engineering doesn’t have to coordinate a deploy with a launch date — the deploy already happened. Only the flag flip is time-sensitive, and flipping a flag is cheap enough to do exactly when it’s needed, including instantly reverting it if the launch gets pulled at the last minute.
Env var flag versus a real flag service
The simplest possible flag is an environment variable: NEW_CHECKOUT_ENABLED=true, read once when the process starts, checked with a plain if. This is a completely legitimate way to start — it costs nothing to build, and for a binary “is this feature on in this deploy or not” decision, it’s often all you need.
Its limits show up fast, though. An environment variable gives the same answer to every request the process handles — there’s no way to say “on for these 50 beta users, off for everyone else” without literally branching your deploy logic on user IDs, which nobody wants to maintain. And changing it means restarting the process, so there’s no such thing as flipping it mid-incident without a deploy, which defeats the kill-switch use case almost entirely.
A real feature flag service — whether that’s a hosted product like LaunchDarkly or Unleash, or a homegrown table in your own database — stores flag state somewhere the running application checks per request, not once at startup. That unlocks two things an env var fundamentally can’t do: per-user or per-cohort targeting (a specific user ID, a percentage of traffic, everyone on a particular pricing plan) and live updates with no restart, because the check happens on every request against current state rather than against whatever was true when the process booted. The tradeoff is real: you’re now running (or paying for) a system that stores and serves flag state, and a team that accumulates hundreds of long-lived flags without ever cleaning up the ones whose rollout finished will eventually find half its codebase laced with conditionals nobody remembers the purpose of. A flag that exists to gate a finished rollout should get deleted — along with the old code path — once that rollout is done.
Flags are not environment config
It’s easy to conflate feature flags with the environment variables covered in the-twelve-factor-app-and-environment-config, because both often get read out of similar-looking key/value configuration. They’re solving different problems. Environment config answers “what does this deployment look like” — which database to connect to, which third-party API key to use, how verbose logging should be — and the defining property is that it varies by environment: the same value for every request hitting a given deploy, different between dev, staging, and production. A feature flag answers “what should this particular user see,” a business-logic decision that can vary per user, inside the exact same environment, completely independent of whether you’re in staging or production. You could run the identical environment config in every region and still have a feature flag on for 10% of users in all of them — the two axes are orthogonal, and treating a targeting decision as if it belonged in the same bucket as a database URL is how teams end up with sprawling .env files that are really trying to be a flag system without admitting it.
Check yourself
What does it mean to 'decouple deploying code from releasing a feature,' and why does that matter?
Deploying is getting new code running in production; releasing is making that code's behavior visible to users. Without flags, those two happen at the same moment — the instant you deploy, everyone sees the change. With a flag wrapping the new code path, you can deploy it dark, confirm it's stable in production under real conditions, and only flip it on for users whenever you choose, turning ship-it-and-hope into ship-it-then-decide.
How does a gradual rollout using a feature flag differ from just deploying a feature to everyone at once?
A gradual rollout exposes the new behavior to a small slice of traffic first — say 5% of users, chosen by a stable hash of their user ID so the same users stay in the new path across requests — and watches error rates and metrics before widening the percentage. Deploying to everyone at once means any problem the feature has shows up for your entire user base simultaneously, with no smaller blast radius to catch it in first.
Why is a feature flag described as a kill switch, and what makes that different from a rollback?
A rollback means redeploying an older version of your code, which takes a build pipeline, a deploy, and however long that process takes — minutes at best. A feature flag that's already wrapping the risky code path just needs its value flipped from true to false, which is a config change that takes effect for the next request, not the next deploy. The code causing the problem is still there; it's simply not being executed anymore.
What's the difference between a simple environment-variable flag and a real feature flag service, and when do you need the latter?
An environment variable is read once at process startup and gives every request in that deploy the same answer — on or off, no exceptions, and changing it means restarting the process. A feature flag service stores flag state somewhere the running app can check per request, so it can answer differently for different users (a specific beta tester, 10% of traffic by a stable hash, everyone on the enterprise plan) and change its answer live, with no restart and no redeploy. You need the latter the moment 'on for everyone or off for everyone' stops being precise enough.
How is a feature flag different from the environment config covered in the-twelve-factor-app-and-environment-config, even though both often live in similar-looking key/value settings?
Environment config answers 'what does this deployment look like' — which database to connect to, which log level to use — and the same value applies to every request a given deploy handles, changing only between dev, staging, and prod. A feature flag answers 'what should this specific user see right now,' a business-logic decision that can vary per user within the exact same environment, completely independent of whether you're in staging or production.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.