System design · Question 44 of 50

How do you design caching? Cache-aside, invalidation, stampedes.

What they're checking

Practical caching, and its traps.

The answer

Cache what is read often and changes rarely, as close to the user as you can: the browser and a CDN for static files, Redis for computed results and hot rows. Cache-aside is the common pattern: read the cache; on a miss, read the database and store the result with a TTL. On writes, update the database, then delete the cache key (deleting is safer than updating it, which can race).

Watch for a stampede: when a hot key expires, thousands of requests miss at once and hit the database. Let one request refill it behind a lock, serve slightly stale data while refreshing, or add random jitter to the TTLs. Decide the eviction policy (LRU), and what a cache outage does to the database.

The picture

read:   app ─▶ cache? ─ hit ─▶ return
                  │ miss
                  ▼
               database ─▶ store in the cache (TTL 5 min) ─▶ return
write:  database ✓ ─▶ delete the cache key
The follow-up they'll ask

Write-through or write-back?

Show the answer

Write-through writes the cache and the database together: consistent, slower writes. Write-back writes the cache now and the database later: fast, but data can be lost.

Your progress stays on this device.