Learning Objectives
- Compare Cache-Aside, Read-Through, Write-Through, and Write-Back strategies by latency and consistency trade-offs.
- Prevent Cache Stampede (Thundering Herd) and Cache Penetration outages.
Prerequisites
- REST API architecture and SQL relational database familiarity
Matching Caching Strategy to Consistency Needs
Adding Redis in front of PostgreSQL cuts read P99 latency from tens of milliseconds down to sub-millisecond memory reads — unless cache invalidation races or simultaneous expirations overload your database. The dominant industry default for web catalogs is Cache-Aside (Lazy Loading) paired with short jittered TTLs and explicit write invalidation.
| Pattern | Read Path | Write Path | Best Fit |
|---|---|---|---|
| Cache-Aside | App checks cache; on miss queries DB & populates cache | App updates DB, deletes cache key | General read-heavy catalogs (courses, jobs) |
| Write-Through | Cache sits inline | Writes hit cache + DB synchronously | Predictable hot reads, moderate writes |
| Write-Behind | Fast cached reads | Async batch flush to DB later (risk of loss on crash) | View counters, non-critical telemetry |
Strengthen your architecture readiness with SQL Query Optimization or join our Java Backend & HLD Career Program.
Practical Programming Examples
Single-Flight Coalescing & Jittered TTL Pseudo-Implementation (Python)
Coalesce concurrent cache misses for the same hot key so only one DB fetch runs instead of N concurrent DB queries.
import random
def compute_jittered_ttl(base_seconds: int = 300, max_jitter: int = 45) -> int:
return base_seconds + random.randint(0, max_jitter)
cache_store: dict[str, str] = {}
def get_course_summary(slug: str, db_fetch) -> str:
if slug in cache_store:
return cache_store[slug]
fresh = db_fetch(slug) # Acquire per-key lock / single-flight in multi-worker prod
cache_store[slug] = fresh
return fresh
print("Sample jittered TTL (s):", 300 <= compute_jittered_ttl() <= 345)
Sample jittered TTL (s): True
- Adding random jitter spreads expirations smoothly over 45 seconds instead of synchronized spikes.
- Cache-Aside deletes stale keys cleanly on write updates before readers warm the next snapshot.
Common Mistakes to Avoid
- Caching strongly consistent financial wallet balances inside stale TTL caches.
- Allowing non-existent IDs (`/courses/nonexistent-999`) to bypass caching repeatedly (Cache Penetration — cache negative misses briefly).
Production Best Practices
- Always pair Redis caching with `allkeys-lru` / `volatile-lfu` maxmemory policies and single-flight guards on hot keys.
Technical Interview Relevance
Classic SDE-1/SDE-2 System Design round deep-dive (bookmyshow, flipkart catalog, URL shortener).
Practice & Self-Check Questions
When modifying a record in Cache-Aside, should you delete the cache key or overwrite it with the new value?
Deleting (invalidating) the key is usually safer than overwriting under concurrent updates, preventing slower older writers from overwriting newer values inside Redis.