System Design Visualized
Module C3 · about 45 minutes

Caching in Depth

"Add a cache" is easy to say. Interviewers then ask the hard questions: what do you cache, how does it stay correct when data changes, and what happens when a hot key expires? This chapter answers them.

Start the interactive chapter
  1. C3.1Caching strategiesStrategy Read path Write path Trade-off Cache-aside (lazy loading) App checks cache; on a miss reads the DB and fills the cache App writes…
  2. C3.2Eviction policiesCache memory is limited, so something must go when it's full: LRU (least recently used): evict what hasn't been touched for the longest.…
  3. C3.3Invalidation and the stale-cache race"There are only two hard things in computer science: cache invalidation and naming things." Here's why invalidation is hard. With…
  4. C3.4Cache stampede (thundering herd)A hot key (the homepage, a celebrity profile) expires. In the next millisecond, thousands of requests miss at once and all hit the database…
  5. C3.5Where to cache, and how much it helpsCaches sit at every layer, closest to the user first: Browser / app cache (HTTP Cache-Control, ETag → 304 Not Modified). CDN for static…