Enterprise Java

Feature Flags in Spring Boot: Unleash vs Togglz

Modern applications need the ability to toggle features dynamically without redeploying code. Feature flags (or feature toggles) enable:
✅ Gradual feature rollouts
✅ A/B testing capabilities
✅ Emergency kill switches
✅ Environment-specific configurations

In this guide, we’ll implement two popular Java feature flag solutions in Spring Boot:
🔹 Unleash (Open-source feature management platform)
🔹 Togglz (Mature feature toggle library)

1. Why Use Feature Flags in Spring Boot?

Key Benefits

  • Reduce Risk – Disable problematic features instantly
  • Enable CI/CD – Decouple deployment from feature release
  • Improve Testing – Test features in production with select users
  • Simplify Maintenance – Manage features via UI/dashboard

Common Use Cases

  • Canary releases
  • Permission-based feature access
  • Holiday-themed UI changes
  • Experimental features

2. Implementing Feature Flags with Togglz

Step 1: Add Dependencies

1
2
3
4
5
<dependency>
    <groupId>org.togglz</groupId>
    <artifactId>togglz-spring-boot-starter</artifactId>
    <version>3.2.0</version>
</dependency>

Step 2: Define Feature Enum

01
02
03
04
05
06
07
08
09
10
11
public enum Features implements Feature {
    @Label("New Checkout Flow")
    NEW_CHECKOUT,
     
    @Label("Dark Mode")
    DARK_MODE;
     
    public boolean isActive() {
        return FeatureContext.getFeatureManager().isActive(this);
    }
}

Step 3: Configure Togglz

01
02
03
04
05
06
07
08
09
10
11
12
@Configuration
public class TogglzConfig {
    @Bean
    public FeatureProvider featureProvider() {
        return new EnumBasedFeatureProvider(Features.class);
    }
     
    @Bean
    public StateRepository stateRepository() {
        return new FileBasedStateRepository(new File("/tmp/features.properties"));
    }
}

Step 4: Use in Controller

1
2
3
4
5
6
7
@GetMapping("/checkout")
public String checkout() {
    if (Features.NEW_CHECKOUT.isActive()) {
        return "new-checkout";
    }
    return "legacy-checkout";
}

3. Implementing Feature Flags with Unleash

Step 1: Set Up Unleash Server

1
docker run -p 4242:4242 unleashorg/unleash-server

Step 2: Add Client Dependency

1
2
3
4
5
<dependency>
    <groupId>io.getunleash</groupId>
    <artifactId>unleash-client-java</artifactId>
    <version>7.0.0</version>
</dependency>

Step 3: Configure Unleash Client

01
02
03
04
05
06
07
08
09
10
@Bean
public Unleash unleash() {
    return new DefaultUnleash(
        Config.builder()
            .appName("spring-boot-app")
            .instanceId("instance-1")
            .unleashAPI("http://localhost:4242/api/")
            .build()
    );
}

Step 4: Use Feature Flags

1
2
3
4
5
6
7
@GetMapping("/dashboard")
public String dashboard(Unleash unleash) {
    if (unleash.isEnabled("new-dashboard")) {
        return "new-dashboard";
    }
    return "legacy-dashboard";
}

4. Comparing Togglz vs Unleash

FeatureTogglzUnleash
ArchitectureLibraryClient-Server
UIBasic admin consoleFull-featured dashboard
PersistenceFile/DB/JDBCBuilt-in PostgreSQL
ScalabilityGood for single appDesigned for distributed
AdvancedCustom activation logicGradual rollouts, metrics

5. Best Practices for Feature Flags

Before implementing your feature flag system, consider these fundamental principles:

  1. Flags are temporary – They should have a defined lifecycle
  2. Flags impact performance – Every check adds overhead
  3. Flags require governance – Unmanaged flags create technical debt

Feature Flag Best Practices

PracticeDescriptionImplementation Tip
Lifecycle ManagementDefine expiration for all flagsSet automatic archival after 90 days
Naming ConventionConsistent, descriptive flag namesUse domain-feature-environment format (e.g., cart-newui-prod)
DocumentationMaintain flag metadataStore purpose, owner and timeline in README
Performance MonitoringTrack flag evaluation impactLog flag evaluation time >5ms
Security ControlsRestrict flag modificationsImplement RBAC for production flags
Testing StrategyVerify both enabled/disabled statesInclude in CI/CD pipeline checks
Cleanup ProcessRemove unused flagsMonthly review + automated pruning

Conclusion

When implementing feature flags in Spring Boot, your choice between Togglz and Unleash should be guided by:

Choose Togglz when:

  • You need a simple, self-contained solution
  • Your team prefers Java-centric configuration
  • You have limited infrastructure resources

Choose Unleash when:

  • You manage multiple services/languages
  • You need advanced rollout strategies
  • Your team values centralized management

Pro Tip: For large-scale implementations, consider combining both:

1
2
3
4
5
// Hybrid approach example
if (unleash.isEnabled("experimental-feature")
    && Features.EXPERIMENTAL.isActive()) {
    // New feature code
}

Final Recommendations:

  1. Start with Togglz for simplicity, migrate to Unleash as needs grow
  2. Implement monitoring from day one (track flag usage metrics)
  3. Build flag retirement into your development workflow

“Feature flags are powerful but require discipline – treat them like radioactive material: useful but dangerous when mishandled.” – Adapted from Martin Fowler

Resources for Implementation:

Eleftheria Drosopoulou

Eleftheria is an Experienced Business Analyst with a robust background in the computer software industry. Proficient in Computer Software Training, Digital Marketing, HTML Scripting, and Microsoft Office, they bring a wealth of technical skills to the table. Additionally, she has a love for writing articles on various tech subjects, showcasing a talent for translating complex concepts into accessible content.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button