Skip to content
Back to log

What is a feature flag and how is it used?

Chainabit's Chao · · 7 min read

In short

A feature flag is used to change an application's behavior at runtime. Its types, configuration options and testing burden vary according to its purpose.

A feature flag is a technique for changing application behavior without redeploying code. OpenFeature compares a flag, in its simplest form, to an if/else statement that can be controlled at runtime. Pete Hodgson uses the names feature flag and feature toggle for the same techniques.

A flag can be used to hide unfinished functionality from end users while making it available for internal testing. OpenFeature also lists releasing functionality to a small group of users first, A/B testing, and reducing production functionality affected by an outage among its use cases. Access can also be restricted based on user attributes such as geography or IP address.

How is a flag decision made?

In Hodgson's account, a Toggle Point is where the application chooses between alternative code paths. The Toggle Router provides the decision about which path to use. The Router can be implemented in different forms, from a simple in-memory structure to a distributed system.

In a simple configuration, an On or Off state is stored for each feature. The Router passes this state to the Toggle Point. In a more dynamic decision, contextual information such as the user can be considered for each request. The same flag can direct requests from different users to different code paths.

In Hodgson's Experiment Toggle example, the Router can use an algorithm based on the user ID to place the user in a group. The configuration contains information such as the sizes of the experiment and control groups. The Router uses this information as input when determining the group.

OpenFeature states that information used for evaluation can be held in the Evaluation Context. Static information, such as the host or application ID, can be configured globally. Dynamic information, such as the client IP address in a web application, can be passed during evaluation and merged with the static values.

Dynamic routing and dynamic configuration are not the same thing. According to Hodgson, an Ops Toggle can be reconfigured from On to Off in response to an outage in the system. An Experiment Toggle, meanwhile, can choose a different path based on the user for each request; even so, the experiment's configuration can remain fixed. Changing the experiment's parameters at runtime can affect the validity of the results.

Flag types by purpose

Hodgson examines flag types in terms of their lifespans and how dynamic the decision needs to be. Who manages the decision can also vary. Flags in the same application can serve separate purposes for deployment, experimentation, operations or access.

Release Toggle

A Release Toggle allows unfinished or untested code paths to be carried into production as latent code. Hodgson notes that this code path may never be turned on. One example of this use is integrating work into the shared integration branch while development continues and keeping the branch deployable to production.

A Release Toggle can separate deploying code from making functionality available to users. In Hodgson's example, if a product feature works for only some delivery partners, it can be kept hidden from users until it has been implemented for all partners. Flags of this type are transitional; a flag related to making a product available can remain in place longer.

Experiment Toggle

An Experiment Toggle is used for A/B testing or multivariate testing. In Hodgson's account, users are divided into groups. The Toggle Router routes a user to the same code path at runtime according to the group they belong to. The effects of the code paths are compared by monitoring the aggregate behavior of the groups.

The experiment configuration remains the same for long enough to obtain statistically significant results. This period depends on traffic patterns. Hodgson also points to the risk that other changes to the system during the experiment may invalidate the results. The routing decision is dynamic because each request may be on behalf of a different user.

Ops Toggle

An Ops Toggle controls operational aspects of system behavior. Hodgson describes how this type of flag can be used for functionality whose performance impact is uncertain. Operators can quickly turn off the functionality in production or reduce its scope when necessary.

Some Ops Toggles are temporary. Others are kept in place for a long time to reduce noncritical functionality under heavy load. In Hodgson's example, a recommendation panel that is expensive to generate can be turned off under heavy load. These controls are used to respond quickly to production problems and require rapid reconfiguration.

Permissioning Toggle

Turn this insight into action.

Use the mobile app to continue with guided workflows.

A Permissioning Toggle changes the functionality or product experience that certain users receive. Hodgson gives functionality made available only to internal users as an example. For user-specific permissions, the decision is made on a per-request basis, which makes the flag dynamic.

The difference between a Canary Release and granting access to a specific group of internal users is how the group is formed. In Hodgson's definition, the canary group is selected randomly. When functionality is released to internal users, it is made available to a specific group. During a Canary Release, the group consistently experiences the functionality in either the On or Off state.

Where is configuration stored?

Hodgson describes different mechanisms for flag configuration. A fixed value in code requires code deployment for reconfiguration. When command-line arguments or environment variables are used, the configuration can be changed without recompiling the application. However, the change still requires redeployment or at least a process restart.

Configuration can also be read from a file. Changing the file does not require recompiling the application code to change the flag state. Hodgson notes that redeployment may nevertheless be necessary in most cases. Changing files consistently across many servers can also become difficult.

A central application database is another option. In the arrangement Hodgson describes, this can be accompanied by an administration interface where operators, testers and product managers can view and change flags. In distributed configuration systems, nodes in the cluster can use shared configuration and be notified of changes.

Per-request overrides are also possible. Hodgson writes that a flag's On or Off state can be changed through a special cookie, query parameter or HTTP header. This approach allows a user to change a flag in production without affecting other users. On the other hand, it increases the risk that end users will change the flag state themselves and increases system complexity.

Evaluation with OpenFeature

OpenFeature defines a common client SDK that can connect to different flag management systems. The Evaluation API is the part the application uses to evaluate flag values. The resulting values can be used to affect the application's control flow or features.

A Provider is a translation layer between the Evaluation API and the flag management system being used. It maps arguments given to the API to their counterparts in the management system. According to OpenFeature, a provider can use an SDK, call a REST API or read a local file to determine a flag's value.

Hooks add behavior at various points in the flag evaluation lifecycle. OpenFeature gives validating the result value, modifying or augmenting data in the Evaluation Context, and logging and telemetry as examples. Events allow a response when the provider's readiness, error state or flag configuration changes.

Test coverage and limits

Feature flags result in multiple code paths within the same deployable application. According to Hodgson, because a flag can later be On or Off in production, verifying the paths that could go live requires testing both states. When there are multiple flags, the number of possible states increases.

Hodgson states that testing every combination of flags is not necessary. The test coverage he describes includes the configuration expected to be used in production and a fallback configuration in which the flags planned for activation are off. Some teams also test with all flags on. This last approach is based on an arrangement in which the Off state represents the existing behavior and the On state represents the changed behavior.

Martin Fowler describes forgetting to wrap a UI element that should be hidden in a flag as a risk of accidental visibility. If functionality has no UI element, the flag check can reside in the application code. Fowler also notes that if creating, maintaining and removing a feature flag takes too much time, this may indicate too many toggle checks.

Fowler also describes releasing in smaller pieces and the Keystone Interface approach for Release Toggles. In the Keystone Interface approach, all the functionality is built except for the entry point in the interface; the entry point is added later. Fowler says to use a Release Toggle if small releases or this approach cannot be used.

Removing a Release flag when its job is done involves more than deleting the configuration definition. According to Fowler, the code that uses it must also be removed. Hodgson also says that flags create additional abstractions or conditional logic in the code and add a testing burden.

Ready to start?

Download the app and continue from your phone.

End of Entry