Joe's
Digital Garden

Feature Flags

Feature flags are a tool that allows new features to be added into a production code base while masking those features until a particular release, date, or keyed to particular beta-test users.

The implementation of a feature flag is simple: a hash of boolean values tied to particular keys denoting when a feature should be enabled in a particular environment.

class FeatureFlag
{
    private static $flags = [
        'production' => [
            'add-user' => true,
            'delete-user' => false,
        ],
        'development' => [
            'add-user' => true,
            'delete-user' => true
        ],
    ];

    public static function isEnabled(string $feature): bool
    {
        return static::$flags[getenv('ENV')][$feature];
    }
}

We can then wrap various elements of the UI around conditional calls to the FeatureFlag class. e.g.

<nav>
    <ul>
        <li>Add User</li>
        <?php if(FeatureFlag::isEnabled('delete-user')): ?>
        <li>Delete User</li>
        <?php endifl ?>
    </ul>
</nav>

In this case the UI element for Deleting a User will not appear in the production environment, but will appear in the development environment. When the time comes to release the feature, a hotfix goes out to production to enable this feature.

Rajiv Prab outlines when to use Feature Flags, namely that they are best used for A/B testing or to allow multi-sprint running Epics to be continuously integrated into the production code to avoid situations of progressive drift between a long-running feature branch and the production code. They are, at worst used as a form of risk aversion -- a way to "rollback" on a failed release. They also come with expensive upkeep as they exponentially increase the possible versions that a piece of software can be configured -- rapidly exceding the ability for a QA team to test all possible configurations.

In a more complex set up we can:

  • Store the state of the Feature Flags in the database, allowing us to forgo the hotfix.
  • Gate the feature flags to a release date such that after that date the feature turns on.
  • Turn the feature flag on for specified beta tester user ids, or for a particular subset of users

External References

  1. Prab, Rajiv. When Feature Flags Do And Don’t Make Sense, Software the Hard Way. Retrieved 2020-10-12.

Linked References