Feature Flags
A feature flag is a runtime decision that changes behaviour without a deploy. It sounds small, but it is the mechanism that decouples deployment (code in production) from release (users seeing it). Once that separation exists, trunk-based development, canaries and gradual rollouts all become practical.
The 60-second answer
Section titled “The 60-second answer”A feature flag is a condition evaluated at runtime that selects behaviour: on/off, a percentage of traffic, or a rule against user attributes. There are three families — release flags for shipping, experiment flags for measuring, and ops flags for controlling the system. Release flags are temporary and must be deleted after rollout; experiment flags live for the duration of a test; ops flags such as kill switches are permanent. Good practice means evaluating flags once at the boundary, logging which variant a request saw, testing both paths, and treating stale flags as debt with an owner and an expiry date.
Three kinds of flag, three lifetimes
Section titled “Three kinds of flag, three lifetimes”| Type | Question | Lifetime | Example |
|---|---|---|---|
| Release | Is the new code path live? | Days to weeks | New checkout flow |
| Experiment | Which variant performs better? | Weeks | Two pricing layouts |
| Ops | Should we degrade or disable? | Permanent | Kill switch for recommendations |
Mixing them up causes trouble. A release flag never deleted becomes an ops flag nobody understands. An experiment flag with no measurement is a release flag with extra steps.
Targeting
Section titled “Targeting”Flags are evaluated against a context — user ID, tenant, plan, region, device, or a percentage bucket. Two properties make this work:
- Determinism. The same user must always get the same variant. Hash the flag key with the user ID
and map into 0–100. Never use
rand(). - Stickiness. A user in the 10% canary must stay there while you ramp to 50%, or your metrics measure a different population each minute.
def enabled?(flag_key, user_id, percentage) digest = Digest::SHA256.hexdigest("#{flag_key}:#{user_id}") (digest[0, 8].to_i(16) % 100) < percentageendAn explicit allow-list for internal users and a tenant-level override are worth having on day one — they are what make debugging a bad rollout possible.
Kill switches
Section titled “Kill switches”An ops flag that disables a dependency or a costly feature under load. Requirements:
- Default to the safe path. If the flag service is unreachable, fall back to conservative behaviour, never an exception.
- Cache flags locally. A flag-service outage must not take down the application.
- Reachable during an incident. If flipping it requires a deploy, it is not a kill switch.
- Documented. A switch nobody knows about is dead code.
Flag debt
Section titled “Flag debt”Every flag is a branch in the code and in the test matrix. Left alone they multiply until nobody can say which combination is live. Symptoms: flags older than six months, flags at 100% with no removal plan, flags referenced nowhere, flags with no known owner.
Countermeasures that work:
- Owner and expiry date in the flag metadata, enforced by the tool or a scheduled job.
- A removal checklist for the 100% case: delete the flag, the dead branch and its tests.
- A dashboard of stale flags, reviewed monthly.
- A cap on concurrent flags per service, so adding one has a cost.
Testing flagged code
Section titled “Testing flagged code”- Test both paths explicitly by injecting an evaluator rather than reading global state.
- Keep flags per feature few. Two flags interact into four states; three into eight.
- Cover the fallback path — behaviour when evaluation fails.
- Keep flags out of pure domain logic. Evaluate at the boundary and pass a decision in.
type CheckoutVariant = "legacy" | "express";
export function priceFor(cart: Cart, variant: CheckoutVariant): Money { return variant === "express" ? expressPrice(cart) : legacyPrice(cart);}Choosing a tool
Section titled “Choosing a tool”- Roll your own with a config table or JSON file: fine for a handful of flags and one service. You own the UI, audit log, SDK caching and outage behaviour.
- Open source self-hosted (Unleash, Flagsmith, GrowthBook): control of data, operational burden on you.
- Managed (LaunchDarkly, Split, Statsig): streaming updates, targeting, experimentation and audit history out of the box; cost scales with seats and evaluations.
Non-negotiables regardless of tool: an audit history of who changed what, sub-second propagation, an SDK that caches locally, and a way to disable a flag from a phone at 3am.
Flags in the deploy pipeline
Section titled “Flags in the deploy pipeline”- Deploy with the flag off. Verify nothing changed.
- Enable for internal users, then 1%, 10%, 50%, 100% — watching error rate and latency at each step.
- Once stable, remove the flag and the dead branch in a follow-up deploy.
Step 3 is the one teams skip, and the one that separates “we use flags” from “we use flags well”.
Follow-up questions
Section titled “Follow-up questions”- How do you roll out to 1% without the other 99% seeing inconsistent data?
- What happens when the flag service is down?
- How would you remove a flag that is live at 100% without another risky deploy?
- How do flags interact with caching? With database writes?
- What is the difference between a feature flag and a config value?
Common traps
Section titled “Common traps”- Non-deterministic bucketing. Users flip variants and metrics become noise.
- Reading flags remotely on every request. Latency plus a hard dependency on the vendor.
- No default on failure. An unreachable flag service turns an optional feature into an outage.
- Flags used for authorization. A flag is a rollout tool, not a permission check.
- Leaving the flag forever. Two code paths, double the tests, a trap for the next engineer.
- Enabling a flag and a schema change in the same window. You cannot attribute the failure.
Further reading
Section titled “Further reading”- Martin Fowler / Pete Hodgson: feature toggles
- OpenFeature specification — vendor-neutral flag evaluation API.
- LaunchDarkly: feature flag lifecycle