Stage 2 · Building blocks · Medium · 16 min
Cache eviction and stampedes
A cache has limited memory. Learn which key it removes when full (LRU, LFU), and how one expired key, or many, can flood the database, with the fix for each.
What each interview level is expected to know
Learn first: Caching strategies
In AI systems: Caching for AI features
After this lesson you can:
- Explain how LRU and LFU each choose what to remove from a full cache.
- Protect the database when one hot key expires, with a lock so only one request rebuilds it.
- Tell a stampede, an avalanche and penetration apart, and give a defence for each.
Start here
See it in 60 seconds
When a popular key expires
When a popular cached key expires, every waiting request can hit the database at once; letting one request refill it protects the database.
Scene 1 of 6 · A popular key
Readers keep asking for item:1, and the cache answers every one, so the database stays quiet.
- Readers — healthy
- App — healthy
- Cache — healthy — item:1 cached
- Database — healthy
- Requests waiting for item:1
- not shown yet
- Database reads of item:1
- not shown yet
- A popular key. Readers keep asking for item:1, and the cache answers every one, so the database stays quiet.
- The copy expires. Then the TTL for item:1 runs out, and the cache throws its copy away.
- Everyone misses at once. 100 requests for item:1 arrive in the same moment, and every one of them misses the cache.
- A stampede. Each miss reads the database itself: 100 reads of the same key at once, a cache stampede.
- One read refills it. With request coalescing, the first miss reads the database and the other 99 wait for its answer.
- Everyone gets the answer. That one read refills the cache, and all 100 waiting requests get the same fresh copy.
- Now you try. How many requests are waiting for item:1 when it expires? For example: 300 waiting requests: without coalescing, 300 database reads at once; with it, 1. The other 299 wait for that one read, then all get the same fresh copy.
Scene 1 of 6: A popular key. Readers keep asking for item:1, and the cache answers every one, so the database stays quiet.
Key points
- A cache stampede happens when a popular key expires and every request for it misses at once.
- Each miss reads the database, so one expiry can send hundreds of identical reads together.
- Request coalescing lets one request per app server refill the key while the others wait.
- If many keys share one TTL, they expire together; a few random extra seconds on each TTL spreads them out.
In plain words
Say item:1 is the most-read key in a shop's (A small, fast store that keeps copies of popular data, so most reads skip the slower database.). Its (How long a cached copy is kept before it is thrown away.) runs out, and the cache drops it. The next 100 requests for item:1 all miss, and all 100 read the database at the same moment. That burst is a (A burst of requests that all miss the cache for the same key at once and hit the database together.). (Removing an item from a full cache to make room for a new one.) is the other half of this lesson. When the cache is full, it must remove a key to make room, and the rule it uses decides which one.
Each problem has a common fix. For eviction, (An eviction rule that removes the item nobody has asked for in the longest time.) removes the key nobody has read for the longest time, which suits many apps. For a stampede, the first miss takes a short lock and reloads item:1 while the other 99 wait for it. When many keys expire at the same moment, called an avalanche, a small random extra on each TTL spreads them out.
The problem it solves
A cache cannot hold everything, so something must go, and a poor choice lowers the (The share of reads the cache answers by itself, without asking the database.). The database behind it is sized for the few reads that miss. So anything that makes many reads miss together can overload it. That can be one busy key expiring, many keys expiring at once, or requests for keys that do not exist.
Why it matters
A cache protects the database only while the right keys are in it. Memory is limited, so a full cache must choose a key to remove. When a busy key goes missing, every request for it reaches the database at once. That is how a cache problem becomes a database outage.
A worked example
Suppose a popular key gets 5,000 reads a second, then expires, and recomputing it from the database takes 200 ms. Every request that arrives during those 200 ms misses the cache. About 5,000 × 0.2 = 1,000 requests hit the database at the same time, instead of one request recomputing the value.
Think of it like…
Think of a shared office fridge with room for 20 lunches. When it is full, the lunch that has sat untouched the longest is taken out to make room.
Where this analogy stops working
Removing what nobody has used lately is the useful part. But a fridge shows nothing like a stampede. When a busy key expires, every request for it misses together. All of them then ask the database for the same value at once.
Try it
See it working
Mechanics
How it works
A full cache has to remove a key, and a missing key sends its reads to the database. This page shows both on one small shop: an app, a cache and a database.
First comes which key goes when the cache is full. Then come the three ways that many reads can miss at once: one busy key, many keys together, and keys that do not exist.
When the cache is full: LRU or LFU
Pick a case to follow belowWith this case: LRU removed A, the most-read key, because B and C were read after it. The next read of A missed. 5 database reads in all.
App, the shop App the shop Cache, room for 3 keys, NOW Cache room for 3 keys A NOW Database, the real record Database the real record - App — the shop — healthy
- Cache — room for 3 keys — healthy — NOW — A
- Database — the real record — healthy
- Read
- Read that misses
Step 1 of 4Step 1 of 4 · Cache
Done: A is read three times
- What happens
- The first read misses, so the app loads A from the database. The next two reads are hits.
- Why
- A is the popular key here, the one most worth keeping.
- What changes
- The cache holds A, read 3 times.
- Next
- B and C are read once each
In the cacheA1 of 3 keysReads of A3A is cachedDatabase reads1one for each missStep 1 of 4: Cache. A is read three times. The first read misses, so the app loads A from the database. The next two reads are hits.
When the cache is full, it must remove (evict) a key to make room. The rule it uses decides which one. In the picture, the cache has room for 3 keys. Switch between the two rules.
LRU (least recently used) removes the key that nobody has read for the longest time. It keeps the keys in order of last use, so finding that key takes one step. In the first case A goes, even though A was read 3 times, because B and C were read after it.
(An eviction rule that removes the item asked for the fewest times.) (least frequently used) counts the reads of each key and removes the key with the fewest. In the second case B goes, and the next read of A is a hit. LFU protects keys that stay popular, but it is slow to notice when an old favourite goes cold.
LRU is the common choice, because it is cheap and suits most traffic. (A widely used data store that keeps its data in memory, so it answers much faster than a database on disk. It is often used as a cache.) lets you pick: its settings include LRU and LFU rules. By default it evicts nothing and refuses new writes when its memory is full.
Cache stampede (thundering herd)
Pick a case to follow belowWith this case: With no lock, 5 requests made 5 database reads for one value. At 5,000 requests a second, about 1,000 arrive in 200 ms, and each one reads the database.
App, the shop App the shop Cache, room for 3 keys, NOW Cache room for 3 keys item:1 expired NOW Database, the real record Database the real record - App — the shop — healthy
- Cache — room for 3 keys — healthy — NOW — item:1 expired
- Database — the real record — healthy
- Read that misses
- Value
Step 1 of 4Step 1 of 4 · Cache
Watch: item:1 expires
- What happens
- Its TTL runs out, and the cache drops it. Rebuilding it takes one database read of 200 ms.
- Why
- item:1 is the busiest key, so many requests ask for it every second.
- What changes
- The next read of item:1 will miss.
- Next
- Request 1 misses and reads the database
Database reads for item:10one rebuildRequests waiting0none: each reads for itselfStep 1 of 4: Cache. item:1 expires. Its TTL runs out, and the cache drops it. Rebuilding it takes one database read of 200 ms.
A busy key expires, and every request for it misses until one database read brings it back. In the picture, 5 requests arrive during the 200 ms that this read takes.
With no lock, each miss starts its own database read. All 5 read the same value, so 4 of those reads are wasted. Now say the key gets 5,000 reads a second. About 1,000 requests miss in those 200 ms, and every one of them queries the database.
The fix is a lock per key. The first request that misses takes the lock and reads the database. The others wait, then read the new copy from the cache. This is also called (Combining several requests for the same missing key into one database read, so the rest wait for its answer.). In the second case, 5 requests cost 1 database read.
Another fix is to refresh a busy key a little before it expires, so readers never find it missing. The caching strategies page shows this as refresh-ahead.
Cache avalanche
Pick a case to follow belowWith this case: With the same TTL, all 4 keys expired at second 60, and the database got 4 reads at once. With thousands of keys, that is an avalanche.
App, the shop App the shop Cache, room for 3 keys, NOW Cache room for 3 keys 4 keys cached NOW Database, the real record Database the real record - App — the shop — healthy
- Cache — room for 3 keys — healthy — NOW — 4 keys cached
- Database — the real record — healthy
- Read
- Read that misses
Step 1 of 2Step 1 of 2 · Cache
Done: Four keys are loaded in the same second
- What happens
- At second 0 the app caches 4 keys, each with the same TTL of 60 s.
- Why
- Keys loaded together, such as after a restart or a nightly job, all start their TTL together.
- What changes
- All 4 keys will expire at second 60.
- Next
- At second 60, all 4 keys expire together
Clock0sKeys still cached4of 4Most database reads in one second0one at a timeStep 1 of 2: Cache. Four keys are loaded in the same second. At second 0 the app caches 4 keys, each with the same TTL of 60 s.
What these pictures simplify. These pictures use a cache with room for 3 keys, 5 requests and 4 keys, with stated times. A real cache holds millions of keys and gets thousands of reads a second. The random extras are stated examples. The Caching lab above runs many requests at once.
An avalanche is many keys expiring in the same second. In the picture, 4 keys are loaded together at second 0, each with a TTL of 60 s.
Keys loaded together, such as after a restart or by a nightly job, start their TTL together. With the same TTL they also run out together, and their reads reach the database at once. With thousands of keys, that can overload it.
The fix is a random extra on each TTL, such as 0 to 30 s. In the second case the 4 keys expire at seconds 67, 73, 82 and 88, so the database gets one read at a time.
Cache penetration
Penetration is a flood of requests for keys that do not exist, such as item:-1. None of them is ever cached, so every request reaches the database.
One fix is to cache the "not found" answer for a short time, so a repeat asks the cache and not the database. Another is to check a (A small row of bits that says an item is definitely not in a set, or probably is. It never says no to an item that was added.) first. It can say for sure that a key does not exist, so those requests stop before the database.
| Problem | What causes it | The usual fix |
|---|---|---|
| Stampede | One busy key expires | One lock per key, or refresh the key early |
| Avalanche | Many keys expire in the same second | A random extra on each TTL |
| Penetration | Requests for keys that do not exist | Cache the "not found" answer, or check a Bloom filter |
| Cold start | The cache restarts empty | Warm it up before it takes traffic |
Use it when
- Setting a memory limit on a cache such as Redis or Memcached: you must also pick the rule it uses to evict keys.
- A busy key that is slow to rebuild: add a lock or an early refresh. Then one expiry does not send every reader to the database.
- Keys that users can make up, such as /item/999999: cache the "not found" answer or check a Bloom filter first.
Skip it when
- A data set small enough to fit in memory with room to spare: nothing has to be evicted, so the rule does not matter.
- A key that is cheap to rebuild and rarely read: a stampede on it is a handful of fast queries, so a lock adds nothing.
Check yourself
Judgement
Trade-offs and how it fails
What you give up
- Memory
- Exact LRU keeps every key in a list ordered by last use, which costs two extra pointers for each key. Redis saves that memory: it samples a few keys and evicts the oldest of the sample, which is close to LRU but not exact.
- Concurrency
- A lock per key stops a stampede, but every other request for that key waits while one rebuilds it. If the rebuild is slow or fails, they all wait or fail with it, so the lock needs a (The time a caller waits before giving up on a call and treating it as failed.).
Failure modes
- Avalanche: every key was loaded at midnight with the same 24-hour TTL. They all expire together the next midnight, and the database gets every read at once.
- Penetration: an attacker asks for ids that do not exist, such as -1 or abc. None is in the cache, so every request runs a database query.
- A cold start: after a restart the cache is empty, so the database takes all the reads until it fills again.
In the real world
- Facebook's paper "Scaling Memcache at Facebook" (2013) describes leases, which let one client refill a missing key while others wait. Source: USENIX: Scaling Memcache at Facebook (2013) (checked 14 October 2026)
- Redis's documentation lists eviction policies such as allkeys-lru and allkeys-lfu. In open-source Redis the default is noeviction, which removes nothing and refuses new writes when memory is full. Managed services may set another default.
Trade-offs to be ready to discuss: Lru vs lfu
Check yourself
Interview
What interviewers look for
- Name the problem, then its fix: "Keys loaded together expire together, so I add a random extra to each TTL."
- For ids that do not exist: "I cache the not-found answer for a short time, or check a Bloom filter first."
- For a busy key: "On a miss, one request rebuilds the value under a lock, and the others wait for it."
Say it out loud: How does a Bloom filter stop cache penetration?
Expected depth by interview level
Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.
- JuniorUseful
- LRU at a high level.
- MidExpected
- Eviction policies and cold caches.
- SeniorCore
- Stampedes, hot keys and cache failure modes.
- StaffExpected
- Designing so a cache outage degrades the service rather than breaking it.
Practice
Apply it
Three harder questions
The first two use the idea on a new case. The last asks you to judge a design.
Apply it
Apply it
Judge a design
Go deeper
Sources and further reading
First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Official docsKey evictionRedis documentationEach Redis eviction policy, and why its LRU and LFU are close estimates rather than exact.
- Research paperScaling Memcache at Facebook (2013)Nishtala et al., USENIX NSDILeases: how Facebook stops many servers rebuilding the same missing key at once.