Completed
✅ Multi-Database Architecture Pattern (2025-12-23)
- Refactored leave module to self-contained structure
- Organized repositories under
repository/mongodb/subdirectory - Clear separation between interface (ports) and implementation (mongodb)
- Makes it easy to add alternative database implementations in the future
Planned
🔜 Composite Repository Pattern for Caching
Priority: HighEstimated Effort: Medium (2-3 days)
Dependencies: Redis client integration
Overview
Implement a caching layer using the Composite/Decorator pattern to significantly improve read performance for frequently accessed data.Implementation Plan
Phase 1: Infrastructure Setup- Add Redis client to
app.Container - Configure Redis connection in
config/config.go - Add Redis health check to server startup
repository/redis/:
repository/composite/:
module.go to optionally wrap repositories with caching:
Target Metrics
- Read Latency: 50ms → 1-3ms (17-50x faster)
- Database Load: 90% reduction for cached data
- Cache Hit Rate: Target 80%+ for leave balances and types
Caching Strategy
What to Cache:-
✅ Leave Types (High priority)
- Rarely changes
- Read on every request
- TTL: 1 hour
-
✅ Leave Balances (Medium priority)
- Changes infrequently
- Read frequently
- TTL: 5 minutes
- Invalidate on: Deduct, Create, Update
-
❌ Leave Requests (Low priority)
- Changes frequently
- CQRS read model is already optimized
- May consider later if needed
- Write operations automatically invalidate related cache keys
- Use cache tags for bulk invalidation
- Fallback to database on cache errors
Configuration
Testing Strategy
- Unit tests for cache repository implementations
- Integration tests for composite pattern
- Load tests to verify performance improvements
- Chaos tests for cache failure scenarios
Rollout Plan
- Deploy to staging with caching enabled
- Monitor cache hit/miss rates
- Tune TTL values based on metrics
- Gradual rollout to production (feature flag)
🔮 Future Considerations
PostgreSQL Support (Optional)
Priority: LowEffort: Medium If needed in the future, add PostgreSQL implementation:
module.go to switch based on config:
Read Replicas
Priority: LowEffort: High For very high read loads:
- Separate read/write database connections
- Route queries to read replicas
- Route commands to primary database
Notes
Why Composite Pattern for Caching?
- ✅ Transparent to service layer (no code changes)
- ✅ Easy to enable/disable via configuration
- ✅ Single Responsibility: Each repository does one thing
- ✅ Testable: Can test cache and database separately
- ✅ Flexible: Easy to swap cache implementations
Alternative Approaches Considered
- Cache in Service Layer: ❌ Violates SRP, harder to test
- Cache in HTTP Handler: ❌ Doesn’t help internal service calls
- Decorator Pattern: ✅ Same as Composite (synonym in this context)
Metrics to Track
Performance
- Average response time for leave balance queries
- Database query count per minute
- Cache hit/miss ratio
- Redis memory usage
Business
- API availability (uptime)
- Error rate
- User satisfaction (response time < 100ms)