Feature toggles, also known as feature flags or feature switches, allow developers to ship code to production that is not yet enabled. This gives developers the ability to deploy code frequently without impacting end users. Feature toggles act as a switch that can turn functionality on or off. When the toggle is off, the code path is ignored. When it is switched on, the code is executed. This allows incomplete or un-tested features to be deployed without being released and provides several benefits.
What problems do feature toggles solve?
Feature toggles address several key problems in the software development process:
- Allow incomplete features to be deployed – Developers can ship code to production that is wrapped in a feature toggle. This code can later be activated without having to re-deploy.
- Reduce risk of deployments – Rather than waiting for a feature to be completely finished, it can be shipped behind a toggle. This means other changes unrelated to the unfinished feature can still safely be deployed.
- Simplify rollbacks – If a deployed feature causes issues, it can easily be toggled off. Rollbacks become as simple as flipping a switch.
- Enable continuous delivery – Teams can ship small changes frequently without worrying about the stability of unfinished features.
- Turn features on/off – Users or admins can activate/deactivate features through toggles.
- Enable A/B testing – Feature toggles allow you to test new features with a percentage of users.
By limiting the blast radius of deployments and providing fine-grained control over each feature, toggles reduce the risks associated with frequent releases. Teams gain confidence that changes can be made often and safely.
How do feature toggles work?
Feature toggles typically work by wrapping a conditional check around the code of an unfinished feature. For example:
if(isFeatureEnabled("newCheckout")) { // new checkout code } else { // existing checkout code }
The feature check may call out to a configuration service to determine whether to enable or disable the code branch. Some common ways of implementing feature toggles include:
Configuration Files
The feature enable/disable flag can be stored in a local configuration file or database table. The application checks this flag to determine whether to run the feature’s code. Changing the config toggles the feature on or off.
Feature Management Services
A SaaS feature management system stores feature toggle states remotely. Services like LaunchDarkly provide SDKs that handle the conditional logic during runtime. Features can be controlled through the vendor’s web UI without changing code.
Feature Flags
Frameworks like Spring Boot allow annotating code with @Enabled/@Disabled. This automatically wraps the code in a conditional check and exposes a UI to toggle flag states.
Regardless of implementation, the key concept is the same – Wrap unfinished code so it can be deployed but not executed until ready. The enabling of the code is decoupled from deployment.
When should you use feature toggles?
Feature toggles are most useful when you want to gain the benefits of continuous delivery but are unable to release unfinished features. Common scenarios where toggles shine include:
- Deploying new features that are incomplete or untested
- Refactoring or rewriting existing functionality
- Safe parallel development of alternative implementations
- Migrating users and traffic gradually to new code
- Instantly reverting code in case of issues
- Releasing experimental features to limited users
Feature flags allow teams to defer completion of work to later stages while still shipping code frequently. They enable pushing code to production with incomplete, disabled features.
What are the benefits of feature toggles?
Adopting feature toggles provides many advantages, including:
Reduce Risk
Toggles compartmentalize features and allow incomplete code to be deployed without execution. This reduces the chance of introducing bugs or breaking changes.
Accelerate Releases
Teams can ship changes faster knowing features can be toggled on/off. Toggles remove the requirement to coordinate deployments with feature completion.
Simplified Rollbacks
If issues arise after shipping code, toggles make rollbacks painless. Disable any suspect features instantly without redeploying.
Enable Continuous Delivery
Feature flags encourage small, frequent releases by removing delays around coordinating feature development.
Facilitate Testing
Teams can use toggles to direct certain users to new features before enabling for everyone. This allows validating changes.
Empower Product Flexibility
Toggles give product managers control over releasing features without engineering work. Flag states can be changed anytime after deployment.
Improve Developer Productivity
Engineers can focus on coding without waiting for other features. Toggles reduce dependencies and bottlenecks.
By containing each change, feature toggles enable continuous delivery, rapid iteration, and prevent risky “big bang” releases. Teams gain increased speed and safety.
What are the drawbacks of feature toggles?
While extremely useful, feature toggles also come with some downsides to be aware of:
Technical Debt
Leftover toggle code can accumulate over time, increasing complexity and technical debt. Toggles must be managed and cleaned up.
Testing Overhead
Both feature branches often require testing. More logic needs validation with each toggle added.
Runtime Performance
Feature checks introduce slightly more processing overhead. Performance critical systems may suffer.
Code Complexity
Business logic can become more tangled with features depending on toggle state. Toggles increase cognitive load.
Operational Overhead
Teams must develop processes and lifecycles around working with toggles to minimize downsides.
With good practices around organizing, monitoring, and cleaning up toggles, teams can contain the complexity introduced. The benefits far outweigh the costs in most cases.
Best practices for feature toggles
Follow these best practices to use toggles effectively:
- Wrap new features in toggles from the start
- Turn toggles off by default for incomplete features
- Manage toggles in a central registry
- Establish toggle monitoring and alerting
- Document toggles with clear enable/disable criteria
- Set expiration dates and lifecycle for toggles
- Communicate toggle status across teams
- Limit toggle locations to central framework or libraries
- Abstract toggle implementations from business logic
- Remove toggles once features ship fully
Using toggles responsibility – encapsulating new code, restricting toggle logic, setting policy and hygiene – minimizes complexity while benefiting from continuous delivery.
Tools for implementing feature toggles
Here are some popular tools used to manage feature toggles:
Tool | Description |
---|---|
LaunchDarkly | SaaS feature flag platform with SDKs for all languages |
FeatureFlags | Open source Go library for feature toggles from GitHub |
Togglz | Java library with repository API for feature flags |
Flipper | App for teams to manage feature flags and get analytics |
Bullet Train | Self-hosted feature flag platform and API |
Many CI/CD platforms like AWS, Azure DevOps, and CircleCI also have built-in support for managing feature toggles.
How to implement feature toggles
Here is an example workflow for implementing feature toggles:
1. Identify target features
Determine which upcoming features would benefit from being toggle-wrapped. Good candidates include large, complex changes or foundational functionality.
2. Design toggle implementation
Decide how toggles will be technically implemented – configuration files, remote API, etc. Encapsulate toggling logic in frameworks.
3. Wrap new code in toggles
In the code, use if/else blocks or libraries to check toggle state and direct execution down enabled/disabled paths.
4. Control toggles centrally
Manage toggle configurations through integrated UIs or dedicated hosts. Do not embed flags deeply in code.
5. Monitor and clean up toggles
Watch toggle usage with logging and metrics. Remove inactive, expired toggles to reduce debt.
Automating and integrating the above practices into the development lifecycle helps maximize the benefits of feature toggles.
Example use cases of feature toggles
Here are some real world examples of using feature toggles effectively:
Gradual User Migration
Redirecting small percentages of users to a new feature for incremental testing before full launch.
Kill Switch
Disabling problematic features instantly without re-deploying the entire application.
Canary Deployments
Releasing new versions to a subset of infrastructure to trial changes in production.
User Personalization
Crafting custom user experiences by toggling features on/off per account.
Emergency Rollback
Rapidly reverting code by disabling faulty features if bugs appear in production.
Seasonal Features
Turning holiday/seasonal functionality on/off without repeated deployments.
Feature toggles provide fine-grained control over releases for safer continuous delivery in many scenarios.
Conclusion
Feature toggles are a powerful technique for gaining confidence in releasing changes frequently. By wrapping new code in conditional checks, incomplete features can be deployed and tested without impact. Toggles provide fine-grained control over each change and reduce the risk of integration.
While toggles can introduce some complexity, good discipline around organization, hygiene, and monitoring helps teams benefit from faster, safer delivery. Feature flags enable a range of use cases like phased rollouts, kill switches, and user personalization that accelerate innovation.
With the right processes, feature toggles allow continuous delivery of incremental changes even when requirements are unclear or shifting. Teams gain speed and safety in releasing new functionality.