Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 37 ยท Experimentation Platforms

Feature Flags and Gradual Rollouts

Learn how feature flags power A/B tests and safe launches: targeting rules, percentage rollouts, kill switches, exposure logging and flag cleanup, with a runnable flag evaluator in Python.

A feature flag (also called a feature toggle) is an if statement whose condition is controlled from outside the code: if flags.is_on("new-checkout", user): .... Flipping the flag changes behavior without deploying new code. Experimentation platforms are built on flags: an A/B test is a flag whose value is chosen by random assignment and whose exposures are logged for analysis.

Flags solve a deeper problem: they separate deploying code (putting it on servers) from releasing it (showing it to users). You can ship half-finished code safely behind a flag that is off, then release gradually and measure.

Kinds of flags

  • Release flags: gate a new feature while it is finished and rolled out; short-lived.
  • Experiment flags: assign variants for a test; live for the test duration.
  • Ops flags / kill switches: disable an expensive or risky component instantly during an incident.
  • Permission flags: show a feature only to a plan, region or beta group; may live forever.

Deploy is not release

Deploy Code reaches servers, flag off
Release gradually 1%, 10%, 50%, 100%
Measure Guardrails and metrics
Kill switch Turn off without redeploying

Flags make releases reversible

1 / 2

A tiny flag evaluator

Flag evaluation combines three things: rules (who is eligible), a rollout percentage (how many of the eligible users), and a deterministic hash for stickiness (see assignment and bucketing). The Python below is a stand-in for what a flag service does internally; real platforms do the same at scale with cached configs.

Python Evaluate flags with rules and a percentage

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that eligibility rules run first and the percentage applies only to eligible users, and that a disabled flag short-circuits everything (the kill switch).

Gradual rollout with guardrails

A canary or progressive rollout increases exposure in steps while monitoring guardrail metrics. If a guardrail degrades, the rollout halts or rolls back automatically.

Python Progressive rollout that halts on harm

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The rollout stops at the stage where the problem becomes visible, instead of shipping to 100% and finding out from customers. Early stages with few exposed users have wide uncertainty, which is why you step up gradually and keep each stage long enough to collect data.

From flag to experiment

A flag becomes an experiment when you add:

  1. Random assignment from a hash with a per-experiment salt.
  2. Exposure logging: record (user id, experiment, variant, timestamp) the first time the user actually sees the difference. Analyzing users who were assigned but never exposed dilutes the effect.
  3. Metrics and a plan: primary metric, guardrails, sample size, duration (see Experiment Design and the power chapter).
  4. A decision rule and cleanup: ship the winner, remove the loser's code, delete the flag.

Flag hygiene

  • Name flags clearly and give each an owner and an expiry date.
  • Remove dead flags. Stale flags create combinatorial testing nightmares and sometimes catastrophic accidents (a famous trading firm lost hundreds of millions of dollars from an old flag reused for new code).
  • Default safely. If the flag service is unreachable, evaluate to the safe state (usually the old behavior), and cache config locally.
  • Test both paths in CI, and keep latency low by evaluating flags locally with periodically refreshed rules rather than a network call per request.
  • Audit changes. Who flipped what and when must be logged.

Open-source and commercial tools exist (for example Unleash, GrowthBook, Flagsmith, LaunchDarkly, Optimizely, Statsig). The principles above apply to all of them, so learn the concepts first; the vendor choice comes later.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the separation of deploy and release mean?

    A flag allows code to be on production hosts while the feature stays hidden until it is released.
  2. 2. What is a kill switch?

    Ops flags let you shut off a misbehaving component immediately.
  3. 3. Why log exposure at the moment the user sees the variant?

    Including unexposed users adds noise and shrinks the measured effect.
  4. 4. What should a flag evaluate to if the flag service is down?

    Failing to the known-good state protects users.

Summary

  • Feature flags separate deploying code from releasing it, which makes releases gradual and reversible.
  • Evaluation = targeting rules + rollout percentage + deterministic hashing.
  • Progressive rollouts with guardrails halt automatically when harm appears.
  • An experiment is a flag with random assignment, exposure logging, metrics and a decision plan.
  • Clean up old flags and default to safe values.

Next: Experimentation Platform Architecture and Metrics Pipelines