Highlights

Contents

Part X
10

The menu request end to end

We follow one menu request through every cache layer and then break the layers one by one. A layered cache is only as good as the weakest agreement between its layers. We will cover the request’s path with timings, staleness that adds up across layers, the Friday night Redis failure, and the whole unit on one page.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Why cache, and where2Caching patterns3Expiry, eviction andsizing4Invalidation andconsistency5Cache failure modes6Redis data structures7Redis internals8Redis at scale9HTTP caching and CDNs10The menu request end toend

#Section 32GET /restaurants/9/menu through every layer

User 42 opens restaurant 9 in the app at 8 p.m. The app checks its own HTTP cache first. The last response arrived 40 s ago with Cache-Control: max-age=60, so the app shows it without any network request. A minute later the copy is too old, and the app sends GET /restaurants/9/menu.

The request reaches the nearest CDN edge in about 5 ms. The edge holds a copy from 20 s ago with s-maxage=60, a hit, and it answers. At this layer roughly 95% of menu requests end. A request that misses at the edge goes to the origin shield and then to Wren’s gateway, about 20 ms into the request, where it is authenticated, rate-limited and routed. The menu service checks its local cache, which holds the menus of the 1,000 hottest restaurants for 1 s, and restaurant 9 is among them during its promotion. If not, it asks Redis for menu:9:v42, the versioned key from Part IV, at about 20.5 ms. Only if Redis misses does the service run its two queries against PostgreSQL, about 28.5 ms in, then fill Redis with a jittered TTL near 300 s and respond with headers that let the edge and the app cache the result.

GET /restaurants/9/menu: WHERE EACH LAYER CAN ANSWERGET /restaurants/9/menu: WHERE EACH LAYER CAN ANSWER0 msbrowsermax-age 605 msCDN edgehit 95%20 msgatewayauth, route20.001 mslocal cache1 s, hot keys20.5 msRedismenu:9:v4228.5 msPostgreSQL2 queriescumulative time to reach each layer, illustrativea CDN hit answers in 5 ms; a Redis hit in about 21 ms; a full miss in about 29 mseach layer has its own TTL and its own invalidation, and they must agree
Figure 7.54Six places that can answer. Most requests end at the first or second.

The numbers are illustrative, but the shape is real. Each layer’s hit ratio multiplies, so even modest ratios compound. If 50% of opens are served by the app’s own cache, 95% of the rest by the CDN, 30% of what reaches the service by the local cache and 90% of the remainder by Redis, then only 0.5 × 0.05 × 0.7 × 0.1 = 0.175% of menu opens reach PostgreSQL.

Every layer also adds staleness. The app may show a copy 60 s old. The edge may have fetched that copy when it was nearly 60 s old itself. The local cache adds up to 1 s, and Redis up to its TTL if invalidation fails. Wren’s menus use versioned keys and change events, so Redis and the local cache are normally fresh within a second, and the CDN is purged by surrogate key. The worst case, when every invalidation fails, is the sum of the TTLs, about 60 + 60 + 1 + 300 = 421 s. The staleness budget for menus is 5 minutes, so Wren shortens the Redis TTL to 180 s, which brings the worst case to 301 s. Writing down that sum is how layered caches are kept honest.

#Section 33Friday night failures, and the unit on one page

At 8:05 p.m. the Redis primary fails during restaurant 9’s promotion. Each design decision in this unit now decides an outcome. Redis calls time out after 50 ms instead of seconds, and after a handful of failures the circuit breaker opens, so requests stop waiting at all. The local cache keeps serving the hottest menus, including restaurant 9’s. Misses go to PostgreSQL, and request coalescing ensures that each menu is built once per instance rather than once per request. The database pool and statement timeouts from Unit VI keep PostgreSQL from drowning, and the few requests that cannot be served get 503 with Retry-After. The CDN, configured with stale-if-error, keeps serving cached menus even for requests that would have reached the origin. Within about 20 s, Sentinel promotes a replica, the circuit breaker closes after its probe succeeds, and the hit ratio is back near 90% because the replica held a copy of the data.

The same evening exercises the other failure modes. When the promotion’s banner goes live, a key format change in the deploy could have caused an avalanche, but the new format was introduced alongside the old one and ramped. When restaurant 9’s menu key expires at peak, early refresh rebuilt it 1.2 s earlier and nobody noticed. A scraper requesting random order ids is turned away by the Bloom filter and the edge’s bot rules. Nothing here is luck. Each failure was named, and each has a cure built in before the evening started.

THE UNIT ON ONE PAGETHE UNIT ON ONE PAGEmenuusersessrankrllockRedis structurespatternsaside, through, behind, aroundexpiryTTL, jitter, LRU, LFUinvalidationdelete, version, eventsfailuresstampede, penetration,avalanche, hot and big keysscalepersistence, Sentinel, ClusteredgeCDN keys, purges, shieldsevery cache is a copy; every design question is how old a copy may be and who pays when it is missing
Figure 7.55Patterns, expiry, invalidation, failures, Redis at scale and the edge, around one idea.

Put together, the unit is one idea seen from many sides. A cache is a copy, and every copy raises the same questions. Who fills it, who updates it and in what order, how old may it get, what happens when it is missing, and what happens when many requests notice that at the same moment? Redis answers many of them with fast data structures, an atomic single thread and configurable durability, and CDNs answer them at the edge of the network. Unit VIII turns to queues and events, which share the same change streams this unit used for invalidation.