My account

Feature flags and gradual rollout

A feature flag lets you deploy code with a feature switched off, and then release it step by step, to the team, to 5 percent of users, to everyone. If something goes wrong, you switch it off in seconds without a new deploy. Use a stable bucket for each user, a safe default when the flag service is down, and delete every flag when the rollout is done.

Not reviewedWritten with AI helpReading time: 11 minOnline shop exampleTypeScript

Author: bezzad

Contents

The problem: a big release on Friday

Our online shop has a new, faster checkout page. The team worked on it in a separate branch for five weeks. On Friday evening they merged it and deployed it to everyone at once.

Within an hour:

  1. Payments failed for customers who used one bank card type.
  2. The team wanted to go back, but the old checkout code was already deleted. Rolling back meant undoing many other changes too.
  3. The fix took four hours, and the shop lost a busy evening of orders.

The new page itself was good. The problem was the way it was released: big, all at once, and with no fast way back.

What is a feature flag?

A feature flag (also called a feature toggle) is a switch in your code. The code for the new feature is already in production, but it runs only when the flag says “on”.

if (await flags.isOn("new-checkout", user)) {
  return renderNewCheckout(cart);
}
return renderOldCheckout(cart);

The value of the flag lives outside the code: in a flag service or a config store. You change it from a dashboard, and it takes effect in seconds, with no new deploy.

Deploy is not release

This is the main idea:

  1. Deploy means the code is on the servers.
  2. Release means users can use the feature.

With a flag, these are two separate steps. You can deploy on Monday and release a little at a time over the next days.

Deployflag off for allMondayTeam onlywe test it liveTuesday5% of userswatch errorsWednesday25% of userswatch metricsFridayEveryonethen delete flagNext weekProblem at any step?Turn the flag off in seconds. No new deploy.
Deploy first with the flag off, then release step by step. At any step, the flag can be turned off in seconds.

Why this helps:

  1. Small merges, often. Unfinished work can be merged behind an “off” flag. No long branches, fewer merge conflicts.
  2. Test in production safely. The team turns it on only for themselves first.
  3. Small blast radius. If 5 percent of users see a bug, 95 percent do not.
  4. A fast way back. Turning a flag off is faster and safer than a rollback.

A gradual rollout plan

For the new checkout, a good plan is:

  1. Deploy with the flag off. Nothing changes for users.
  2. Turn it on for the team. Staff use the new checkout for real orders for a day.
  3. 5 percent of users. Watch errors, payment failures and page speed.
  4. 25 percent, then 50 percent. Compare conversion between users with and without the new page.
  5. Everyone. Keep the flag for a few days, just in case.
  6. Delete the flag and the old code.

Before each step, decide what you watch and what makes you stop. For example: “If payment failures go above 1 percent, turn it off.”

Stable buckets: the same user, the same answer

A percentage rollout must give the same answer for the same user every time. If a user sees the new checkout on one page load and the old one on the next, they get confused, and your numbers are worthless.

The usual way is to give each user a fixed bucket from 0 to 99, made from a hash of the flag key and the user id:

import { createHash } from "node:crypto";

// Same flag + same user always gives the same number 0..99.
function bucket(flagKey: string, userId: string): number {
  const hash = createHash("sha256")
    .update(`${flagKey}:${userId}`)
    .digest();
  return hash.readUInt32BE(0) % 100;
}

export function isOn(flag: Flag, userId: string): boolean {
  if (!flag.enabled) return false; // kill switch
  return bucket(flag.key, userId) < flag.rolloutPercent;
}
Bucket = hash(flag key + user id) mod 1005%25%100%0255099A user in bucket 3 sees the feature at 5%, 25% and 100%. It never flips back and forth.
Each user has a fixed bucket. Raising the percentage only adds users. Nobody who had the feature loses it.

Why each part matters:

  1. A hash, not a random number. A random number per request flips the user between versions.
  2. The flag key is part of the hash. Otherwise the same 5 percent of users would be the first to get every new feature.
  3. “Bucket less than percent”. Going from 5 to 25 percent keeps the first 5 percent inside.
  4. Logged out users need a stable id too, for example an anonymous id in a cookie. Decide what happens when they log in.

In real projects you usually use a ready flag tool or service. It does this for you. But you must understand the idea to use it correctly.

The kill switch and safe defaults

A flag is only useful if turning it off really works.

  1. The kill switch. One “enabled” value turns the feature off for everyone, whatever the percentage.
  2. A safe default. If the flag service is slow or down, the code must use a default. For a new feature, the safe default is usually “off”.
  3. Cache flag values. Most flag tools keep a local copy and refresh it in the background, so a page does not wait for a network call.
  4. Check flags on the server for important things. A flag checked only in the browser can be changed by the user. Prices and permissions must be checked on the server.
Off must mean fully off. If the new checkout writes a new kind of data to the database, the old checkout must still work with that data. Otherwise turning the flag off breaks things too. Plan data changes so both paths work.

Kinds of flags

Flags are used for different jobs. A known article by Pete Hodgson on Martin Fowler’s site names four kinds:

Kind Example How long it lives
Release flag New checkout, rolled out step by step Days or weeks. Delete after rollout.
Experiment flag Two versions of a button for an A/B test Until the test ends.
Ops flag Turn off product suggestions when the database is busy Can stay long. Needs an owner.
Permission flag Beta features only for some customers Can stay long. Often part of the product.

Flag debt: delete your flags

Every flag doubles the paths through the code. Ten old flags mean many combinations nobody tests.

Signs of flag debt:

  1. Flags that are 100 percent on for months.
  2. Nobody knows who owns a flag or what it does.
  3. Flags inside flags. Code like “if A and not B” is very hard to reason about.
  4. Tests run only one combination.

How to keep flags under control:

  1. Every flag has an owner and an end date when it is created.
  2. Removing the flag is part of the task. The work is not done until the flag and the old code are gone.
  3. Keep the flag check in one place, not spread over twenty files.
  4. Review old flags regularly, for example once a month.

Flags and measuring

A release flag is also a natural way to measure. Users with the flag on and users with it off are two groups. But be careful:

  1. A rollout is not always a fair test. If you choose “staff first” or “only users in one country”, the groups are different from the start.
  2. For a real comparison, use random stable buckets and record which version each user saw. (See the lesson on A/B testing.)
  3. Send the flag value with your events, so you can split every chart by version.

Common mistakes

Mistake Result Right way
Big release to everyone at once One bug hits all users. Team, then 5 percent, then more.
Random number on each request Users flip between versions. Stable bucket from flag key and user id.
No default when the flag service is down Errors, or the feature turns on by surprise. A safe default, usually off.
Price or permission checked only in the browser Users can change it. Check on the server.
Old path cannot read new data Turning the flag off breaks things. Data changes that work with both paths.
Flags never deleted Many untested paths, confusing code. Owner, end date, removal as part of the task.
No stop rule before a rollout step Problems are noticed late. Decide what you watch and when you stop.

Summary in six lines

  1. A feature flag separates deploy from release.
  2. Release step by step: team, a small share, then everyone.
  3. Use a stable bucket per user, so nobody flips between versions.
  4. Have a kill switch and a safe default when the flag service fails.
  5. Check important flags on the server, and keep data working for both paths.
  6. Give every flag an owner and an end date, and delete it after rollout.

Done reading this lesson?Mark it as finished so it shows in your lessons list and report card.