Try
↑ ↓ move↵ openesc close
In this lesson · topic 6 of 7
  1. 1. Why a small cache works at all
  2. 2. FIFO, LRU, LFU, and TinyLFU
  3. 3. Move the hot keys under four policies
  4. 4. Why plain LFU stays stuck after a shift
  5. 5. How big a cache do you need
  6. 6. When a hot key expires
  7. 7. Check the eviction rules

Topic 6

When a hot key expires

7 min read

Eviction policies decide what leaves when the cache is full. A second, separate way for a key to leave is expiry: you gave it a time-to-live, and the time ran out. Expiry of a hot key is one of the most common ways a healthy cache takes a database down.

Counting the stampede

A product page is read 20,000 times a second and cached for 60 seconds. Recomputing it takes a 200 ms database query. At the moment the entry expires, every reader sees a miss. Until the first recompute finishes and refills the cache, which takes 200 ms, every request misses:

20,000 requests per second × 0.2 seconds = 4,000 identical queries

The database was sized for a trickle of misses, not 4,000 at once. Under that load each query takes longer than 200 ms. Say it takes a full second. Now the window lasts a second, and 20,000 more requests miss and start their own query. The slowdown lengthens the window, and the longer window adds more load. This feedback is what turns a short spike into an outage.

The same shape appears after a cache restart or a deploy that empties it, because every hot key is missing at the same moment.

Fixes, from cheapest to most careful

Coalesce the misses. Keep a table of in-flight recomputes in each process. The first request for a key starts the query, and later ones wait for its result. With 40 application servers this reduces 4,000 queries to 40.

Hand out a lease. The cache itself gives one client a token to refill the key, and tells the others to wait briefly and try again. This gets the count down to 1, across all servers. The memcache deployment at Facebook added leases for this and also limits how fast tokens are handed out for a key.

Serve stale while refreshing. Keep the old value for a grace period after expiry. The first reader to see the expired entry refreshes it in the background, and everyone else gets the old value at full speed. HTTP caches have a standard form of this, the stale-while-revalidate directive. It trades a bounded amount of staleness for no cliff.

Add jitter. If a deploy fills 10,000 keys with a 60-second lifetime in the same second, they all expire together. Choosing each lifetime at random between 54 and 66 seconds spreads the expiries over 12 seconds.

Refresh early, at random. Each reader decides on its own whether to refresh before expiry. A published rule recomputes when now − δ × β × ln(rand) ≥ expiry, where δ is how long the recompute takes, β is a tuning constant near 1, and rand is uniform on (0, 1]. Because ln(rand) is negative, the check fires a little before expiry. Fires are rare far from expiry and likelier as it nears. On a hot key some reader almost always refreshes in time, and no coordination is needed.

Predict first

A hot key expires. Forty application servers each coalesce misses inside their own process, with no shared lease. About how many database queries run for that expiry?

Sources

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms