Feature Flags
A release is a bundle of changes shipped together. A feature flag (feature toggle) separates deploying code from releasing a feature: the code ships turned off, and the feature is switched on later, for some users first, without a new build. That lets teams merge unfinished work to main daily (trunk-based development), test in production with a small audience, and turn a broken feature off in seconds instead of rolling back a whole release.
Kinds of flags
| Kind | Lifetime | Example | Who flips it |
|---|---|---|---|
| Release toggle | days to weeks | "new checkout page" hidden until ready | the team, then removed |
| Experiment | weeks | A/B test of two layouts | product/analytics |
| Ops toggle / kill switch | long | "disable recommendations if the service is slow" | on-call engineers |
| Permission toggle | permanent | "beta features for beta users", paid tiers | product rules |
The danger is the first kind outliving its purpose. Every release toggle is a branch in the code, and two toggles give four combinations to test. Give each one an owner and a removal date, and remove it in the release after it reached 100%.
How a flag decides
Flag services (LaunchDarkly, Unleash, Flagsmith, GrowthBook, ConfigCat, or the vendor-neutral OpenFeature API in front of any of them) all evaluate a flag the same way:
- if the flag is off (kill switch), return the default;
- apply targeting rules in order (user is internal staff → on; country is X → off);
- otherwise apply a percentage rollout: hash the flag name with a stable user id into a bucket 0–99, and turn it on if the bucket is below the percentage.
Hashing (rather than random numbers) is the important part: the same user gets the same answer on every request and every server, and raising the percentage from 10% to 25% keeps the first 10% enabled and adds new users. This model implements exactly that:
import hashlib
def bucket(flag, user_id):
digest = hashlib.sha256(f"{flag}:{user_id}".encode()).hexdigest()
return int(digest[:8], 16) % 100
def evaluate(flag, user, config):
if not config["enabled"]:
return False, "flag off"
for rule in config.get("rules", []):
if user.get(rule["attribute"]) in rule["values"]:
return rule["serve"], f"rule {rule['attribute']}"
b = bucket(flag, user["id"])
return b < config["rollout"], f"bucket {b} vs {config['rollout']}%"
flag = "new-checkout"
config = {
"enabled": True,
"rollout": 10,
"rules": [
{"attribute": "email_domain", "values": ["acme.example"], "serve": True}, # staff first
{"attribute": "country", "values": ["XX"], "serve": False}, # not yet legal there
],
}
users = [{"id": f"user-{n}", "email_domain": "mail.example", "country": "DE"} for n in range(1, 9)]
users.append({"id": "user-staff", "email_domain": "acme.example", "country": "DE"})
users.append({"id": "user-9", "email_domain": "mail.example", "country": "XX"})
for user in users:
on, why = evaluate(flag, user, config)
print(f"{user['id']:<11} {'ON ' if on else 'off'} ({why})")
population = [{"id": f"u{n}"} for n in range(10_000)]
for pct in (1, 10, 25, 50, 100):
cfg = {"enabled": True, "rollout": pct}
enabled = {u["id"] for u in population if evaluate(flag, u, cfg)[0]}
if pct == 10:
at10 = enabled
print(f"rollout {pct:>3}%: {len(enabled):>5} of 10000 users"
+ (f", still includes all {len(at10)} from 10%: {at10 <= enabled}" if pct > 10 else ""))
config["enabled"] = False
print("kill switch:", evaluate(flag, users[-2], config))user-1 off (bucket 83 vs 10%)
user-2 off (bucket 61 vs 10%)
user-3 off (bucket 40 vs 10%)
user-4 ON (bucket 2 vs 10%)
user-5 off (bucket 91 vs 10%)
user-6 off (bucket 13 vs 10%)
user-7 off (bucket 63 vs 10%)
user-8 off (bucket 39 vs 10%)
user-staff ON (rule email_domain)
user-9 off (rule country)
rollout 1%: 116 of 10000 users
rollout 10%: 1059 of 10000 users
rollout 25%: 2552 of 10000 users, still includes all 1059 from 10%: True
rollout 50%: 5037 of 10000 users, still includes all 1059 from 10%: True
rollout 100%: 10000 of 10000 users, still includes all 1059 from 10%: True
kill switch: (False, 'flag off')
The rollout numbers land close to the target percentages, and every user who had the feature at 10% still has it at 25%, 50% and 100%. That monotonic property is what makes gradual rollouts feel stable to users.
Flags and releases together
- Progressive delivery combines them: deploy the release (flag off) → enable for staff → 1% → 10% → 50% → 100%, watching error rates at each step. A bad step is undone by lowering the percentage, which is far faster than rolling back the release (Rollbacks & Roll-forward).
- Changelogs follow the flag, not the deploy. Users care when a feature becomes available, so the release note goes out with the 100% step (or says "rolling out gradually").
- Test both states. CI should run the test suite with each release toggle on and off, at least for the toggles that are mid-rollout.
- Mobile and desktop apps can't be rolled back once installed; remote flags are often the only fast off switch they have. Ship the code dark, enable it remotely.
- Flags are configuration with production impact. Changes need the same review and audit trail as code: who flipped it, when, and why.
Test yourself
Why do flag systems hash flag:user_id instead of using a random number per request?
- Hashing is faster
- So each user consistently gets the same result, and raising the percentage only adds users
- Random numbers are insecure
- To save memory
A new feature causes errors at 10% rollout. What's the fastest safe response?
- Revert the commit and cut a new release
- Turn the flag off (or lower the percentage), then fix
- Roll back the database
- Wait for the next release train
What should happen to a release toggle after the feature reaches 100%?
- Keep it forever as a kill switch
- Remove the flag and the dead code path in a following release
- Rename it
- Set it to 99%