When the Cache Hurts
A cache that works changes what the store is built for. At a 99 percent hit rate, the store sees 200 of 20,000 reads a second, and it is sized for 200. Every failure in this lesson is a moment when that number jumps: a hot key expires on a hundred servers at once, one key outgrows its node, the requests ask for keys that do not exist, or the whole tier comes up empty.
The previous lesson took the loss of one cache node. This one takes the rest, counted across a fleet of servers rather than inside one service, which is where Backend Deep Dive's caching chapter leaves off.
The Store Behind the Cache
The store's headroom is a multiple of its miss load, not of the traffic. Assume the store takes 200 reads a second on a good day and could take 600, three times its load. That sounds safe. A cold tier sends it all 20,000, a hundred times what it normally sees.
The overload then feeds itself. An overloaded store answers slowly, so the cache refills slowly, so the misses stay high after whatever caused them has gone. Waiting does not end it. Recovery needs the load cut: fewer reads let through to the store until the cache is warm again.
Stampedes Across a Fleet
A hot key expires, and every server reading it misses in the same instant. Assume 100 app servers. Coalescing inside each server, one fetch per key per process while the other requests wait for it, still sends up to 100 store queries for that one key at once: one from every server.
A lease taken in the cache tier brings it down to one. The first server to miss takes the lease and refreshes the key, and the other 99 serve the old value, kept a few seconds past its expiry for this purpose, or wait for the new one. Two habits remove most of these misses before they happen: refresh a key known to be hot before it expires, and add a little randomness, jitter, to expiry times, so that keys set together do not expire together. One service's lock, early refresh and jitter, with code, are in Backend Deep Dive's lesson on invalidation and the stampede. This section owns the count across the fleet.
Hot Keys
One key always lands on one node, as the previous lesson set up, so more nodes spread keys and never one key's reads. Assume a key read 200,000 times a second, about twice what one cache node serves. No number of nodes fixes that, because every one of those reads goes to the node that owns the key.
The first fix is a local tier. Each app server keeps the key in its own memory for one second. With 100 servers, the node then sees about 100 reads a second for the key, one refresh per server, instead of 200,000. The cost is a second of staleness that no invalidation reaches, because nothing tells a hundred processes to drop their copies.
The second fix copies the key onto several cache nodes under suffixed names, the key with a number added, so its reads spread over those nodes. Every change to the key then costs one delete per copy. Either fix needs the key found first: sampled from each node's busiest keys, or known ahead, such as a launch or a celebrity's post. A hot partition in the store, rather than in the cache, is Chapter 3's.
A local hot-key cache keeps the key in each app server's memory for about a second. There is no network hop and almost every read of the key is absorbed, but every server may serve the old value for that second, out of reach of any invalidation. Use it when a second of staleness is acceptable for that key.
A replicated hot key spreads copies of the key over several cache nodes. Invalidation still reaches every copy, at one delete per copy for each change, and every read still crosses the network. Use it when the key must be right just after a write and one node's rate is the only problem.
Keys That Do Not Exist
A key that does not exist misses every time, because there is nothing to cache. One missing key asked for again and again is stopped by negative caching: the cache stores "not found" for that key, with an expiry short enough that a key created a moment later still appears. The one-service version is in Backend Deep Dive's lesson on caches that lie.
Random keys are a different problem. Take the URL shortener of Chapter 7: 18 billion links in a space of 3.5 trillion seven-character keys, so about one random key in 200 exists. Assume a scanner trying 1,000 random keys a second: it adds about 1,000 misses to the store's 200. That is six times its usual load, from one client, and negative caching does nothing, because every random key is new.
The fixes sit in front of the store. A filter that can answer "definitely absent", a Bloom filter, turns most of the scan away, for the cost of memory and an update on every create; how it works is in Computer Science Deep Dive's lesson on probabilistic structures. A check character, which a random string almost always fails, costs one more character per key. And a limit per client, which is Chapter 11's subject, stops the scanner itself.
A Cold Tier
The whole tier can come up empty: after a flush, on a new cluster, or after a failover to another region. Bring it into service in order. First, warm it from the hottest keys before it takes any traffic. Then move traffic onto it in steps whose misses fit the store's headroom. A store that carries 200 reads a second and could carry 600 has room for about 400 more, and each step stays inside that.
Throughout, cap the misses the store accepts, and answer the rest with a stale value or a fast refusal; shedding load is Chapter 6's. And never flush a cache to invalidate it. Bump a version in the keys instead, so that new reads use new keys and the old entries age out, as Backend Deep Dive does for one service.
How Interviewers Probe This
Start every answer here from the number the cache was built around: what reaches the store first, and the fix second. You might say: "My store is sized for about 200 misses a second, so first I want to know what sends it more."
The first probe is "Your most popular key expires. What happens in the next second?" A good answer counts the servers that miss and gives the store's queries two ways: one per server with coalescing, and one in all with a lease. It serves the old value meanwhile, and it refreshes the hottest keys before they expire, so the question does not arise.
The second is "Someone requests millions of short links that do not exist." A good answer works out that nearly every random key misses, says that negative caching does not stop a scan, and picks a filter or a limit per client, with its cost.
The third is "You have to replace the whole cache cluster tonight. How?" A good answer gives the store's headroom as a number, warms the new cluster from the hot keys, moves traffic in steps that fit that headroom, and caps the misses.
- "Coalescing solves the stampede." It does inside one server. Across 100 servers it still sends up to 100 queries for one key at once.
- "More cache nodes fix a hot key." Nodes spread keys, and every read of one key still lands on the node that owns it.
- "Negative caching stops a key scan." Each random key is new and misses once. Negative caching stops repeats, not scans.
- "A flushed cache warms itself." Only while the store can carry the misses. An overloaded store refills the cache slowly, and the tier can stay cold after the cause has gone.
- "A longer expiry prevents stampedes." It makes them rarer, not smaller, and it leaves every entry staler.
- State the store's headroom as a multiple of its miss load, and name the failure that would exceed it.
- Coalesce misses in every server, and give each known hot key one refresher across the fleet.
- Put a local tier with a short expiry in front of any key near one node's rate, and say that expiry aloud.
- Warm a new or flushed tier before it takes traffic, move traffic onto it in steps, and cap the misses.
Knowledge Check
A hot key expires on 100 app servers, and each server coalesces its own misses. How many store queries for that key arrive at once, and how many with a lease in the cache tier?
- Up to 100 with coalescing alone, and a single one with a lease in the cache tier
- One with coalescing alone, since coalescing already merges the misses of the fleet
- Up to 100 either way, since a lease only puts the same queries in order over time
- None either way, since coalescing has every server keep serving the old value
One key takes 200,000 reads a second. Why does doubling the number of cache nodes not help it?
- Every read of one key still goes to the one node that owns it
- The new nodes start cold, so the key misses until they are warm
- More nodes raise the hit rate, which sends more reads to the key
- Each new node needs a copy of the key, so writes to it get slower
Each app server keeps a hot key in its own memory for one second. What does that cost?
- Up to a second in which each server serves a value no invalidation can reach
- One network hop on every read of the key, to check that it is still current
- A delete sent to every app server each time the key changes, a hundred a write
- Twice the memory in the cache tier, because the key is now held in both places
A scanner requests 1,000 random short keys a second. Why does negative caching not protect the store?
- Each random key is new, so it misses once before anything is cached
- Negative entries expire too fast to help against any repeated key
- The cache will not store an absent key unless a filter is in front
- Negative caching works only for keys that existed once and then went
The whole cache cluster is replaced tonight. Which order brings the new one into service safely?
- Warm it from the hot keys, move traffic in steps, and cap the misses
- Move all traffic at once, then let the misses warm the new cluster
- Flush the old cluster first, so that no stale entry survives the move
- Double the store's replicas, move all traffic, remove them next week
You got correct