Skip to main content

Feature Toggle

BehavioralDecouplingExtensibilityFeature managementScalabilityAbout 3 min

Also known as

  • Feature Flag
  • Feature Switch

Intent

To enable or disable features in a software application dynamically without deploying new code.

Explanation

Real-world Example

A real-world example of the Feature Toggle pattern is Netflix's rollout of new user interface features. When Netflix decides to introduce a new feature, such as a redesigned homepage layout or a new recommendation algorithm, they use feature toggles to control the release. Initially, the new feature is toggled off for most users, allowing only a small group of users (e.g., beta testers) to experience and provide feedback on the feature. Based on the feedback and performance metrics, Netflix can quickly toggle the feature on for a broader audience or turn it off if issues are detected, all without redeploying the application. This approach allows Netflix to continuously innovate and improve their platform while minimizing risk and ensuring a stable user experience.

In plain words

Feature Toggle is a way to introduce new features gradually instead of deployment all at once.

Wikipedia says

A feature toggle in software development provides an alternative to maintaining multiple feature branches in source code. A condition within the code enables or disables a feature during runtime. In agile settings the toggle is used in production, to switch on the feature on demand, for some or all the users.

Programmatic Example

This Java code example demonstrates how to display a feature when it is enabled by the developer and the user is a Premium member of the application. This approach is useful for managing subscription-locked features.

The Feature Toggle pattern enables the seamless activation or deactivation of entire code executions. This allows features to be managed dynamically based on user information or configuration properties.

Key Components:

  1. PropertiesFeatureToggleVersion: This class uses properties to control the feature toggle. The properties determine whether the enhanced version of the welcome message, which is personalized, is turned on or off.

  2. TieredFeatureToggleVersion: This class uses user information to control the feature toggle. The feature of the personalized welcome message is dependent on the user group the user is in.

  3. User: This class represents the user of the application.

  4. UserGroup: This class represents the group the user belongs to.

public static void main(String[] args) {

    // Demonstrates the PropertiesFeatureToggleVersion running with properties
    // that set the feature toggle to enabled.

    final var properties = new Properties();
    properties.put("enhancedWelcome", true);
    var service = new PropertiesFeatureToggleVersion(properties);
    final var welcomeMessage = service.getWelcomeMessage(new User("Jamie No Code"));
    LOGGER.info(welcomeMessage);

    // Demonstrates the PropertiesFeatureToggleVersion running with properties
    // that set the feature toggle to disabled. Note the difference in the printed welcome message
    // where the username is not included.

    final var turnedOff = new Properties();
    turnedOff.put("enhancedWelcome", false);
    var turnedOffService = new PropertiesFeatureToggleVersion(turnedOff);
    final var welcomeMessageturnedOff =
            turnedOffService.getWelcomeMessage(new User("Jamie No Code"));
    LOGGER.info(welcomeMessageturnedOff);

    // Demonstrates the TieredFeatureToggleVersion setup with
    // two users: one on the free tier and the other on the paid tier. When the
    // Service#getWelcomeMessage(User) method is called with the paid user, the welcome
    // message includes their username. In contrast, calling the same service with the free tier user results
    // in a more generic welcome message without the username.

    var service2 = new TieredFeatureToggleVersion();

    final var paidUser = new User("Jamie Coder");
    final var freeUser = new User("Alan Defect");

    UserGroup.addUserToPaidGroup(paidUser);
    UserGroup.addUserToFreeGroup(freeUser);

    final var welcomeMessagePaidUser = service2.getWelcomeMessage(paidUser);
    final var welcomeMessageFreeUser = service2.getWelcomeMessage(freeUser);
    LOGGER.info(welcomeMessageFreeUser);
    LOGGER.info(welcomeMessagePaidUser);
}

Running the example produces the following output.

07:31:50.802 [main] INFO com.iluwatar.featuretoggle.App -- Welcome Jamie No Code. You're using the enhanced welcome message.
07:31:50.804 [main] INFO com.iluwatar.featuretoggle.App -- Welcome to the application.
07:31:50.804 [main] INFO com.iluwatar.featuretoggle.App -- I suppose you can use this software.
07:31:50.804 [main] INFO com.iluwatar.featuretoggle.App -- You're amazing Jamie Coder. Thanks for paying for this awesome software.

Applicability

Use the Feature Toggle pattern when

  • Conditional feature access to different users and groups.
  • Rolling out a new feature incrementally.
  • Switching between development and production environments.
  • Quickly disable problematic features
  • External management of feature deployment
  • Ability to maintain multiple version releases of a feature
  • 'Hidden' deployment, releasing a feature in code for designated testing but not publicly making it available

Known Uses

  • Web development platforms use feature toggles to gradually roll out new features to users to ensure stability.
  • Enterprise applications use feature toggles to enable or disable features during runtime to cater to different market needs.

Consequences

Benefits:

  • Facilitates A/B testing and canary releases.
  • Allows for quicker rollback and minimal risk deployments.
  • Enables conditional feature execution without redeploying the application.

Trade-offs:

  • Code complexity is increased.
  • Testing of multiple states is harder and more time-consuming.
  • Potential for technical debt if toggles remain in the code longer than necessary.
  • Risk of toggle misconfiguration leading to unexpected behavior.
  • Strategyopen in new window: Both patterns allow changing the behavior of software at runtime. The Feature Toggle changes features dynamically, while the Strategy allows switching algorithms or strategies.
  • Observeropen in new window: Useful for implementing feature toggles by notifying components of feature state changes, which allows dynamic feature modification without restarts.

Credits