Deploying code and releasing a feature are different decisions. A feature flag is a conditional in your code that decides, at runtime, whether a feature is on.
What flags allow
- Dark launches: merge and deploy unfinished work, switched off, so branches stay short.
- Gradual rollout: enable for staff, then 1 percent of users, then everyone, watching errors on the way.
- Kill switch: turn a misbehaving feature off in seconds, without a new deploy.
- Targeting and experiments: show a variant to a segment and compare results.
A minimal implementation
FLAGS = { new_checkout: { enabled: true, percent: 25 } }
def enabled?(flag, user_id)
config = FLAGS.fetch(flag, { enabled: false })
return false unless config[:enabled]
# The same user always lands in the same bucket.
(user_id.hash.abs % 100) < config.fetch(:percent, 100)
end
puts enabled?(:new_checkout, 42)Stable bucketing matters: a user should not flip between old and new behaviour on every request.
Kinds of flags
- Release flags: short-lived, removed once the feature is fully out.
- Ops flags: long-lived switches for load shedding or disabling a dependency.
- Permission flags: who may use a paid or beta feature. These are really product rules.
The cost: flag debt
Every flag doubles the paths through the code and the combinations to test. Flags that nobody removes turn into dead branches that nobody dares to delete.
- Give each release flag an owner and an expiry date.
- Remove the flag and the old code path right after full rollout.
- Keep the number of active flags visible.
Safety tips
- Default to off when the flag service is unreachable, unless it is a kill switch.
- Log the flag state with errors, so you can tell which variant failed.
- Test both states of a flag, not only the new one.
Takeaway
Flags make releases boring, which is the goal. Use them deliberately, and treat deleting them as part of finishing the work.
Comments