A feature flag is a switch in your code that turns a feature on or off at runtime, without a new deployment. The code for both paths ships to production, and a configuration value decides which path each user gets. Feature flags let teams separate deploying code from releasing features.
The simplest version is an if statement:
if flags.is_enabled("new_checkout", user):
return new_checkout(cart)
return old_checkout(cart)
That’s really all a feature flag is. What makes them powerful, and what makes them a mess when handled badly, is everything around that if statement: who can flip it, which users see which side, how long it stays in the code, and whether anyone tested both sides.
This guide covers how feature flags work, why they beat long-lived branches, how they power progressive delivery, the practices that keep flag debt under control, and how to test code behind a flag properly. For how flags fit with canary, blue-green and other release methods, see our overview of deployment strategies.
What is a feature flag?
A feature flag, also called a feature toggle, is a conditional in code whose value comes from configuration at runtime. Flags can target everyone, a percentage of users, specific accounts or regions. Common types are release flags, experiment flags, ops flags (kill switches) and permission flags.
Not all flags are the same, and mixing them up is where a lot of the trouble starts:
| Flag type | Purpose | Typical lifespan |
|---|---|---|
| Release flag | Hide unfinished or risky features until ready | Days to weeks |
| Experiment flag | Split users for an A/B test | Weeks |
| Ops flag (kill switch) | Turn off a costly or fragile feature during an incident | Long-lived |
| Permission flag | Give a feature to certain plans or customers | Long-lived |
Release and experiment flags are meant to die. Ops and permission flags are part of how the product works. Treat them differently, name them differently, and you avoid most of the cleanup headaches later.
How do feature flags work under the hood? The app asks a flag service (a SaaS platform, an open source tool like Unleash or Flagsmith, or a config file) for the flag’s value, usually with some user context. Most SDKs cache the rules locally, so checking a flag takes microseconds and still works if the flag service has a bad day.
Feature flags vs long-lived branches
Long-lived branches keep unfinished work out of production by keeping it out of the main branch, which leads to painful merges. Feature flags keep unfinished work out of users’ hands while the code merges into main every day. That is the core of trunk-based development.
Anyone who has merged a three-week-old branch knows the pain. Conflicts everywhere, tests that passed on the branch now fail on main, and a big-bang release with a pile of changes that all went live at once.
With flags, you merge small changes daily behind a flag that defaults to off. The code is in production, built and deployed with everything else, but nobody sees it. When the feature is ready, you turn the flag on. No merge day, no giant release. The trade-off is that your codebase temporarily holds two versions of the logic, and you have to test both.
Progressive delivery with flags
Progressive delivery means releasing a feature to small groups first and widening access as confidence grows. Feature flags make this possible inside the app: you can turn a feature on for internal staff, then 1% of users, then 10%, then everyone, and turn it off instantly if something breaks, all without redeploying.
This is the same idea as a canary deployment, applied at the feature level instead of the service level. The difference is granularity. A canary tests the whole new version of a service. A flag tests one feature, and you can target it by user, account, region or device.
A typical rollout:
- Ship the code with the flag off for everyone.
- Turn it on for your own team. Use it for a few days.
- Turn it on for a beta group or 1% of users. Watch errors and support tickets.
- Go to 10%, 50%, 100% as each step holds.
- Remove the flag and the old code path.
Feature flag best practices
Good feature flag management comes down to a few habits: name flags clearly, give each one an owner and an expiry date, default to off, keep flag checks out of deep code paths, log every change, and test both states before turning a flag on.
- Use names that explain themselves.
checkout_v2_releasebeatsflag_42. Put the flag type in the name if your team mixes release flags and kill switches. - Assign an owner and an expiry. Every release flag should have a person responsible for it and a date by which it gets removed. Some teams fail the build when a flag passes its date.
- Default to off. If the flag service can’t be reached, the safe path should be the old behavior.
- Check the flag once, high up. Evaluate it at the edge of the feature and pass the result down. Flag checks sprinkled through ten files are hard to test and harder to remove.
- Don’t reuse flags. Old flag names come with old code. Reusing one can wake up logic nobody remembers. The famous example is Knight Capital in 2012: a repurposed flag reactivated dead code on one server, and the firm lost about $440 million in under an hour.
- Log every change. Who flipped what, when, and for which users. During an incident, “did anyone change a flag?” should take seconds to answer.
- Limit long-lived flags. Kill switches and permission flags are fine. A release flag that has been on for 100% of users for six months is just dead code with extra steps.
Testing both flag states
Code behind a feature flag has two behaviors, so it needs two sets of tests. Test with the flag off to prove existing behavior didn’t change, and with the flag on to prove the new feature works. The most reliable way is to replay the same real requests in both states and compare the responses.
This is where most teams cut corners. The developer writes tests for the new path with the flag on. Nobody re-checks the old path, because “we didn’t change it”. Except the refactor that made room for the flag did change it, slightly, and now a field comes back as null for everyone with the flag off. That’s still 99% of users on day one.
Keploy makes this cheap. Record real API traffic once. Then replay the same recorded requests twice against the new build:
- Flag off: every response should match the recording exactly. Any difference is a regression in the old path.
- Flag on: responses should differ only in the fields the feature is supposed to change. Anything else is a side effect worth looking at.

Because Keploy mocks the database and downstream calls from the recording, both runs get identical inputs, so a difference in output points straight at the flag logic. You can wire both runs into CI so a pull request can’t merge unless both states pass. See how the replay works on Keploy’s API testing page.
For flags with more than two variants, say an experiment with three options, the same approach applies: one replay per variant.
Cleaning up old flags
Remove a release flag as soon as it has been fully on, at 100% of users, and stable for one or two release cycles. Delete the flag check, delete the old code path and its tests, then remove the flag from your flag service. Leaving flags in place creates flag debt that makes code harder to read and test.

Every flag doubles the number of states a piece of code can be in. Ten active flags in one service means up to 1,024 combinations, and nobody tests that many. Cleanup isn’t optional housekeeping. It’s how you keep the test surface sane.
A cleanup routine that works:
- Review flags in each sprint or every two weeks. Anything at 100% for more than two releases gets a removal ticket.
- Remove the flag in a normal pull request, with tests, not as a quick edit.
- Remove the code first, then the flag in the flag service. The other way around flips the code to its default, which may be the old path.
FAQs
Are feature flags the same as feature toggles?
Yes. Feature flag, feature toggle, feature switch and feature flipper all describe the same thing: a runtime setting that turns a code path on or off without redeploying. Feature flag is the more common term today, while feature toggle shows up more in older writing and in some tools’ names.
How do you test code behind a feature flag?
Test it in both states. With the flag off, confirm existing behavior is unchanged. With the flag on, confirm the new feature works and changes only what it should. A reliable way is to record real API traffic once, replay it with the flag off and on, and compare each response to the recording. Run both in CI so neither path can break unnoticed.
When should you remove a feature flag?
Remove a release flag once it has been on for all users and stable for one or two release cycles. Delete the flag check and the old code path in a normal pull request, then retire the flag in your flag service. Long-lived ops flags and permission flags are the exception, since they are meant to stay.

Leave a Reply