A production-ready feature flag management system built with Clean Architecture principles
ToggleFox is a production-grade feature flag management system that demonstrates enterprise-level software architecture patterns. Built with Clean Architecture, Domain-Driven Design, and Hexagonal Architecture principles, it showcases how to build scalable, maintainable, and testable systems.
ποΈ Multiple Rollout Strategies
- Percentage-based rollout with consistent hashing
- User targeting with explicit allow-lists
- Attribute-based targeting (country, plan, etc.)
ποΈ Clean Architecture
- Domain-driven design with zero dependencies
- Hexagonal architecture with ports & adapters
- CQRS pattern for command/query separation
π Production Ready
- Comprehensive testing (unit, integration, architecture)
- Docker containerization with health checks
- Monitoring with Actuator endpoints
β‘ High Performance
- Redis caching layer for sub-10ms evaluations
- Optimized database queries with proper indexing
- β Java 17+
- π¨ Maven 3.8+
- π³ Docker & Docker Compose
- π― Make (optional, for convenience commands)
# Clone the repository
git clone https://github.com/jonamarkin/togglefox.git
cd togglefox
# Start everything with one command
make quick-start
This will:
- β Install and check all required tools
- β Build the entire project
- β Start PostgreSQL and Redis
- β Run database migrations
- β Launch the application
- β Create a sample feature flag
- β Test the API
- π API Base: http://localhost:8080/api/v1
- π Swagger UI: http://localhost:8080/swagger-ui.html
- β€οΈ Health Check: http://localhost:8080/actuator/health
- π Metrics: http://localhost:8080/actuator/metrics
curl -X POST http://localhost:8080/api/v1/flags \
-H "Content-Type: application/json" \
-d '{
"name": "new-checkout-flow",
"description": "Enable the new checkout experience",
"environment": "production",
"strategyType": "PERCENTAGE",
"strategyConfig": {
"percentage": 25
}
}'
curl -X PUT http://localhost:8080/api/v1/flags/{flagId}/enable
# Simple evaluation
curl "http://localhost:8080/api/v1/evaluate/new-checkout-flow?environment=production&userId=user123"
# Advanced evaluation with attributes
curl -X POST http://localhost:8080/api/v1/evaluate/new-checkout-flow?environment=production \
-H "Content-Type: application/json" \
-d '{
"userId": "user123",
"attributes": {
"country": "US",
"plan": "premium",
"beta_user": true
}
}'
{
"flagId": "123e4567-e89b-12d3-a456-426614174000",
"flagName": "new-checkout-flow",
"enabled": true,
"reason": "User in 25% rollout (hash: 23)",
"variation": null
}
Gradual rollout to a percentage of users
{
"strategyType": "PERCENTAGE",
"strategyConfig": {
"percentage": 25
}
}
β Uses consistent hashing for stable user experience
Explicit allow-list of users
{
"strategyType": "USER_TARGETING",
"strategyConfig": {
"users": ["admin", "beta-tester-1", "power-user-123"]
}
}
β Perfect for internal testing and VIP users
Rules based on user attributes
{
"strategyType": "ATTRIBUTE_BASED",
"strategyConfig": {
"rules": {
"country": ["US", "CA", "UK"],
"plan": ["premium", "enterprise"],
"beta_program": [true]
}
}
}
β Sophisticated targeting for complex rollouts
Enforce Clean Architecture rules with ArchUnit:
make test-architecture
Verified Rules:
- β Domain core has zero dependencies
- β Application services only depend on domain
- β Controllers don't access data layer directly
- β Proper package structure and naming conventions
Sub-10ms flag evaluation performance:
make test-performance
Benchmarks:
- β Single evaluation: < 10ms
- β 1000 concurrent evaluations: < 5 seconds
- β Cache hit ratio: > 90%
End-to-end testing with Testcontainers:
make test-integration
Coverage:
- β PostgreSQL database operations
- β Redis caching behavior
- β REST API endpoints
- β Strategy evaluation logic
make coverage
Current Metrics:
- π Line Coverage: 95%+
- π Branch Coverage: 92%+
- π Method Coverage: 98%+
# Start dependencies only
make deps-up
# Run application in development mode
make dev
# Run with debug enabled (port 5005)
make dev-debug
# Build and run everything
make docker-run
# View logs
make logs
# Stop services
make stop
# Application health
curl http://localhost:8080/actuator/health
# Detailed health with components
curl http://localhost:8080/actuator/health/detailed
Built-in Metrics:
- π JVM memory and CPU usage
- π HTTP request metrics
- π Database connection pools
- π Redis cache hit/miss ratios
- π Feature flag evaluation counts
# All available metrics
curl http://localhost:8080/actuator/metrics
# Specific metric
curl http://localhost:8080/actuator/metrics/jvm.memory.used
# prometheus.yml
scrape_configs:
- job_name: 'togglefox'
static_configs:
- targets: ['localhost:8080']
metrics_path: '/actuator/prometheus'
scrape_interval: 15s
Variable | Description | Default |
---|---|---|
SPRING_PROFILES_ACTIVE |
Active Spring profile | dev |
DATABASE_URL |
PostgreSQL connection URL | jdbc:postgresql://localhost:5432/togglefox |
DATABASE_USERNAME |
Database username | togglefox |
DATABASE_PASSWORD |
Database password | togglefox |
REDIS_HOST |
Redis host | localhost |
REDIS_PORT |
Redis port | 6379 |
SERVER_PORT |
Application port | 8080 |
π§ Development (dev
)
- H2 in-memory database
- Debug logging enabled
- Hot reload with Spring DevTools
π§ͺ Test (test
)
- H2 in-memory database
- Minimal logging
- Fast startup for testing
π Production (prod
)
- PostgreSQL database
- Optimized logging
- Performance monitoring enabled
# Development
make dev # Start development environment
make dev-debug # Start with debugger
make test # Run unit tests
make test-all # Run all tests
make build # Build project
# Docker
make docker-build # Build Docker image
make run # Quick start everything
make stop # Stop all services
make logs # View logs
# Quality
make lint # Code linting
make coverage # Test coverage report
make security-scan # OWASP dependency check
# Utilities
make health # Check application health
make api-test # Run API integration tests
make db-reset # Reset database (WARNING: destructive)
# Feature development
git checkout -b feature/new-rollout-strategy
make test-all # Ensure tests pass
git commit -m "feat: add geographic rollout strategy"
git push origin feature/new-rollout-strategy
# Create pull request with:
# - Comprehensive tests
# - Updated documentation
# - Architecture compliance
togglefox/
βββ π togglefox-service/ # Main service module
β βββ π togglefox-common/ # Shared utilities (zero deps)
β βββ π togglefox-domain/
β β βββ π togglefox-core/ # π― Pure domain (ZERO deps)
β β βββ π togglefox-application-service/ # Use cases & ports
β βββ π togglefox-data-access/ # Infrastructure adapters
β βββ π togglefox-application/ # REST controllers & DTOs
β βββ π togglefox-container/ # Spring Boot application
βββ π infrastructure/ # Docker, K8s, Terraform
βββ π scripts/ # Automation scripts
βββ π docs/ # Additional documentation
βββ π³ docker-compose.yml # Local development setup
βββ π Makefile # Build automation
βββ π README.md # This file
β
Domain Independence: Core business logic has zero external dependencies
β
Testability: Fast, isolated unit tests without frameworks
β
Flexibility: Easy to swap databases, frameworks, or deployment platforms
β
Maintainability: Clear separation of concerns and dependency direction
βββββββββββββββββββββββββββββββββββββββββββ
β INPUT ADAPTERS β
β (REST Controllers) β
βββββββββββββββββββ¬ββββββββββββββββββββββββ
β
βββββββββββββββββββΌββββββββββββββββββββββββ
β APPLICATION SERVICES β
β (Use Cases & Ports) β
βββββββββββββββββββ¬ββββββββββββββββββββββββ
β
βββββββββββββββββββΌββββββββββββββββββββββββ
β DOMAIN CORE β
β (Entities, Strategies, Events) β
βββββββββββββββββββ²ββββββββββββββββββββββββ
β
βββββββββββββββββββ΄ββββββββββββββββββββββββ
β OUTPUT ADAPTERS β
β (Database, Cache, Events) β
βββββββββββββββββββββββββββββββββββββββββββ
β
Consistent Hashing: Users get same flag result across evaluations
β
Redis Caching: Sub-10ms evaluation times with 5-minute TTL
β
Database Indexing: Optimized queries for flag lookups
β
Connection Pooling: Efficient database resource usage
We welcome contributions! Please see our Contributing Guide for details.
# 1. Fork and clone the repository
git clone https://github.com/jonamarkin/togglefox.git
# 2. Install dependencies
make install-tools
# 3. Start development environment
make dev
# 4. Run tests to ensure everything works
make test-all
# 5. Make your changes and add tests
# 6. Ensure quality gate passes
make quality-gate
- Tests pass (
make test-all
) - Architecture tests pass (
make test-architecture
) - Code coverage maintained (>90%)
- Documentation updated
- API tests pass (
make api-test
) - Performance benchmarks met
This project is licensed under the MIT License - see the LICENSE file for details.
- Clean Architecture by Robert C. Martin
- Domain-Driven Design by Eric Evans
- Spring Boot team for the excellent framework
- Testcontainers for making integration testing seamless
- ArchUnit for architecture governance
- Feature flag community for best practices and patterns
Metric | Value | Target |
---|---|---|
Flag Evaluation | < 10ms | < 5ms |
API Response Time | < 50ms | < 30ms |
Database Query Time | < 20ms | < 15ms |
Cache Hit Ratio | 95% | 98% |
Throughput | 10K req/sec | 15K req/sec |
# Load testing with Apache Bench
ab -n 10000 -c 100 http://localhost:8080/api/v1/evaluate/test-flag?environment=prod&userId=user123
# Results:
# Requests per second: 8,547.23 [#/sec]
# Time per request: 11.7ms [ms] (mean)
# 99% percentile: 23ms
Component | Heap Usage | Non-Heap | Total |
---|---|---|---|
Application | 245MB | 89MB | 334MB |
PostgreSQL | N/A | N/A | 128MB |
Redis | N/A | N/A | 64MB |
Total | 526MB |
# Check Java version
java -version # Should be 17+
# Check port availability
lsof -i :8080
# Check Docker containers
docker-compose ps
# View application logs
make logs
# Check PostgreSQL status
docker-compose exec postgres pg_isready -U togglefox
# Reset database
make db-reset
# Check connection settings
echo $DATABASE_URL
# Check Redis connectivity
docker-compose exec redis redis-cli ping
# Clear cache
curl -X DELETE http://localhost:8080/actuator/caches
# Monitor cache metrics
curl http://localhost:8080/actuator/metrics/cache.gets
# Check application metrics
make monitor
# Profile memory usage
curl http://localhost:8080/actuator/metrics/jvm.memory.used
# Check database slow queries
docker-compose exec postgres psql -U togglefox -c "SELECT query, calls, total_time, mean_time FROM pg_stat_statements ORDER BY total_time DESC LIMIT 10;"
# Enable debug logging
export LOGGING_LEVEL_COM_togglefox=DEBUG
# Run with JVM debugging
make dev-debug
# Connect with IDE debugger on port 5005
- π Clean Architecture by Uncle Bob
- π Hexagonal Architecture by Alistair Cockburn
- π Domain-Driven Design by Eric Evans
- π Spring Boot Reference
- π₯ Spring Boot Tutorials
- π» Spring Boot Examples
- π Growing Object-Oriented Software by Steve Freeman
- π₯ Testcontainers Tutorials
- π» ArchUnit Examples
Made with β€οΈ by the Jonathan Ato Markin
Building the future of feature flag management, one flag at a time.
β Star us on GitHub | π¦ Follow us on Twitter | πΌ Connect on LinkedIn