SDE & MLE Prep

Caching

Where: client → CDN → API gateway → application cache (Redis/Memcached) → DB buffer pool.

StrategyHowTrade-off
Cache-aside (lazy)app reads cache, on miss reads DB and fillssimple, most common; first read slow, possible stale
Read-throughcache library loads from DB on misscleaner app code
Write-throughwrite cache + DB synchronouslyconsistent; slower writes
Write-backwrite cache, flush to DB asyncfast writes; risk of loss
Write-aroundwrite DB only, cache fills on readavoids polluting cache
  • Eviction: LRU (default), LFU, TTL.
  • Pitfalls: thundering herd / cache stampede (use request coalescing, locks, or probabilistic early refresh), hot keys (replicate the key, local in-process cache), invalidation (delete-on-write + TTL as a safety net), cache penetration (cache negative results, Bloom filter).
read(k):  v = cache.get(k)
          if v is None: v = db.get(k); cache.set(k, v, ttl)
write(k): db.put(k, v); cache.delete(k)      # delete, don't update -> avoids races