Feature flags: shipping code before turning it on
Every deploy is also a release, so a half-finished or risky feature either waits on a branch or goes out to everyone at once. Feature flags split the two: the code ships switched off, and you turn it on for a few users, then all of them, without a deploy. Here is how a rollout picks users, and how to clean up afterwards.
01 Deploy and release are two different moments
A deploy puts a new version of the code on your servers. A release is the moment users start getting the new behaviour. With nothing in between, the two are the same moment: the new checkout goes live for everyone as the last server restarts, and taking it back means another deploy.
A feature flag pulls them apart. The new code ships switched off, so deploying it changes nothing a user can see. Turning it on, first for staff, then for 20% of users, then for everyone, is the release, and it is a config change rather than a build.
02 A flag is an if with a remote switch
In the code, a flag is a branch. Both the old path and the new one are in the build; the flag decides which one runs for this user, on this request.
The value lives outside the code, in a flag service or a config store. Most flag SDKs on servers download all the rules when the process starts, keep them in memory and refresh them by polling or a streaming connection. So isOn is a lookup in local memory, not a network call on every request, and if the flag service is unreachable the SDK keeps using the last rules it had.
The app server decides from the rules it already holds in memory.
This is also what makes trunk-based development work: unfinished code can be merged to the main branch every day, switched off, instead of living for weeks on a branch that gets harder to merge.
03 Ramping up: 20%, then 50%, then everyone
A percentage rollout must be sticky: the same user has to get the same answer on every request, or they would see the new checkout on one page load and the old one on the next. So the SDK does not roll a die. It hashes the flag name and the user id into a bucket from 0 to 99, and the user is on when the bucket is below the percentage.
- old checkout
- just turned on
- new checkout
- switched off again
Version 2 is deployed to every server with new-checkout at 0%. Each user already has a fixed bucket from the hash, but no bucket is below 0, so all eight get the old checkout. Nothing has been released.
Step 4 is the point of the whole exercise. The broken code was still deployed, but it was released to nobody, and turning it off needed no build, no review and no rollout. Compare a canary deploy, which limits a new version to some servers: a flag limits a new feature to some users, and it can be undone separately from everything else in the version.
04 Bucketing users in a dozen lines
Here is the bucketing from the trace. It takes the first four bytes of a SHA-256 of flag:user as a number and keeps the remainder after dividing by 100. Real SDKs use other hash functions, and often many more buckets so a rollout can move in steps smaller than 1%, but the idea is the same.
That output is a real run on Node 24, which runs .ts files directly by stripping the types. Look at the dark-mode line: the same eight users get different buckets for a different flag. That is why the flag name goes into the hash. Without it, u5 would be in the first 20% of every rollout, and the same few users would meet every new bug first.
05 Not every flag is the same kind
The same if serves very different jobs, and the job decides how long the flag should live.
| Kind | What it is for | How long it lives | Who flips it |
|---|---|---|---|
| Release | Hide unfinished or risky code until it is ready | Days to weeks | The team shipping it |
| Experiment | Show variant A or B and measure the difference | As long as the test | Product and analysis |
| Kill switch | Turn off a costly or fragile path under load | Long, on purpose | On-call engineers |
| Permission | Turn a feature on for a plan, a customer or staff | As long as the product | Product or support |
Release and experiment flags are temporary by design and must be removed. Kill switches and permission flags are long-lived, which makes them really configuration: give them clear names, document both states, and test the off state, because a kill switch that has never been flipped may not work when you need it.
06 Cleaning up: a flag is a branch you must delete
Once a release flag has been at 100% for a while and nobody needs the old path, it is pure cost: two paths to read, test and keep working. Flags also multiply. Ten independent on-off flags allow 2¹⁰ = 1,024 combinations, and nobody tests most of them.
export function checkout(user: User, cart: Cart): Receipt {if (flags.isOn('new-checkout', user.id)) {return newCheckout(user, cart);}return oldCheckout(user, cart);}
export function checkout(user: User, cart: Cart): Receipt {return newCheckout(user, cart);}
The order matters. First remove the check and the old path from the code, and deploy that. Only then delete the flag in the flag service. Delete it first and the running code, which still calls isOn, falls back to its default, usually off, and the old checkout comes back for everyone.
Make removal part of creating the flag. Give each one an owner and a date, and let CI fail the build once a release flag is past it:
Never reuse an old flag. In 2012 Knight Capital repurposed a flag that had once switched on long-dead trading code. One of its eight servers did not get the new deploy, so on that server the flag woke the old code, which sent a flood of unintended orders. The firm lost more than $400 million in under an hour. Delete old code with its flag, and give every new flag a new name.
07 When flags bite
if (flags.isOn('new-nav', id)) ... // later, in the template: isOn againChecked twice, answered differentlyRules can refresh between two checks in one request, and half the page renders the new way. Evaluate a flag once per request and pass the answer down.bucket = hash(userId) % 100The same users always go firstWithout the flag name in the hash, every rollout picks the same 20% of users. Hash flag:user so each flag picks its own slice.flags.isOn('payments-v2', id, { default: true })A default that is not safeA new server that cannot reach the flag service has no rules yet and uses the default in code. Pick the default you would want during an outage, usually the old path.