Video in progress
How do you use feature flags so that you do not have to keep a three-week feature on a separate branch?
The code lands on main in small pieces, but because the flag is off, users do not see it. That keeps merge conflicts small, and release is separated from deploy: turning the flag on is a separate decision.
📖 Answer
The cost of a long-lived branch grows as time passes: in three weeks main moves 200 commits ahead, and in the end you get a big conflict and a merge that "nobody fully understands".
With a flag:
@Service
@RequiredArgsConstructor
class CheckoutService {
private final FeatureFlags flags;
private final LegacyPricing legacy;
private final NewPricing newPricing;
Money price(Cart cart, UserId user) {
// the new calculation only for users who have it enabled
return flags.isEnabled("new-pricing", user)
? newPricing.price(cart)
: legacy.price(cart);
}
}
A few rules, without which flags quickly turn into debt:
- every flag should have an owner and a removal date; after the release, the old path and the flag are removed from the code;
- both states are tested: in CI, with the flag on and with it off;
- if a DB schema change is tied to the flag, do expand/contract: first add the new column, let both work, then remove the old one;
- do not scatter flag checks deep inside the code. It is better to decide at a single entry point.
I have seen a project that piled up 140 "temporary" flags, half of which had been on for over a year. That is why I put removing flags into the Definition of Done as well.
Sign up to mark studied questions and save your progress.