What Are Feature Flags? Safe Deployments Guide
Is deploying a new feature stressful? Hard to roll back? Feature flags let you deploy code but toggle features on or off at runtime — no redeployment needed.
What Is a Feature Flag?
A feature flag (feature toggle) is a technique that allows you to enable or disable a feature at runtime. The code is live, but the feature can remain hidden.
if (featureFlags.isEnabled('new-checkout')) {
showNewCheckout();
} else {
showOldCheckout();
}
Why Feature Flags?
- Safe deployment — Roll out to 1% of users, monitor, then increase
- Trunk-based development — No long-lived feature branches; everyone merges to main
- A/B testing — Show different experiences to different user groups
- Kill switch — Instantly disable a feature without redeploying
Types of Feature Flags
| Type | Lifetime | Purpose | |------|----------|---------| | Release | Short (weeks) | Toggle new features | | Experiment | Medium (months) | A/B testing | | Ops | Long | Performance/maintenance | | Permission | Permanent | User-specific features |
Simple Implementation
const flags = {
'new-dashboard': true,
'dark-mode': true,
'beta-ai-chat': (user) => user.plan === 'premium',
'experimental-editor': (user) => user.email.endsWith('@company.com'),
};
function isEnabled(flagName: string, user?: User): boolean {
const flag = flags[flagName];
if (typeof flag === 'function') return user ? flag(user) : false;
return !!flag;
}
React Usage
function CheckoutPage() {
const { isEnabled } = useFeatureFlags();
return isEnabled('new-checkout')
? <NewCheckoutFlow />
: <LegacyCheckoutFlow />;
}
Canary Release Strategy
Day 1: 1% of users → Feature on → Monitor metrics
Day 2: 5% of users → No issues → Continue
Day 3: 25% of users → No issues → Continue
Day 5: 50% of users → No issues → Continue
Day 7: 100% of users → Full rollout → Remove flag
Feature Flag Tools
| Tool | Type | Highlights | |------|------|-----------| | LaunchDarkly | SaaS | Enterprise, rich SDKs | | Unleash | Open source | Self-hosted, free | | ConfigCat | SaaS | Simple, affordable | | Flagsmith | Hybrid | Open source + SaaS |
Best Practices
- Clean up flags — Remove flags that are no longer needed (tech debt!)
- Naming convention — Use
feature.checkout.new-flowstyle - Default off — New flags should be disabled by default
- Monitor — Log and track flag changes
- Test both paths — Write tests for enabled and disabled states
- Limit active flags — More than 30-50 creates complexity
Conclusion
Feature flags are essential for modern software development. They enable safe deployments, A/B testing, and instant rollbacks. But without careful management, they create technical debt — always clean up your flags.
Learn feature flags and deployment strategies on LabLudus.