System Design: Caching Strategy
Where to cache, how to invalidate, and consistency trade-offs — with an interactive simulator for hit ratio, latency, and multi-layer caching.
Introduction
Caching is not a default — it is a deliberate trade-off between speed, freshness, and complexity. Cache hit ratio, invalidation logic, and consistency boundaries are what separate a fast system from a stale one.
← Back to System Design overview
When to cache
| Data type | Cache? | Why |
|---|---|---|
| Product catalog | Yes (CDN + Redis) | Read-heavy, tolerates short staleness |
| User session | Yes (Redis) | Fast lookup, TTL-based expiry |
| Account balance | Careful | Strong consistency often required |
| Search results | Sometimes | Short TTL, invalidate on write |
Cache layers
Client → CDN → Load Balancer → API → Redis → DatabaseEach layer has a different TTL and invalidation strategy:
- CDN — static assets, public GET responses
- Application cache (Redis) — computed aggregates, session, rate-limit counters
- Database query cache — often replaced by Redis in modern stacks
Interactive simulator
Pick a strategy and simulate requests. Watch hit ratio and latency change.
Cache Strategy Simulator
Not every layer needs a cache — pick the right one and measure hit ratio.
Hit ratio
78%
Avg latency
48ms
Cache hits
0
Hot keys stored in memory with TTL invalidation.
Path: Client → API → Redis → Database · Misses: 0
Invalidation strategies
TTL (time-to-live)
Simplest: data expires after N seconds. Good for catalog pages where 30–60s staleness is acceptable.
Write-through / write-behind
Update cache when the database writes. Keeps cache warm but adds write latency.
Cache-aside
App reads cache → on miss, read DB → populate cache. Most common pattern:
async function getProduct(id: string) {
const cached = await redis.get(`product:${id}`)
if (cached) return JSON.parse(cached)
const product = await db.products.findById(id)
await redis.set(`product:${id}`, JSON.stringify(product), 'EX', 300)
return product
}Consistency trade-offs
| Model | Behavior | Use when |
|---|---|---|
| Strong | Always fresh | Payments, inventory locks |
| Eventual | May be stale briefly | Feeds, analytics, catalog |
| Read-your-writes | User sees own updates | Profile, settings |
Subscribe to my newsletter
Stay up to date and get notified when I share new contents.
No spam ever, unsubscribe anytime