Skip to content

Feature flags: deploying is not releasing

Until now, putting a version on the fleet and letting users see what it does were the same event. A canary’s 5% is a share of the traffic sent to a different version of the code. A feature flag pulls them apart. The new code is deployed, with the old code beside it, and a setting that is read while the program runs decides which of the two a request takes.

Pete Hodgson’s article on martinfowler.com describes feature toggles as a way to modify the behaviour of a system without changing code, by having alternative code paths in one deployment that are chosen at run time. His first category, the release toggle, lets unfinished features ship as latent code, which separates deploying the code from releasing the feature.

deploy release
(a new image, as in the (a setting, changed later,
last four lessons) without a deploy)
+------------------------+ flag "new-checkout"
| v2 code | +---------------------------+
| old checkout path | <--- off | 0% of users -> old path |
| new checkout path | <--- on | 5% of users -> new path |
+------------------------+ | 100% of users -> new path |
+---------------------------+

The picture in words: one deployment contains both paths. A setting outside the code, shown on the right, says what share of users take the new one. Changing the setting is the release, and changing it back is the rollback.

Mental model. A deployment moves code. A flag moves users. A canary does both at once, because users are sent to instances, and a flag lets you do them one at a time.

A flag gives the arithmetic of a canary without the instances. To send 5% of users to the new path, the program checks, for each request, whether this user falls in the 5%. A common way is to hash a stable user identifier into a bucket, so the same user gets the same answer each time. To turn the feature off, set the share to 0%.

What this removes is the start. The lab’s canary pays 50 s to start the instance that holds the new version, and a rollback of a canary is a routing change. A flag change involves no new instance at all, so there is nothing to start and nothing to be ready. What the lab does not tell you is how long a changed flag takes to reach every instance: that depends on how the flag system distributes its settings, and it is not modelled.

It also changes how you can roll back. Fowler’s categories include the ops toggle, a kill switch used to degrade a feature gracefully under load or during an outage. A kill switch turns off the one risky feature, not the whole release. The Site Reliability Workbook lists feature flags among the alternatives to a canary of the binary: they allow the separation of a change from the release of a binary.

Hodgson sorts toggles by how long they live and how dynamic the decision is:

Kind What it decides How long it lives
Release toggle Whether unfinished or new code is visible Days to weeks, the same answer for everyone
Experiment toggle Which cohort a user is in, for an A/B test Hours to weeks, per request
Ops toggle Whether to degrade a feature, a kill switch Varies; must change quickly
Permissioning toggle Who may use a feature (premium, beta, internal) Can be years, per request

A flag is cheap to add and expensive to keep, and the kinds differ in how they should be managed. A release toggle should be removed when the feature is released; a permissioning toggle is part of the product.

The program has to decide something when it cannot read the flag. OpenFeature, a vendor-neutral specification for flag evaluation, says that if there is any error during evaluation, the default value is returned, and asks you to give consideration to your default values. So every flag has a failure mode that is chosen at the moment the code is written. A new, unproven path should default to off. A kill switch should usually default to the safe behaviour. A flag whose default is “on” turns an outage of the flag service into a release of everything behind it. The choice should be made by someone who has asked what happens to users for the minutes the flag service is unreachable.

Hodgson says that teams should treat feature toggles as inventory with a carrying cost: each adds code complexity and a testing burden, because a program with several independent flags has more behaviours than anyone tests. He recommends removing the toggles that are no longer needed, with a task on the backlog, an expiry date, or a test that fails when a flag outlives its date.

Flags also do not replace the other strategies. They need the new code to be safe to run when the flag is off, they need a deployment that does not break, and they do nothing for a bad change in a library or a database schema that applies to every request. They sit beside a rolling, blue/green or canary deployment, and are used for the part of a change that can be hidden behind a condition.

Check your understanding: feature flags

This quiz needs JavaScript to check your answers.

Question 1 of 3

A new payment path is behind a flag. The code says that if the flag service cannot be reached, the flag evaluates to its default, and the default is set to on.

What happens when the flag service goes down?

Choose one answer.

Question 2 of 3

What does a feature flag separate?

Choose one answer.

Question 3 of 3

A release toggle hid the new checkout while it was built. The checkout has been at 100% of users for two months.

What should happen to the toggle?

Choose one answer.

  • A feature flag separates deploying code from releasing a feature: both paths are deployed, and a setting read at run time chooses between them.
  • It gives a canary’s gradual exposure without starting instances, so a flag rollback does not pay the 50 s start of the lab. How fast the setting spreads is not something the lab measures.
  • Hodgson’s four kinds are release, experiment, ops and permissioning toggles; they live from days to years.
  • When a flag cannot be read, the default value is returned, so the default is a decision about failure.
  • Flags cost testing and attention. Treat them as inventory and remove the ones that are done.

Next: zero-downtime schema changes, the part of a change that a flag cannot hide.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.