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" ) .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
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:
1 2 3 4 5 | // 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: