Feature Toggles for Spring Boot Applications with Togglz

Last Updated:  July 19, 2022 | Published: August 11, 2019

Mega Spring Boot Bundle Sale: Unleash the Ultimate Mega Bundle Packed with 10 Game-Changing Digital Products (Online Courses & eBooks) on Spring Boot, Java, Testing, AWS, and Architecture. Available until the 27th of February.

With continuous integration (CI) and continuous deployment (CD), our changes and features are usually deployed to production whenever our pipeline passes. There might be features we don't want to activate until a specific date or only enable them for particular users. For such use cases, we can use so-called feature toggles aka. feature flags.

These are usually boolean flags to indicate whether to expose functionality or not. We can achieve this with a static variable in Java, but then we need to redeploy the application whenever we want to enable a specific feature. Fortunately, there is a convenient library for the Java ecosystem to create more advanced feature toggles: Togglz. This library comes with an excellent Spring Boot integration and is easy to set up.

We're going to use the following technologies for this blog post: Java 17, Spring Boot 2.7, and Togglz 3.2.1.

Spring Boot Maven Project Setup

The Maven project for this application looks like the following:

The Togglz project offers a dedicated Spring Boot library to autoconfigure everything we need to start.

In addition, we include the togglz-console dependency to enable and disable feature toggles later on with a simple web UI.

Guard Features With Togglz

For a sample use case to utilize feature toggles, we plan to toggle specific features for two REST endpoints that serve data about books.

Depending on whether or not a feature toggle is active, the application will expose additional information and content for the second endpoint.

The main interaction with the Togglz library is done using the FeatureManager class, which is injectable and configured automatically.

A feature toggle is defined either in the application.properties and accessed with new NamedFeature("NAME") or using a typesafe enum that implements the Feature interface:

Toggle Features with the Togglz Web Console

To enable or disable feature toggles during runtime (no need to restart our application), we can make use of the web console.

The default path is /togglz-console but can be configured in our application.properties files:

The web console offers a visual representation of all of our feature toggles and their current state:

togglzWebConsole

On this overview page, we can change the status of a toggle by clicking on its current status button.

In addition, we can have more fine-grained feature en-/disabling rules. Therefore, we can click on the button in the Actions column. The following page displays a set of pre-defined strategies for changing the state of our feature toggle (e.g., date-based, IP based, etc.):

Further Thoughts on Using Togglz With Spring Boot

The Togglz console can also be seamlessly integrated and secured with Spring Security, using the following dependency:

We can configure the Togglz console to use Spring Boot's management port and apply further security rules for this (e.g., blocked by a firewall internet access):

Using the default configuration, Togglz manages the feature toggle state with an in-memory-based solution. If we run multiple instances of our application (scaling out), we should switch to a different approach with a shared persistence.

There are out-of-the-box integrations for feature state management with a JDBC data source and MongoDB available.

For further information about feature toggles in general, take a look at the following blog post on Martin Fowler's homepage.

The source code for this example is available on GitHub.

Joyful toggling,

Philip

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>