Feature flags
A shop ships a new checkout switched off, turns it on for staff, then for a quarter of users, pulls it back when errors rise, finishes the rollout, and finally removes the switch.
In 30 seconds
A feature flag is a switch in the code that decides, for each request, whether to run a new feature or the old behaviour. Its setting lives outside the code and can be changed without deploying.
That separates deploying code from releasing it. A feature can be shipped switched off, turned on for staff, then for a growing share of users, and switched off again in seconds if it misbehaves.
Flags are meant to be temporary. Once a feature is fully released, the flag and the old code are deleted.
Feature flags, step by step
The 0.2% and 6% error rates are invented. Not every flag is temporary: some kill switches and per-customer settings are kept on purpose, but they should be few and known.
- Step 1: Deploy is not release. A shop has rebuilt its checkout. Normally, putting new code on the servers and showing it to customers are the same moment. A feature flag splits them. The new checkout is deployed alongside the old one but switched off, so all 100 users still get the old page. Deploying becomes routine; releasing becomes a separate decision.
- Step 2: A flag is an if. There is no magic in a flag: it is an if statement. Each time a request arrives, the code asks whether new_checkout is on for this user and runs one branch or the other. The answer is not written in the code. It is looked up in a settings store, so it can be changed while the program is running, with no new deploy.
- Step 3: Target a group first. A flag’s rule can depend on who is asking. The first rule turns the new checkout on only for the shop’s own staff, 4 of the 100 users shown. They use the real thing, on the real servers with real data, while customers notice nothing. Other common targets are beta testers, one country, or one paying customer.
- Step 4: Gradual rollout. Next the flag opens to a percentage of everyone: 5%, then 25%, then all. Each user’s ID is hashed into a fixed bucket from 0 to 99, and the flag is on for buckets below the current percentage. At 25%, 25 of the 100 users get the new checkout. A user’s bucket never changes, so nobody flips back and forth between versions.
- Step 5: Kill switch. At 25% the error rate climbs from 0.2% to 6%: the new checkout has a bug. Nobody needs to rebuild or redeploy anything. Someone switches the flag off, and within seconds every request takes the old path again. Used this way a flag is a kill switch. The bug can then be fixed calmly and the rollout restarted.
- Step 6: Finish the rollout. With the bug fixed, the rollout runs again through 5% and 25% and this time reaches 100%. Every user now gets the new checkout and the feature is fully released, days after its code was first deployed. The old checkout is still in the program, and the flag is still asked on every request, although the answer is now always yes.
- Step 7: Clean up old flags. A finished flag should be deleted: the if, the old branch and the setting. Left in place, flags pile up. Each one is a fork in the code, so three leftover flags allow 8 combinations and ten allow 1024, far more than anyone tests. Old flags also invite accidents, when someone flips one whose old branch stopped working long ago.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Spotted a mistake on this page?