advanced
Application cache
Use in-process or distributed caches for expensive reads while controlling stampedes and stale data.
Application caches live in-process (local) or in a distributed store like Redis/Memcached. They accelerate expensive reads — database queries, aggregation results, session data — with microsecond to millisecond latency. Local caches are fastest but inconsistent across instances; distributed caches unify state at network cost.
Guard against cache stampedes with single-flight, probabilistic early expiration, or request coalescing. Set memory limits and eviction policies (LRU, LFU).
On interviews: place Redis in your architecture, explain local versus distributed trade-off, and describe stampede mitigation.
Common pitfalls: unbounded in-process cache causing OOM; no TTL on hot keys; caching errors or null results incorrectly.
Checklist:
- Choose local versus distributed per data type.
- Set TTL, size limits, and eviction policy.
- Mitigate stampede on popular keys.
- Monitor hit rate and memory pressure.