Patterns
35 items
35 items
Multi-tier caching for performance optimization
Caching stores frequently accessed data in fast storage (memory) to reduce latency and database load. The main strategies are: Cache-Aside (application manages cache), Read-Through (cache manages reads), Write-Through (cache manages writes synchronously), Write-Behind (cache manages writes asynchronously), and Refresh-Ahead (proactive cache refresh). Choosing the right strategy depends on read/write ratio, consistency requirements, and failure tolerance. Caching is the single most effective optimization for read-heavy workloads - it can reduce response times from 100ms to 1ms and database load by 99%. Every production system uses caching.
Application checks cache first, falls back to database on miss, then populates cache. Simple, flexible, and handles cache failures gracefully. Default choice for most use cases.
Write-Through: consistent but slower (write to both). Write-Behind: fast but risk data loss (write to cache, async to DB). Write-Around: avoids cache pollution but misses on first read.
"There are only two hard things in CS: cache invalidation and naming things." When source data changes, cache must be invalidated. TTL, explicit invalidation, and event-driven updates each have trade-offs.
High hit ratio (>90%) means cache is effective. Low hit ratio means cache size too small, TTL too short, or access pattern not cache-friendly. Monitor and tune.
When cache is empty (restart, invalidation), all requests hit database simultaneously. Protect with request coalescing, background refresh, or gradual warming.
L1 cache (in-process) → L2 cache (Redis) → Database. Each level trades off latency vs freshness. Browser → CDN → API cache → Database for web apps.
| Aspect | Advantage | Disadvantage |
|---|---|---|