Sub topic 12
Feature Flags: Separating Release from Deployment
This chapter assumes you know what a deployment and a release are, and that you have seen rolling, blue-green, and canary deployments. It also assumes you can read a short Python snippet. You should be comfortable with error rate and latency as release signals.
Deploy and release are different events
Every strategy so far controls how a new binary reaches users. Each one ties the new code to user exposure. When the new pods take traffic, users get the new behavior.
A feature flag breaks that tie. A feature flag is a conditional in the code that turns a behavior on or off at runtime, based on configuration that you can change without redeploying. With a flag, you can:
- Deploy the code with the new behavior switched off.
- Check that the deployment itself is healthy.
- Release the behavior later by changing the flag.
Deployment becomes a low-drama technical step. Release becomes a product decision that you can make, and reverse, in seconds.
A minimal flag
The sketch below reads a flag and chooses a code path. It buckets users by hashing their ID, so each user gets a stable answer while the percentage changes. It runs as written.
import hashlib
FLAGS = {
"new_checkout": {"enabled": True, "percent": 10},
}
def is_enabled(flag_name: str, user_id: str) -> bool:
flag = FLAGS.get(flag_name)
if flag is None or not flag["enabled"]:
return False
digest = hashlib.sha256(f"{flag_name}:{user_id}".encode()).hexdigest()
bucket = int(digest, 16) % 100
return bucket < flag["percent"]
def checkout(user_id: str) -> str:
if is_enabled("new_checkout", user_id):
return "new checkout flow"
return "old checkout flow"
if __name__ == "__main__":
served = [checkout(f"user-{i}") for i in range(1000)]
print(served.count("new checkout flow"), "of 1000 users see the new flow")Real systems keep FLAGS in a flag service or config store and refresh it while the process runs. The hashing idea is the same. Including the flag name in the hash keeps the same users from always landing in the first bucket of every experiment.
How flags complement the other strategies
Flags do not replace rolling, blue-green, or canary deployments. They cover a different risk.
| Question | Deployment strategy answers | Flag answers |
|---|---|---|
| Does the new build start and pass health checks? | Yes | No |
| Does the new build behave under real traffic? | Canary, rolling | Partly |
| Is this one feature safe for users? | Not by itself | Yes |
| How fast can I undo a bad behavior? | Seconds to minutes (rollback) | Seconds (flag off) |
A canary splits traffic by instance. A flag splits it by user, account, region, or any attribute you can evaluate. That makes flags better for targeting, for example "enable for internal staff first, then for 5% of users". A canary stays better at catching problems in the build itself, such as a memory leak or a bad dependency. Many teams use both. They canary the build with the flag off, then ramp the flag up while watching the same error rate and latency metrics.
Flags also help with the database constraints from the earlier chapter. The new code path can write the new schema only when the flag is on. If the flag is turned off, the old path keeps working against the expanded schema.
Common errors
- Stale flags. Each flag adds a branch that must be tested in both states. Remove a flag and its dead code path once the release is complete. Give each flag an owner and a removal date.
- Untested combinations. Two flags create four states. Test the combinations that can actually occur together.
- Flag service as a single point of failure. If the flag lookup fails, the code needs a safe default, usually the old behavior. Cache the last known values locally.
- Inconsistent assignment. If a user flips between the old and new flow on every request, they see broken sessions. Hash on a stable ID, as in the example.
- Flags that hide risk. A flag does not undo data that the new path already wrote. Turning it off stops new writes, but it does not repair bad records.
- Using a flag instead of a rollback. If the build itself is faulty, the flag cannot help. Roll back the deployment.
Choosing between a flag and a strategy
Use a flag when the change is a user-visible behavior, when you want a gradual or targeted ramp, or when you need an instant kill switch. Use a deployment strategy for everything that is about the build: new dependencies, runtime upgrades, and configuration changes that cannot sit behind a conditional.
Exercise
Your team deploys a new payment flow behind a flag set to 0%, using a rolling update. The rollout finishes with all pods ready. Thirty minutes after you raise the flag to 20%, the error rate on payments climbs. Which action undoes the problem fastest, and why is a deployment rollback the wrong first move here?
Show answer
Turn the flag off. This takes effect in seconds without replacing any pods, and the build was already shown to be healthy at 0%. A rollback would redeploy the previous version, which takes longer and would not address the cause, because the faulty behavior is the new code path, not the build. You should still check whether the new path wrote bad data while the flag was on.
Exercise
In the Python example, why does the hash include the flag name and not only the user ID?
Show answer
Without the flag name, a given user would get the same bucket number for every flag. The same 10% of users would always be first to see every new feature. Including the flag name gives each flag an independent split.