Runtime Feature Toggling with Spring & Togglz

Fatih İver
Midas Engineering
Published in
4 min readJul 31, 2022

--

What is runtime feature toggling?

Runtime feature toggling is the ability to turn features off/on at runtime without any deployment or restart.

In this post, we’ll write a microservice that will help us toggle features at runtime by using Spring — a web framework, and Togglz — a feature toggling library.

Let’s name this microservice “feature-API”.

Here is the pom.xml:

Togglz provides a dependency to be used with Spring and another dependency that provides an admin panel to toggle features off and on.

After adding the Togglz dependency, we can now write our feature enum class that is needed by Togglz library:

In this enum class, we define the features that will be toggled. For this post, an enum named NEW_WEBSITE will be used. Name your features as you wish.

Another thing in this enum class is the “isActive” method. There are two methods with this name. One of them takes no argument. This method will be used for features that are not user-specific and the other one will be used for user-specific features. The one that takes the user name needs to create a Togglz user and bind it to the current thread so that the Togglz library can understand who the current user is.

Now, let’s do the necessary configuration:

There are 3 beans initialized in this class.

FeatureProvider bean tells the Togglz library where the features are. It is the enum class we defined.

StateRepository bean tells Togglz library where to store feature data. It is an in-memory repository for this simple project. You may use a database or any other data source you wish.

UserProvider bean tells the Togglz library how to find out the user. ThreadLocalUserProvider is used for this simple project. This is why we need to bind the user to the current thread for user-specific feature requests.

As last, let’s see the controller:

An endpoint is provided to question whether a feature is active for a user or not. User parameter is optional since some features are not user-based due to their nature. This endpoint answers the following questions:

— Is feature X active?

— Is feature X active for user Y?

Based on the feature’s configuration, the Togglz library will say yes or no, and this endpoint will return this boolean answer.

Let’s start our project by executing the following command:

The endpoint above can be questioned as follows:

If it is a user-specific feature, then:

Now, let’s come to the last part. How to togglz features off and on or how to do more configuration on features?

The answer is by using Togglz Admin Panel. Remember the dependency added.

The panel login address is: http://localhost:8080/togglz-console/index

NEW_WEBSITE feature can be enabled by clicking on the red disabled button.

The feature can be configured further by clicking on the settings button.

Here is the configuration screen:

There is no activation strategy yet. Let’s see the options:

For example, if you choose gradual rollout, a percentage needs to be set. If the percentage is 50, the feature will be active for half of the users and will be inactive for the other half of the users.

As another example, if you choose the “Users by name” strategy, user names need to be set. The feature will be active for those users specified in the panel and will be inactive for the other users.

As the last example, if you choose the “Release Date” activation strategy. A date needs to be set. The feature will be inactive until that day comes and after that day it will act.

That’s all. This is how to toggle/configure features at runtime.

You can find the source code here: https://github.com/fatih-iver/feature-api

Checkout Togglz website: https://www.togglz.org/

--

--