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
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
Step 2: Define Feature Enum
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
@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
@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
docker run -p 4242:4242 unleashorg/unleash-server
Step 2: Add Client Dependency
<dependency>
<groupId>io.getunleash</groupId>
<artifactId>unleash-client-java</artifactId>
<version>7.0.0</version>
</dependency>
Step 3: Configure Unleash Client
@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
@GetMapping("/dashboard")
public String dashboard(Unleash unleash) {
if (unleash.isEnabled("new-dashboard")) {
return "new-dashboard";
}
return "legacy-dashboard";
}
4. Comparing Togglz vs Unleash
| Feature | Togglz | Unleash |
|---|---|---|
| Architecture | Library | Client-Server |
| UI | Basic admin console | Full-featured dashboard |
| Persistence | File/DB/JDBC | Built-in PostgreSQL |
| Scalability | Good for single app | Designed for distributed |
| Advanced | Custom activation logic | Gradual rollouts, metrics |
5. Best Practices for Feature Flags
Before implementing your feature flag system, consider these fundamental principles:
- Flags are temporary – They should have a defined lifecycle
- Flags impact performance – Every check adds overhead
- Flags require governance – Unmanaged flags create technical debt
Feature Flag Best Practices
| Practice | Description | Implementation Tip |
|---|---|---|
| Lifecycle Management | Define expiration for all flags | Set automatic archival after 90 days |
| Naming Convention | Consistent, descriptive flag names | Use domain-feature-environment format (e.g., cart-newui-prod) |
| Documentation | Maintain flag metadata | Store purpose, owner and timeline in README |
| Performance Monitoring | Track flag evaluation impact | Log flag evaluation time >5ms |
| Security Controls | Restrict flag modifications | Implement RBAC for production flags |
| Testing Strategy | Verify both enabled/disabled states | Include in CI/CD pipeline checks |
| Cleanup Process | Remove unused flags | Monthly 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:
// Hybrid approach example
if (unleash.isEnabled("experimental-feature")
&& Features.EXPERIMENTAL.isActive()) {
// New feature code
}
Final Recommendations:
- Start with Togglz for simplicity, migrate to Unleash as needs grow
- Implement monitoring from day one (track flag usage metrics)
- 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:




