How would you design a multi-level caching architecture?
Layer caches by distance from the caller and accept that each layer adds staleness and invalidation complexity.
Typical design:
- Edge/CDN: cache public and static responses close to users with long TTLs and purge by tag.
- Service-local in-process cache: very small, very short TTLs (seconds) for the hottest keys, giving sub-millisecond reads and absorbing hot keys.
- Shared distributed cache such as Redis: the main cross-instance cache with TTLs and explicit invalidation.
- Origin database: the source of truth, protected by the caches above.
client -> CDN -> local (1s) -> Redis (60s) -> DB
Rules: shorter TTLs and more aggressive invalidation as you move inward; namespace keys per environment and version; handle cache outages by falling back to the origin with protection such as circuit breakers; and add negative caching for known-missing keys. Because stale data can appear at several layers, define the acceptable staleness per data type and prefer deleting over updating. Measure hit ratio and latency per layer, and expect to tune TTLs continuously.