1.7 KiB
1.7 KiB
ADR-0022: Cache Implementation
Status
Accepted
Context
The platform needs caching for:
- Performance optimization (reduce database load)
- Frequently accessed data (user permissions, roles)
- Session data (optional)
- Query results
Options considered:
- Redis - Industry standard, feature-rich
- In-memory cache - Simple, no external dependency
- Memcached - Simple, but less features than Redis
- No cache - Simplest, but poor performance at scale
Decision
Use Redis as the primary cache with in-memory fallback:
- Primary: Redis for production
- Fallback: In-memory cache for development/testing
- Interface abstraction:
Cacheinterface allows swapping implementations - Use cases: Permission lookups, role assignments, query caching
Rationale:
- Industry standard, widely supported
- Rich feature set (TTL, pub/sub, etc.)
- Can be shared across instances (multi-instance deployments)
- Good performance
- Easy to abstract behind interface
Consequences
Positive
- High performance
- Shared across instances
- Rich feature set
- Easy to scale horizontally
- Abstraction allows swapping implementations
Negative
- Additional infrastructure dependency
- Network latency (minimal with proper setup)
- Need to handle Redis failures gracefully
Implementation Notes
- Install:
github.com/redis/go-redis/v9 - Create
pkg/infra/cache/cache.gointerface - Implement
internal/infra/cache/redis_cache.go - Implement
internal/infra/cache/memory_cache.gofor fallback - Use connection pooling
- Handle Redis failures gracefully (fallback or error)
- Configure TTLs appropriately (e.g., 5 minutes for permissions)