Pogorelovs

Navigate back to the homepage

Unleash - ready to go feature toggle solution

Sergey Pogorelov
July 10th, 2020 · 1 min read

For more information about what feature toggle is - visit Feature Toggles aka Feature Flags

Unleash is a feature toggle system that might be set-up in a minutes! In this post, I want to show a quick set-up demo for this powerful tool.

Prerequisites

For the infrastructure setup, it’s required to have Docker and docker-compose. For the demo app build it’s required to have Java > 11 and Gradle.

Prepare the infrastructure

To run the Unleash server we need to have Node.js >= 12 and a PostgreSQL >= 10 For the demo purposes let’s use docker images of Unleash and PostgreSQL.

I’ve prepared the following docker-compose.yml:

1version: "3"
2services:
3 postgres:
4 image: postgres
5 environment:
6 - POSTGRES_DB=unleash
7 - POSTGRES_PASSWORD=${POSTGRES_PASS}
8 unleash:
9 image: unleashorg/unleash-server
10 environment:
11 - DATABASE_URL=postgres://postgres:${POSTGRES_PASS}@postgres:5432/unleash
12 restart: on-failure
13 ports:
14 - "4242:4242"

It’ll create both Postgres and Unleash containers and will expose the 4242 port of Unleash, so we’ll be able to access the Unleash management UI.

Now you can open localhost:4242 in your browser and to play with the Unleash UI.

Unleash UI

Unleash in action

I’ve prepared an application that fetches all available toggles from the Unleash server and shows their statuses.

Unleash demo

Unleash client

For the moment, the official Unleash client SDKs available for:

  • Java
  • Python
  • Node.js
  • Go
  • Ruby
  • .Net

Unleash client configuration (Java example)

The client API is self-descriptive, so I’ll just show you the code:

1var unleashConfig = UnleashConfig.builder()
2 .appName("UnleashDemo")
3 .instanceId("Instance 1")
4 .unleashAPI("http://127.0.0.1:4242/api")
5 .fetchTogglesInterval(10)
6 .subscriber(new UnleashSubscriber() {
7 @Override
8 public void togglesFetched(FeatureToggleResponse toggleResponse) {
9 logger.info("Fetched toggles");
10 }
11
12 @Override
13 public void onError(UnleashException unleashException) {
14 logger.info("Error: {}", unleashException.getMessage());
15 }
16 })
17 .build();
18
19var unleash = new DefaultUnleash(unleashConfig);

Once you created the client, you can just use it like:

1if (unleash.isEnabled("someFeature")) {
2 // someFeature code
3}

Important places to notice

Unleash client has the backup mechanism. By default, the client fetches the server every 10 seconds. Once the client fetched the toggles it creates the local backup file in the temp directory (can be overridden by specific file).

This means that if the unleash-server becomes unavailable, the unleash-client will still be able to toggle the features based on the values stored in unleash-repo.json.

And here you may face unexpected behavior when you receive outdated information about toggles because your Unleash server is down or something went wrong. For better diagnostics of that problem don’t forget to add the onError subscriber to the unleash client.

Demo repository

All client app code and the infrastructure related thigs are available in my Unleash demo repository

Join my email list and get notified about new content

Be the first to receive my latest content with the ability to opt-out at anytime. I promise not to spam your inbox or share your email with any third parties.

More articles from Pogorelovs

Installation and configuration of NX server on remote host

Installation and configuration of NX server on remote host...

April 13th, 2020 · 1 min read

Sbt assembly without tests

Sometimes it might be useful to run the `sbt assembly` without tests...

February 7th, 2020 · 1 min read
© 2018–2020 Pogorelovs
Link to $https://github.com/sergeioffLink to $https://www.linkedin.com/in/sergeioff