Using feature flags, also known as feature toggles or switches, is a software technique that allows segments of code to be toggled during runtime, without the need for redeploying.
Feature flags can be a powerful tool for teams utilizing continuous integration practices, allowing code to be deployed in a dormant state and enabled later on. At a basic level, they provide the ability to conditionally render or activate features at will.
In this blog, I’ll cover feature flags. We’ll start by discussing why and when we use them, and then move into an overview of what it looks like to develop with them. Let’s dive in!
Why Use Feature Flags
There are many benefits to applying feature flags to your application, aiding not only in the velocity of development but also in the quality of your product. It can also help product managers better control releases, allowing for more effective feedback.
New features can be introduced immediately and defaulted to “off,” which allows production to continue as expected. Dormant features will then only be available when the toggle is explicitly turned on, so teams can validate functionality and verify code behaves as expected.
By putting features behind a flag, you can also minimize the risks of putting new features into your application. Essentially acting as a kill switch, poorly performing features can be diasabled to prevent application errors or even just to increase overall performance.
Feature flags also help to quicken release cycles. By decoupling feature lifecycle management from code deployment, developers can focus more on other tasks. That way, application stakeholders can enable features whenever they see fit.
When to Use Feature Flags
As briefly touched on above, this tool has many different use cases. Below, I’ve listed a handful of their more popular applications.
1. Testing in Production
Because it is almost impossible to completely simulate a production environment, feature flags allow you to test features in real-world scenarios safely. It provides a quick way to “roll back” to a stable state if needed.
2. A/B Testing
A/B testing is the practice of comparing multiple versions of a feature to find out which one performs the best. By dividing your user base and assigning a unique flag to each of them, you can enable distinct versions of code to each and measure performance before deciding on a final version.
3. Canary Launches
Canary launching, or feature gating, allows you to deploy a new feature to a subset of users in order to monitor performance before rolling out to more users. If the feature does not perform well, it can be immediately rolled back to a more stable version.
Deploying code alongside database migrations can often cause issues if not coordinated correctly. Feature flags can ease the complexity by “hiding” the dependent code until the migration can be completed, at which point the flags can be toggled on and the code will be active.
5. Continuous Deployment
Feature flags can enable you to continuously deliver software to users quickly. By separating feature rollout from code deployment, developers can continuously deploy new features behind flags without the risk of committing faulty or incomplete code. In addition, continuous deployment systems can monitor your application and automatically enable or disable feature flags in order to improve performance.
6. Dependency Injection
By pairing feature flags with dependency injection, objects can be instantiated at runtime based on the current state of the flag. This is especially useful for feature testing, allowing you to pass mocked or stubbed dependencies.
7. System Outages
Applications requiring maintenance or downtime can instantly be toggled on or off. This can allow users to be notified of an outage and give developers time to work on the application without user interference.
Developing with Feature Flags
Developing with feature branches can often require multiple updates during its life cycle in order to stay up to date and minimize code conflicts. To accommodate, developers have to put in extra effort to ensure their branch is fresh.
Instead of a long-lived feature branch, code can be committed continuously, and a feature flag can be created so that the code can immediately be merged in a dormant state.
As teams scale using feature flags, toggling flags on or off becomes a business decision. It can be important to have a flag management mechanism for handling states. Third-party feature flags services are often best and can handle the logistics and provide API to easily integrate into your codebase.
Listed here are some popular feature flags services.
While using third-party services is great, they are often not best for applications containing security concerns. Implementing your own feature flag service can be difficult. It is simple to support basic booleans to determine whether code is active or not, but more advanced techniques such as percentage rollouts or experimentation can be burdensome. Also, as the number of flags increases, sunsetting stale flags can be challenging without the proper precautions.
Feature flags can be utilized in many different ways in order to optimize development and can really complement other techniques, such as continuous integration or deployment.
By allowing new features to be committed quietly, they can safely be added without risking the integrity of the application and can be activated without having to redeploy. Feature flags give you better control over the life-cycle of your features and your application.
If you enjoyed this post, check out the many more on the Keyhole Dev Blog. Thanks for reading!
Below are some sources I found helpful if you’re interested in reading more about feature flags.