Highlights

Contents

Part I
01

Why cache, and where

We start with what a cache buys and what it costs, measured in latency and in database load. The numbers explain why a small change in hit ratio can decide whether a database survives the dinner rush. We will cover hit ratio arithmetic, the layers of caching along a request’s path, and the choice between local and distributed caches.

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 1What a cache buys

Wren’s menu endpoint reads the same data again and again. Restaurant 9’s menu changes a few times a week and is read thousands of times an hour. Loading it from PostgreSQL costs two queries of 4 ms each, plus connection time from the pool. Loading it from Redis costs one round trip of about 0.5 ms. A cache is a store that keeps a copy of data somewhere faster or closer than its source, so repeated reads can skip the slow trip.

The fraction of reads the cache answers is the hit ratio. A read the cache answers is a hit, and one it cannot answer is a miss, which goes to the source and usually puts the result in the cache for next time. With a 90% hit ratio, average latency is a weighted mix.

tavg=h⋅thit+(1−h)⋅tmisst_{avg} = h \cdot t_{hit} + (1 - h) \cdot t_{miss}

Read it as “the share of hits times their cost, plus the share of misses times theirs”. With a hit costing 0.5 ms and a miss costing about 9 ms, the GET, two queries and a SET, Wren averages 0.9 × 0.5 + 0.1 × 9 = 1.35 ms. At 99% it would be 0.585 ms, and at 80% it would be 2.2 ms.

A CACHE IS THE FRIDGE; THE DATABASE IS THE SUPERMARKETA CACHE IS THE FRIDGE; THE DATABASE IS THE SUPERMARKETmenuusertipcartranksessRedis GET: 0.5 mstwo queries: 8 mson a miss: walk to the shop90% hits: 0.9 × 0.5 + 0.1 × 9 = 1.35 ms on average, and the database sees one request in ten
Figure 7.1Nine trips in ten end at the fridge. The tenth walks to the supermarket.

The second benefit matters more than latency. The database only sees misses. At 2,000 requests per second and a 90% hit ratio, 200 requests per second miss, each running two queries, so PostgreSQL handles 400 queries per second. Drop to 80% and the database sees 800. Climb to 99% and it sees 40. The database’s load follows the miss ratio, which is why going from 90% to 99% is a tenfold change, not a 9% one.

DATABASE QUERIES PER SECOND AT 2,000 REQUESTS PER SECOND, BY HIT RATIODATABASE QUERIES PER SECOND AT 2,000 REQUESTS PER SECOND, BY HIT RATIOhit ratioqueries/s80%800 q/s90%400 q/s99%40 q/sfrom 90% to 80% doubles the loadthe database feels the miss ratio, so going from 90% to 99% cuts its load tenfold
Figure 7.2Database load against hit ratio. A ten-point drop doubles the queries.

That curve also explains the danger. A database sized for 400 queries per second with a cache in front is a database that cannot survive without the cache. Part V comes back to this with the cache outage.

Caches cost something too. They need memory and servers. They introduce staleness, the window in which the copy differs from the source. They add failure modes of their own. And they make the system harder to reason about, because every read now has two possible sources. Caching is worth it when data is read far more often than it changes, when the source is slow or expensive, and when slightly old data is acceptable. Wren’s menus, restaurant profiles and session lookups fit. Account balances and stock counts usually do not.

#Section 2The layers of caching

A request for restaurant 9’s menu passes through several places that can keep a copy, and each one saves a trip to the next. The user’s browser or app keeps responses according to HTTP caching headers, which Unit I covered. A content delivery network, a CDN, keeps copies at edge locations near users, the subject of Part IX. The API gateway can cache whole responses. Inside each app instance, a local cache in process memory can keep the hottest objects. A distributed cache such as Redis keeps shared copies for all instances. And inside PostgreSQL, the buffer cache keeps recently used pages in memory, as does the operating system’s page cache beneath it.

CACHES ARE NESTED ALONG THE PATH; EACH RING SAVES A TRIP TO THE NEXTCACHES ARE NESTED ALONG THE PATH; EACH RING SAVES A TRIP TO THE NEXTbrowserCDN edgegatewaylocal memoryRedisdatabase buffersdiskfrom the outside in:browser 0 msCDN edge 5 msgateway 20 mslocal memory 0.001 msRedis 0.5 msdatabase buffers 4 mstimes are the extra costof missing the ring above(illustrative)
Figure 7.3Seven rings, from the browser to the disk. Every inner ring is slower to reach and closer to the truth.

The outer layers are the cheapest per hit and the hardest to control. A response cached in a user’s browser costs Wren nothing to serve again, but Wren cannot take it back. If the menu changes, the browser keeps the old copy until its max-age runs out, unless the response told it to revalidate. A copy in Redis can be deleted in a millisecond. The rule follows directly. The further out the cache, the shorter its lifetime should be or the more carefully its keys should be versioned.

Each layer also caches a different thing. The browser and CDN cache whole HTTP responses, keyed by URL and some headers. The gateway can do the same. The application caches objects or fragments, such as a serialized menu or a user’s permissions, keyed however the code likes. The database caches pages of tables and indexes, regardless of which query wanted them. Choosing what to cache at which layer is mostly a question of what is shared between users. A menu is the same for everyone and can live at the CDN. A user’s order history is private and belongs in the application cache, keyed by user.

Layers multiply staleness. If the CDN caches the menu for 60 s and Redis caches it for 300 s, the CDN can refresh from Redis just before Redis expires, and a user can see data up to 360 s old. Part X traces exactly that.

#Section 3Local and distributed caches

A local cache lives inside the application process, as a hash map with a size limit, using a library such as Caffeine for Java, functools.lru_cache or cachetools for Python, or ristretto for Go. A lookup is a memory access, about a microsecond, with no network and no serialization. The weaknesses come from the same fact. Each of Wren’s four instances has its own copy. When the tip on order 123 changes, one instance may hold tip 30 while another still holds 20, and nothing tells them. Each copy uses that instance’s memory, and a restart empties it.

LOCAL CACHES ARE FAST BUT EACH INSTANCE HAS ITS OWN COPYLOCAL CACHES ARE FAST BUT EACH INSTANCE HAS ITS OWN COPYapp 1tip 30app 2tip 20app 3tip 30app 4tip 20Redislocal memoryabout 1 µs, per instance,can disagreeRedis0.5 ms, shared,one copy for everyonetwo-tier: a small local cache with a 1 s TTL in front of Redis for the hottest keys
Figure 7.4Four local caches can disagree. One Redis gives everyone the same copy, half a millisecond away.

A distributed cache runs as a separate server that all instances share. Memcached, created by Brad Fitzpatrick for LiveJournal in 2003, popularized the idea, and Redis, released by Salvatore Sanfilippo in 2009, became the most common choice. Every instance sees the same copy, deleting a key invalidates it for everyone, and the cache survives app restarts. The price is a network round trip per lookup, serialization of every value, and one more server to run.

Most busy systems use both, a pattern called a two-tier cache. Wren keeps a small local cache, 10,000 entries with a 1 s TTL, for the very hottest keys such as the menus of the top restaurants during a promotion. Everything else goes to Redis. The 1 s limit bounds how long instances can disagree. When restaurant 9 changes its menu, users may see the old one for up to a second on some instances, which Wren accepts for menus and would never accept for an order’s payment status.

Local caches have one more use that is easy to miss. They protect Redis itself from hot keys, which Part V explains. If 50,000 requests per second read one key, a 1 s local cache on 20 instances turns that into 20 Redis reads per second.