Feature flags: separating release from deploy without accruing debt
Working with long-lived branches, one pattern repeats: two weeks of work, then a merge and half a day resolving conflicts. After which you try to work out what broke.
Feature flags attack the problem from the other side. Code lands on the main branch unfinished, but hidden behind a condition:
if (flags.newCheckout) {
return renderNewCheckout()
}
return renderCheckout()Code merges every day, conflicts do not pile up, and users see nothing — the flag is off.
That is the primary benefit of a flag. Not A/B testing, but separating release from deploy.
Four kinds of flag and their lifespans
The most common mistake is treating all flags as the same thing. In reality they fall into four kinds with wildly different lifespans.
Release flags. They hide unfinished functionality. They live for weeks. Once the feature ships, the flag must be removed.
Experiment flags. For A/B tests. They live as long as the experiment, usually a month or two. Once you have the result, they go.
Operational flags. They let you switch off an expensive feature under load — a recommendations block or a heavy report, say. They live permanently. These are not removed, because they are a control lever.
Entitlement flags. Which customer gets which feature. Permanent, but this is not really a flag — it is product logic, and it belongs in the database.
This split matters because it answers the question when to remove it. When you create a flag, record its kind — then it is clear what happens next.
The simplest implementation
You do not need an external service to start. Configuration is enough:
const flags = {
newCheckout: process.env.FLAG_NEW_CHECKOUT === 'true',
heavyReports: process.env.FLAG_HEAVY_REPORTS !== 'false',
}The problem is that changing a flag requires a deploy. For release flags that is acceptable; for operational flags it is not — those need to be switchable instantly under load.
The next step is moving flags into the database:
create table feature_flags (
key text primary key,
enabled boolean not null default false,
kind text not null, -- release | experiment | ops | permission
rollout_percent int not null default 0,
owner text not null,
created_at timestamptz not null default now(),
expires_at date
);Two columns deserve special attention.
owner — whose flag this is. Six months from now that is the only thing that lets anyone answer "what is this, can I turn it off?".
expires_at — when the flag should be removed. Not strictly required, but it gives you a list: check overdue flags weekly.
When reading from the database, cache it — do not query per call. A thirty-second cache is enough and still makes turning off an operational flag fast enough.
Partial rollout
Most flags only need on/off. But for a risky change, percentage rollout helps: 5% first, then 25%, then everyone.
There is one important condition here: the same user must always get the same result.
function isEnabled(flag: Flag, userId: string) {
if (!flag.enabled) return false
if (flag.rolloutPercent >= 100) return true
const hash = crypto.createHash('sha256')
.update(`${flag.key}:${userId}`)
.digest()
return hash.readUInt32BE(0) % 100 < flag.rolloutPercent
}Do not use a random number. With Math.random() a user sees a different version when they reload — that is confusing and makes testing impossible.
Including the flag key in the hash matters too: without it, the same user lands in the same bucket for every experiment and results start contaminating each other.
In code: where the branch lives
Where you place the flag condition is an architectural question in itself.
The worst option is spreading it through the codebase. If if (flags.newCheckout) appears in twenty places, removing the flag means cleaning twenty places, and you will certainly miss one.
The right approach is lifting the condition as high as possible:
export function getCheckout() {
return flags.newCheckout ? newCheckout : legacyCheckout
}Two complete implementations, one decision point. When the flag goes, you delete one function and one old file — no searching required.
This is not always possible, but the fewer branch points there are, the easier the cleanup.
Flag debt
Now the most important part.
Every flag creates two paths in the code. Two paths mean twice the states, twice the testing and twice the confusion.
If three flags interact, you have eight combinations. How many of them have been tested? Usually one — the one on your machine.
That is why unremoved flags become quiet debt. They break nothing; they simply make the code unreadable and confuse the next developer.
My rules:
A removal date is assigned when the flag is created. A month is usually enough for a release flag.
The overdue flag list is reviewed weekly. That is a one-minute job: one query, a handful of rows.
Removing a flag is not a separate task, it is part of shipping the feature. "The feature is done" means the flag and the old code are gone.
Cap the number of flags. My practical limit is no more than ten active release flags at once. When you hit the limit, you clean out the old ones before starting a new feature.
When you do not need a flag
Flags are not free, so do not use them everywhere.
You do not need a flag when:
- The change is small and safe. Hiding a button colour behind a flag is overkill.
- The feature takes a day. All of a flag's value is in long work.
- The change is easy to roll back. If you can revert a deploy in a minute, the flag adds nothing.
Where flags genuinely pay: multi-week features, changes tied to data migrations, external integrations, and heavy operations sensitive to load.
Putting it together
A feature flag is not a technical tool but a process tool. It lets you work without long-lived branches and roll out risky changes gradually.
But it has a price, and if you do not pay it, it accumulates. Adding a flag is easy; removing it is never urgent — which is why it never happens.
If you are introducing flags now, do one thing from the start: record an owner and a removal date for every flag. Everything else can be added later; those two fields cannot be filled in afterwards, because in six months nobody will remember why the flag was created.