Feature Flags (Feature Toggles)
Feature Flags (also known as Feature Toggles, Feature Flippers, or Feature Controls) are a software development technique that allows you to turn specific functionality on or off during runtime, without deploying new code.
1. Decoupling Deployment from Release
Traditionally, deploying code meant releasing features to users. Feature flags break this coupling.
- Deployment: Moving code to production servers.
- Release: Making features visible/available to users.
With flags, you can deploy unfinished code to production (hidden behind a flag) and "release" it later by flipping a switch.
2. Types of Toggles
Not all flags are the same. They differ in longevity and dynamism.
Release Toggles
Used to hide incomplete features for Trunk-Based Development.
- Lifespan: Short (days/weeks).
- Dynamism: Static (often just a config file change).
Experiment Toggles (A/B Testing)
Used to perform multivariate or A/B testing.
- Lifespan: Medium (weeks/months).
- Dynamism: Dynamic (different users see different values).
Ops Toggles (Kill Switches)
Used to control operational aspects of the system. For example, a flag to disable a heavy "Recommendations" widget if the database is under high load.
- Lifespan: Long/Permanent.
- Dynamism: Dynamic (flipped by operators in response to incidents).
Permission Toggles
Used to change the features or product experience that certain users receive (e.g., Premium vs. Free users).
- Lifespan: Permanent.
- Dynamism: Dynamic (based on user attributes).
3. Implementation
Simple (If/Else)
At its core, a feature flag is just an if statement.
const flags = {
newCheckout: false
};
function renderCheckout() {
if (flags.newCheckout) {
return <NewCheckout />;
} else {
return <OldCheckout />;
}
}
Advanced (Contextual)
Real-world flags often depend on context (User ID, Region, Browser).
if (featureManager.isEnabled("new-checkout", currentUser)) {
// Show new checkout
}
4. Managing Technical Debt
Feature flags are essentially Technical Debt by design. They introduce conditional logic and dead code paths.
- Cleanup: Once a feature is fully released and stable, the flag must be removed. If not, the code becomes a nightmare of nested
ifstatements. - Flag Retirement: Make removing the flag a task in your backlog immediately after the release.
programming/continuous-deployment programming/git-version-control programming/canary-deployment programming/software-testing-basics