Skip to main content
Cachingchapter 5 of 8 · 7 lessons

Cache Eviction & Stampede

What gets removed when cache is full, and how to handle mass expiry.

Your cache holds 8 gigabytes and your catalogue is 40. It has been full since Tuesday, so every new entry pushes an old one out.

Which one leaves is a rule you chose, possibly without reading it. Choose badly and the overnight job that walks all 200,000 products evicts the 300 that people actually buy, and your morning traffic arrives to an empty cache.

Then there is the other failure, the one that happens in a single second. One popular key expires, the 5,000 requests a second that were being served from memory all miss at once, and every one of them goes to the database together.

That database was comfortable a moment ago and is now taking 5,000 copies of the same query. It has a name worth knowing before you meet it at 3am.

Lessons

7 in this chapter
  1. Eviction PoliciesLRU bets on recency, LFU bets on frequency, and TTL is not an eviction policy at all.2 min
  2. LFU: Counting Instead of TimingEvict what is asked for least, not what was asked for longest ago, and survive the batch job that ruins LRU.2 min
  3. FIFO, Random, and Why Nobody Picks ThemTwo policies that are simpler than LRU, worse than LRU, and still worth being able to dismiss.2 min
  4. The Stampede ProblemWhen a hot key expires, every waiting request charges the database at once.2 min
  5. Locks, Jitter, and Early RefreshLet one request do the work, stagger the expiries, or refresh before the deadline.3 min
  6. Hot KeysOne key taking more traffic than a whole server can serve, and no amount of sharding helps.2 min
  7. Negative CachingCache the fact that something does not exist, or misses for missing data will never end.3 min