Overview

About Feature Flags

Togglz is an implementation of the Feature Toggles pattern for Java. Feature Toggles are very useful especially in the context of agile development practices and are used by big sites like Flickr.

Martin Fowler defines Feature Toggles like this:

The basic idea is to have a configuration file that defines a bunch of toggles for various features you have pending. The running application then uses these toggles in order to decide whether or not to show the new feature.

So the basic idea is to define a toggle for each new feature you implement. As this toggle is disabled by default you can safely deploy the application to the production servers even if the feature is not completely finished and tested. If the feature implementation is complete you can enable it in the production system at any point in time.

This concepts brings many advantages:

  • You are able to deploy the application at any time. Even if not all features are completely implemented. Just disable the features that shouldn't be visible for the users.
  • The deployment of a new version of your application doesn't mean all the changes are effective. You can do a Dark Launch, which means that you first deploy the new version and activate features at a later point in time.
  • You can test a new feature in production by activating it only for a small group of beta testers. This is also known as Dark Testing.
  • If you find a serious bug in a new feature, you can immediately disable it until you are able to deploy a bugfix.

About Togglz

Togglz has been developed for providing simple to use feature flags for Java applications.

The following table contains definitions for some basic terms. It is very important to understand these because they will be used in the following sections.

Term Description
feature A feature represents a single characteristic that you can enable or disable. Features in Togglz are typically represented by a Java enum with one value for each feature.
enabled / disabled A feature is either enabled or disabled for the entire application. Note that being enabled does not guarantee that the feature will be active, unless no activation strategy has been defined for it.
active / inactive Enabled features that are also active are visible and usable by the users while enabled, but inactive, features are hidden and therefore not usable. The section below, titled "The Feature State" explains this in more detail.

Features are simply declared using a standard Java enum type. Annotations can be used to enrich the feature with additional metadata.

public enum MyFeatures implements Feature {

    @Label("First Feature")
    FEATURE_ONE,
    
    @Label("Second Feature")
    @ActiveByDefault
    FEATURE_TWO;
    
    public boolean isActive() {
        return FeatureContext.getFeatureManager().isActive(this);
    }
    
}

The Togglz API has been developed in a way that makes it very easy to check whether a feature is active or not. Just call the isActive() method on the corresponding feature enum value.

public void someBusinessMethod() {

  if( MyFeatures.FEATURE_ONE.isActive() ) {
    // do new exciting stuff here
  }
  
}

The feature state

The state of a feature in Togglz consists of the following properties:

  • This enabled flag specifies whether a feature is currently turned on (enabled) or off (disabled).
  • An optional activation strategy allows you to specify in which situations an enabled feature is active. You can for example restrict a feature to a certain list of users or to a specific time frame. Have a look at the Activation Strategies chapter to learn more about activation strategies.
  • An optional list of strategy parameters are used to configure the activation strategy.

The values of these properties can be used to determine whether a feature is active or not:

  • A feature is active if it is enabled and there is either no activation strategy or the strategy evaluates to true.
  • A feature is inactive if it is either disabled or there is a strategy defined which evaluates to false.