Feature Toggles for Spring Boot Applications with Togglz

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

With continuous integration (CI) and continuous deployment (CD) your changes and features are usually deployed to production whenever your pipeline passes. There might be features you don't want to use until a specific date or only enable them for specific users. For such use cases, you can make use of so-called feature toggles aka. feature flags. These are usually boolean flags to indicate whether to expose functionality or not. You can achieve this with a static variable in Java but then you need to redeploy the application whenever you want to enable a specific feature. Fortunately, there is a nice library for the Java ecosystem to create more advanced feature toggles: Togglz. This library comes with a good Spring Boot integration and is easy to setup.

I'll use the following technologies for this blog post: Java 11, Spring Boot 2.3, and Togglz 2.6.1.

Spring Boot project setup

The Maven project for this application looks like the following:

The Togglz project offers a dedicated Spring Boot library to autoconfigure everything you need to start. In addition, I've added the togglz-console dependency to enable and disable feature toggles later on with a simple web UI.

Hide features in your application using Togglz

For a sample use case to utilize feature toggles, I've created two REST endpoints to serve information 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 which implements the Feature interface:

Enable/disable feature toggles with the Togglz console

To enable or disable feature toggles once your application is running, you can make use of the web console. The default path is /togglz-console but can be configured in your application.properties files:

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

togglzWebConsole

On this overview page, you can change the status of a toggle by clicking on its current status button. In addition, and if you need a more fine-grained feature en-/disabling, you can click on the button in the Actions column. The following page offers you a set of pre-defined strategies for changing the state of your feature toggle (e.g. date based, IP based, etc.):

Further thoughts on using Togglz alongside Spring Boot

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

You 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 state of a feature toggle with an in-memory based solution. If your application runs multiple instances you should switch to a different approach.  There are out-of-the-box integrations for feature state management with a JDBC data source and MongoDB already available.

Also, be aware that the Togglz library is currently looking for a new maintainer and no active development is happening.

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

You can find the code for this blog post on GitHub.

Have fun using Togglz,

Phil

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