System Design

From 250ms to 0ms: how requests short-circuit through 4 cache layers

TL;DR

Systems usually have several cache layers: the browser, a CDN, Redis, then the database. A request falls from the top and stops at the first layer holding the data. The first (all-miss) trip is the most expensive, but on the way back each layer stores a copy for next time.

Three trips

TripAnswered byTime
1 — cold cacheDatabase (missed all 3 caches)~250ms
2Browser~0ms
3 — upper-layer TTL expiredRedis~80ms

How to put it to work

  • Stop requests as high as possible: deeper layers cost more; cache static or slow-changing content in the browser/CDN (Cache-Control, ETag).
  • Set TTLs by how stale each data type can be — product prices differ from avatars.
  • Plan invalidation for when data changes (delete Redis keys, purge the CDN) instead of only waiting for TTLs.
  • Protect private data: never let a CDN cache responses with personal information (Cache-Control: private).
  • Track hit rate per layer; a low hit rate means the cache costs money without helping.