Feature toggle

What is a feature toggle?

In software development, a feature toggle is a mechanism that allows code to be turned “on” or “off” remotely without the need for a deploy. Feature toggles are commonly used by product, engineering, and DevOps teams for canary releases, A/B testing, and continuous deployment.

Also known as “feature flags”, “feature switches”, or "release toggles"; feature toggles have a variety of uses in applications and infrastructure, most of which are to mitigate risk.

For example, they can be used in applications to gradually roll out new features, allowing teams to test changes on a small subset of users before releasing them to everyone. Similarly, in infrastructure, feature toggles can act like circuit breakers, allowing engineers to control a deployed system’s behavior remotely.

How feature toggles work

Feature toggles are typically added to codebases to allow for the remote control of certain logic at runtime. In code, this logic is wrapped so that it can be controlled by the status of a feature toggle. If the status of the feature toggle is “on”, then the wrapped logic is executed. If the status of the feature toggle is “off”, then the wrapped logic is skipped.

When an application wants to know the status of a feature toggle at runtime, it makes a request to an external data source or managed service like Optimizely. The application can then decide whether or not to execute wrapped logic based on the returned status.

Because the application gets the status of feature toggles from a remote source, it allows wrapped logic to be controlled remotely, simply by toggling the flag on or off in the configuration file without requiring code deployment. For example, if the status of a feature toggle is changed in Optimizely, it is also changed in the application at runtime.

Codebases often contain many feature toggles that control different parts of the application. Because of this, feature toggles are typically assigned unique names or keys to be differentiable and descriptive of the logic that they control. Additionally, feature toggles may have different statuses for different users or audiences. These unique keys as well as details about the user or audience can then be used to look up the status of a particular feature toggle in an external data source or Optimizely.

The benefits of feature toggles

The primary benefit of feature flagging is that it mitigates the risks associated with releasing changes to an application. Whether it be a new feature release or a small refactor, there is always the inherent risk of releasing new regressions. To mitigate this, changes to an application can be placed behind feature toggles, allowing them to be turned “on” or “off” in the event of an emergency.

In more complex cases, feature toggles can be used to test changes on small subsets of users. By enabling a feature toggle for a fraction of all users, teams can gain insights before changes are rolled out to everyone. This functionality of feature toggles is typically used for feature rollouts or canary releases, which involve validating changes to applications and infrastructure by testing them on a subset of users. These practices allow teams to catch regressions early and roll back if necessary, mitigating the risk of bugs at runtime.

Feature toggle development process

The first step in implementing a feature toggle is creating and defining it in an external data source or a service like Optimizely. This typically involves choosing a unique name or key for the feature toggle as well as an initial status. Once the feature toggle is defined, applications and infrastructure can begin to look up its status using its unique key.

After the feature toggle is defined in Optimizely or elsewhere, its status must be accessible to the application. Therefore, code must be added to the codebase making it possible for the application to look up the status of a feature toggle from the external source. Optimizely provides SDKs for many major languages and frameworks which provide this functionality.

Finally, once feature toggles are accessible to the application, they can be added to specific code paths to provide remote control. Code paths that require remote control can be wrapped in conditional logic that checks on the status of the feature toggle before executing. Then, when that code path is reached during runtime, it will only execute if the feature flag is in a particular state.

Feature toggle examples / use cases

Canary releases

When releasing a new feature, it is often useful to test it amongst a small subset of users before releasing it to everyone. This practice is known as performing a “canary release” or “feature rollout”, and is used by teams to mitigate risk when adding or changing a feature. Feature toggles can be used to perform canary releases by controlling whether or not to show the new feature to a particular user and providing the ability to remotely roll out or roll back.

Testing in production

Because of the complexity of modern deployment systems, there can be differences between testing and production environments that can introduce bugs that can't be discovered during unit testing. By incorporating feature toggling into their development process, software development teams can release features into their production environment in a controlled manner, targeting a smaller segment of their user base in order to reduce risk, in order to test new features in production. If the test fails, it can easily be rolled back by simply adjusting the toggle configuration without any changes to the source code.

A/B testing

A/B tests are used by teams to experiment with new ideas and confirm hypotheses using real-world data. They segment traffic into one of two variations of a feature, allowing metrics to be captured for each variation side-by-side. A feature toggle can be used to facilitate an A/B test by controlling the variation in which each user is bucketed. If a feature toggle is “off” for a user, they would see the “A” variation of the test, whereas if it is “on” they would see the “B” variation.

Circuit breaker

As an application or infrastructure grows in complexity, it can become more difficult to maintain and slower to stabilize during a failure. Feature toggles are a great way to address maintainability and recoverability concerns in a growing system. When used like circuit breakers of kill switch, feature toggles can provide teams with a centralized, remote way of controlling components of a deployed systems and responding to failures.

Trunk based deployment

Because feature toggles enable rapid deployment and rollbacks of new code, they can be used to support a trunk based development process, where code changes can be made to the the main trunk instead of having multiple feature branches which then need to be merged and reconciled in Github or other source management platforms. this can lead to quicker release cycles and less time spent on debugging.

Feature toggles and continuous delivery

Championed by influential software developer Martin Fowler, continuous integration and continuous delivery (CI/CD) is a software development discipline in which software can be released to production at any time. It has been adopted by leading software companies around the world whose teams are now expected to quickly deliver software that is stable and bug-free to your user base.

Feature flagging enhances CI/CD by making “continuous” more achievable. The feature flags and feature toggles are a key component of the implementation of continuous delivery, allowing the separation of feature rollout from code deployment. Incomplete features can be merged into the production codebase but hidden behind feature flags.

With continuous delivery and feature flag management, a team can launch, control, and measure their features at scale.

Optimizely and feature toggles

Optimizely is a leader in the space of Progressive Delivery and Experimentation, and we offer a number off different solutions for feature flag management.

Optimizely server-side experimentation is our enterprise feature flagging and experimentation suite which enables development teams to manage features and run feature tests on any Internet connected device, with SDKs available for the most popular languages and frameworks including Node, Python, Ruby, Go, React, Swift and C#.

For teams that want to just get started with feature toggles, Optimizely free feature flagging is a feature flagging solution you can signup for and deploy right away to start using feature flags without the a/b testing functionality, completely for free (unlike competitors like LaunchDarkly).

Feature toggles are a powerful tool to employ in your software development practice, and Optimizely makes it easier than ever to deploy than ever to reduce risk, speed up development times, and test new functionality in your software.