When to Use Caching
1
Read-heavy workloads
The same data is accessed frequently.
2
Expensive computations or queries
Results can be stored and reused.
3
Data that doesn't change often
Or where slight staleness is acceptable.
4
Reducing load on downstream services
Take pressure off databases and APIs.
Cache Architectures
- Cache-Aside (Lazy Loading): App checks cache first, on miss reads from DB and populates cache. Most common pattern.
- Write-Through: Every write goes to cache AND DB simultaneously. Ensures consistency but adds write latency.
- Write-Behind (Write-Back): Writes go to cache first, async flush to DB. Fast writes but risk of data loss.
- Read-Through: Cache itself handles fetching from DB on miss. Simplifies app logic.
Eviction Policies
- LRU (Least Recently Used): Evicts the least recently accessed item. Most common default.
- LFU (Least Frequently Used): Evicts the least frequently accessed item. Good for skewed access patterns.
- TTL (Time-To-Live): Items expire after a set duration. Simple and predictable.
- FIFO: First in, first out. Rarely used in practice.
Cache Types
- In-Process Cache: Lives in app memory (HashMap). Fastest but not shared across instances.
- External Cache (Redis/Memcached): Shared across instances. Adds network hop (~1ms).
- CDN Cache: Edge servers cache static assets close to users. Great for images, JS, CSS.
- Client-Side Cache: Browser cache, HTTP cache headers (Cache-Control, ETag, Last-Modified).
Key Numbers
Redis
100k+ ops/sec, sub-millisecond latency
Memcached
Similar throughput, simpler (no persistence)
CDN hit
~5-20ms vs origin ~100-500ms
In-process cache
Nanoseconds
Common Pitfalls
- Cache Stampede: Many requests hit cache miss simultaneously. Fix: locking, request coalescing.
- Stale Data: Cache returns outdated data. Fix: TTL, cache invalidation on writes.
- Cold Start: Empty cache after restart causes DB overload. Fix: cache warming.
- Over-caching: Caching everything wastes memory. Cache only hot data.
Reading Progress
On This Page

Your account is free and you can post anonymously if you choose.