Chapter 4 · Lesson 18 of 18
Deploy and operate

Feature flags vs deploys: separating release from shipping code

A deploy puts new code on your servers, while a feature flag is a runtime switch that decides whether users see the new behavior.

By the end you will

Use flags to ship code dark, roll out gradually, kill a bad feature without a deploy, and remove flags before they pile up.

Time
12 min
Level
Intermediate
Check
4 questions
You need
  • An app you deploy regularly
  • Access to its configuration or environment

A deploy and a release are different events. A deploy puts new code on your servers. A release is the moment users get the new behavior. A feature flag sits between them: the code ships with the behavior switched off, and you turn it on later, for some users or all, by changing a setting instead of deploying again. That separation makes risky changes easier to roll out and quicker to undo.

How it works#

In code, a flag is a condition around the new behavior:

js
if (flags.isEnabled("new-checkout", { userId })) {
  return renderNewCheckout();
}
return renderOldCheckout();

Where the answer comes from is the design choice. It can be an environment variable, a row in a database, a file you reload, or a separate flag service that evaluates rules and returns on or off per user. Anything beyond a simple on or off needs the app to know who the user is, so a percentage can be applied consistently.

Compare this with strategies that move traffic between versions, such as canary releases and blue-green deploys. Those control which version of the code a request hits. Flags control which path runs inside one version. They combine well.

What flags are good for#

Dark launches. Ship the code with the flag off. It runs through your normal deploy and health checks, but users see nothing. You can turn it on for yourself or your team to test on the real server.

Gradual rollout. Enable the feature for a percentage of users, such as a small share first, then more as metrics stay healthy. Use a stable hash of the user ID so the same person keeps the same experience, instead of a random roll on each request.

Kill switches. Wrap risky or expensive features so you can disable them in seconds. Turning a flag off is faster than a rollback and does not undo unrelated changes in the same deploy.

Decoupling teams and schedules. Work can merge to the main branch before it is finished, hidden behind a flag, so branches stay short. Marketing can pick the launch time without asking for a special deploy.

Where environment variables fit#

An environment variable is the simplest flag. In Compose:

yaml
services:
  app:
    image: myapp:1.4.0
    environment:
      FEATURE_NEW_CHECKOUT: "false"

Changing it means editing the file and recreating the container, as described in environment variables and secrets. That gives a short restart gap, and every instance flips together. This is fine for flags that change rarely, such as turning a module on for one environment. It is a poor fit for kill switches or per-user rollout, where you want the change to apply live and to some users only. For those, read the flag from a database table, a small config endpoint or a flag service, and cache it for a few seconds.

Never put secrets in flag values, and do not expose raw flags to the browser if they reveal unreleased plans.

Step 1: Add the flag with a safe default#

Define the flag in one place, with a default of off. If the flag source is unreachable, fall back to the default, so an outage in the flag store does not turn on unfinished work:

js
const defaults = { "new-checkout": false };
function isEnabled(name, ctx) {
  try { return store.get(name, ctx) ?? defaults[name]; }
  catch { return defaults[name]; }
}

Step 2: Ship dark, then enable in stages#

Deploy with the flag off. Enable it for internal users, then a small percentage, then everyone. After each stage, check error rate and latency, as in monitor uptime simply. Decide before the rollout what result makes you stop.

Step 3: Test both paths#

The flag-off path is your fallback, so it must keep working. Write tests that run each path, at least for the core flow. A flag that is only ever tested in the on position is a hidden bug in your rollback plan. Watch for combinations too: two flags that interact give four paths. Keep flags independent where you can.

Step 4: Clean up#

When a feature is fully rolled out and stable, delete the flag and the old code path. This is the step teams skip. Leftover flags are flag debt: dead branches, confusing logic and tests for paths nobody uses. Give each flag an owner, a purpose and a removal date when you create it, and review the list on a regular schedule.

Check it worked#

Confirm that the new code is deployed but dormant:

bash
docker compose exec app printenv FEATURE_NEW_CHECKOUT
curl -s https://app.example.com/health

The first prints the current value, false for a dark launch. The second shows the app is up. Then flip the flag for a test user and confirm the behavior changes without a new image tag in docker compose ps. Flip it back and confirm the old behavior returns. If the flag lives in a database, check that a change takes effect within your cache window.

Edge cases and gotchas#

  • Flag debt: Every flag is a branch to maintain. Remove flags once a rollout is done.
  • Inconsistent users: Random percentages per request make users flip between old and new. Hash the user ID.
  • Database changes: A flag does not make a schema change reversible. Use backward compatible migrations so both paths work with the same tables.
  • Caching: If responses are cached for all users, a per-user flag may show the wrong version. Vary the cache or skip it for flagged pages.
  • Flag service outage: Decide the default when the flag source fails, and test that case.
  • Security: A flag that hides a feature does not protect it. Enforce authorization on the server regardless of flags.

Troubleshooting#

SymptomLikely causeFix
Flag change has no effectApp caches the value or needs a restartCheck cache time, recreate the container for env flags
Some users see old, some newRandom per request, not per userUse a stable hash of the user ID
New code breaks with flag offOff path untestedAdd tests for both paths, fix the fallback
Nobody knows what a flag doesNo owner or descriptionAdd metadata, remove if unused
Feature visible to wrong usersFlag rules too broadReview targeting rules, test with sample users

Quick reference#

GoalTool
Hide unfinished workDark launch, flag off by default
Roll out slowlyPercentage rollout by user hash
Stop a bad feature fastKill switch flag
Per-environment on or offEnvironment variable
Move traffic between versionsCanary or blue-green deploy

Next#

Continue with rollbacks.

Check what you learned4 questions
1What does a feature flag separate?
2What is a kill switch?
3Why do old flags become a problem?
4How should you test a flagged feature?

Frequently asked questions

What is the difference between a feature flag and a deploy?

A deploy changes which code runs on your servers. A feature flag changes which behavior that code shows at runtime, so you can release or hide a feature without deploying.

What is a dark launch?

A dark launch ships a feature to production with the flag off or limited to staff, so you can test with real infrastructure before any normal user sees it.

Are environment variables feature flags?

They can act as simple ones, but changing them usually needs a restart or redeploy. Flags read from a database or flag service can change while the app runs.

How many feature flags is too many?

There is no fixed number. The warning sign is flags that nobody can explain or that stay long after the rollout finished, so give each one an owner and a removal date.

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios