What you'll learn: By the end of this lesson you will be able to explain how a flag separates deploying code from releasing it, name the four common kinds of flag and how long each lives, describe how a sticky percentage rollout is computed from a stable key, choose a safe default for when the flag source is unreachable, and say what leftover flags cost.
A building has a fuse box. The wiring for a new room is installed while the breaker is off; nobody gets power until someone flips it, and if something sparks, flipping it back is the whole repair. A feature flag is that breaker for a piece of code: the code ships switched off, and a separate decision turns it on.
This lesson builds on deployment strategies, which decide which servers run the new build. A flag decides which requests take the new code path inside a build already running everywhere.
Verified on 2026-12-01 against Martin Fowler's article on feature toggles, the OpenFeature documentation on the evaluation context, and the Unleash documentation on gradual rollout and stickiness. No flag service was available: the one script below was run with Node and its output is shown.
The core idea
A flag is a conditional in the code whose answer is decided outside the code. Because the answer can change while the service keeps running, "deploy" (the build is on the servers) and "release" (users get the behaviour) become two separate events with two separate owners and two separate rollbacks.
Three illustrations run through the lesson:
- Minimal example: a sticky 5% rollout computed from a hash.
- Realistic example: ShopStream releases a new courier-assignment rule behind a flag.
- Failure example: the flag source is unreachable and the default is wrong.
Four kinds of flag
Fowler's article sorts flags by how long they live and how dynamic the decision is:
| Kind | Purpose | Lifetime and dynamism, per the article |
|---|---|---|
| Release toggle | Ship unfinished code dark, then enable it | Transitionary, usually not more than a week or two; mostly static decisions |
| Experiment toggle | A/B and multivariate tests | Decided per request; must stay stable long enough for significant results |
| Ops toggle | Control operational behaviour, including a kill switch | Mostly short-lived; some long-lived kill switches act as manual circuit breakers |
| Permissioning toggle | Features for certain users or plans | Per request; may live for years |
Treating them as one thing causes most flag trouble: a release flag never removed becomes a permanent branch, and a kill switch removed after launch takes away the fastest way to stop an incident.
How a flag is evaluated
Fowler names the parts: a toggle router decides which code path is live, toggle configuration supplies the settings it uses, and a toggle context (a user identity, a cookie) enables per-request decisions. OpenFeature uses the same shape: the evaluation context is "a container for arbitrary contextual data that can be used as a basis for dynamic evaluation", and its optional targeting key identifies the subject of the evaluation and is used for deterministic percentage-based rollouts. The documentation says the key should "contain a string uniquely identifying the subject", such as a UUID.
Figure 1. Where a flag decision is made. How to read this: the request supplies the context, the code asks the flag client, and the client answers from rules it already holds. The flag source updates those rules in the background, so the request path does not wait on it.
A sticky percentage rollout
A rollout to 5% of users must give a user the same answer every time, or the feature flickers. Unleash states the goal as guaranteeing "that the same user gets the same features every time", and describes hashing a context field with the strategy's group id into a number from 0 to 100; it checks userId first and then sessionId, and if neither exists it falls back to a random number, so "stickiness is not guaranteed". Its gradual rollout uses a normalized MurmurHash of the user's ID.
Minimal example (illustrative). This script uses SHA-256 rather than MurmurHash, but the principle is the same: hash the flag key together with the user id, take a bucket from 0 to 9,999, and compare it with the percentage.
import { createHash } from "node:crypto";
function bucket(flagKey, userId) {
const h = createHash("sha256").update(`${flagKey}:${userId}`).digest();
return h.readUInt32BE(0) % 10000;
}
function isOn(flag, userId) {
if (!flag.enabled) return false;
return bucket(flag.key, userId) < flag.percent * 100;
}
Run with Node v26.13.1 over 10,000 synthetic user ids (user-0 to user-9999), it printed:
on at 5%: 506
on at 25%: 2458
5% subset of 25%: true
same user twice: true
overlap with another 5% flag: 27
kill switch off: 0
Four properties are visible. The share is near the target, not on it. The 5% group is entirely inside the 25% group, so raising the percentage only adds users. The same user gets the same answer. And because the flag key is part of the hash, two flags select different groups: the overlap with an unrelated 5% flag was 27 users, close to the 25 an independent split would give. Setting enabled to false turned the feature off for everyone, which is the kill-switch behaviour.
below the percentage?"} P -->|yes| ON P -->|no| OFF S -.->|evaluation fails| D["Return the default value"]
Figure 2. One evaluation. How to read this: the switch is checked first so a kill takes effect for everyone; targets and the percentage decide the rest. The dashed line is the case people forget: what happens when the flag cannot be evaluated at all.
Flags and deployment strategies together
A canary decides which pods run version 2; a flag decides which requests use the new behaviour inside it. Together: deploy everywhere with the flag off, confirm health, then raise the flag in steps. A bad behaviour is rolled back by a flag change; a bug in code that runs regardless of the flag still needs a deployment rollback.
Figure 3. A release flag's lifecycle. How to read this: the last step is the one that gets skipped. A flag at "full on" with its old code path still present is already a cost.
What flags cost
Fowler calls toggles "inventory which comes with a carrying cost": each adds a conditional, an abstraction and testing burden. To keep it down he suggests a removal task created with every release flag, expiration dates, "time bombs" that fail a test after expiry, and a cap on the number of flags. He says it is not necessary to test every combination: test the expected production configuration, the fallback with new toggles off, and sometimes all on.
| Choice | Gain | Cost | Wrong when |
|---|---|---|---|
| Flag in code, rules in the source | Instant change, per-user control | Conditionals, a dependency on the flag source | The change is permanent and trivial |
| Dynamic rules in a flag service | Fast, targeted, auditable | Another system to run and secure | A single global switch is enough |
Build it
There is no lab for this lesson. No flag service was available, so the only code that ran is the bucketing script above.
In practice: ShopStream releases a new courier-assignment rule
This is an educational scenario with illustrative numbers.
This is a realistic scenario, not a case history.
The situation
This is the realistic example. ShopStream assigns each order to a courier. A new rule weighs distance and load differently and cannot be tested well offline.
Starting architecture
The assignment service reads orders from a queue and picks a courier with the current rule. The new rule is in the same build behind a release flag, assign-v2, off by default.
Options considered
| Option | Gain | Cost | Wrong when |
|---|---|---|---|
| A. Deploy and enable together | Simple | Any bug reaches everyone | The rule changes money or safety |
| B. Flag with a percentage rollout | Small exposure, instant undo | Flag source and cleanup work | The two rules cannot coexist on one queue |
The decision
Option B, preceded by a shadow run in which the new rule's choices are logged but not acted on, and only then enabled for 5% of orders. The targeting key is the order id, not the courier, so one order always takes one path. A kill switch flag, assign-kill, is a separate ops flag that forces the old rule and is never removed.
Rollout and verification
Raise the percentage in steps with a pause at each, comparing assignment delay and reassignments for the flagged group against the rest, as in a canary. Once 100% has held through a normal peak, delete assign-v2 and the old path.
The 2 a.m. failure
The page reads: "Assignments stalled in one region". The flag source was unreachable after a network change, and the flag client could not evaluate assign-kill. Its configured default was the new path, not the old one, so a request that should have been forced to the old rule took the new one.
Figure 4. A default that undoes the switch. How to read this: the switch was flipped at the source, but the client never heard, and its default chose the risky path.
First checks: the flag client's connection state and which default each flag returns. Mitigation: restore access to the source, or ship a configuration that changes the default. Follow-up: the default for an unreachable flag must be the safe path for that flag: off for a release flag, and for a kill switch the state that keeps the system conservative. Test that failure on purpose.
Architect's notes
- Name the kind of flag when you create it, with an owner and, for release flags, a removal date.
- Use a stable targeting key matching the unit you care about: user, order or tenant.
- Keep a tested kill switch for every risky new path, and log which variant served each request.
- Do not let flags guard schema changes. A flag cannot undo data already written.
Common mistakes
- Never deleting release flags.
- Targeting with a random value, so users flip between variants.
- A default that is the risky path when the flag source is down.
- Nested flags whose combinations nobody has tested.
Key takeaways
- A flag separates deploying code from releasing it, and gives it its own rollback.
- Release, experiment, ops and permissioning flags live for different times.
- A sticky percentage rollout hashes a stable key with the flag key, so raising the percentage only adds users.
- The default when a flag cannot be evaluated is a design decision, not an afterthought.
- Every flag is carried inventory: remove release flags, keep and test kill switches.
Practice exercises
Exercise 1 (20 min). Adapt the script to take a userId list and print which users enter when the percentage goes from 5 to 10. Confirm none leave.
Exercise 2 (30 min). For ShopStream, list four flags (one of each kind) with owner, default if the source is unreachable, and removal date.
Related reading
- Deployment strategies: blue-green, canary and rolling: the server-side half of progressive delivery.
- Graceful degradation and fallbacks: what a safe default looks like.
- Feature flags and A/B testing on the front end and flag lifecycle and experiment pitfalls: the other side of this topic, with flags evaluated in the browser.
Official documentation
- Martin Fowler: Feature Toggles.
- OpenFeature: Evaluation context.
- Unleash: Activation strategies and Stickiness.