Eviction & Invalidation
Definition
Eviction
- In plain English
- Eviction is removing items from a full cache to make room for new ones.
- Think of it like
- It is like clearing old leftovers from a full fridge.
Invalidation
- In plain English
- Invalidation is removing or updating a cached item because the original data changed.
- Think of it like
- It is like taking down a poster when the event's date changes.
TTL
- In plain English
- TTL, time to live, is how long an item may stay in the cache before it expires — the same idea you met in DNS.
- Think of it like
- Like a best-before date.
Why it exists
Your cache is full when a new menu arrives. Something must leave to make room.
A changed price needs a fresh copy. Thousands of readers may rush the database when one popular page expires.
How it works
LRU
- In plain English
- LRU, least recently used, evicts the item that hasn't been used for the longest time.
- Think of it like
- Like clearing out the clothes you haven't worn all year.
LFU
- In plain English
- LFU, least frequently used, evicts the item used the fewest times.
- Think of it like
- Like removing the app you've opened only once.
Cache stampede
- In plain English
- A cache stampede is when a popular item expires and a crowd of requests all hit the database at once to reload it.
- Think of it like
- Like everyone rushing the kitchen the moment the buffet tray empties.
| Rule | Removes what | Good for | Weakness |
|---|---|---|---|
| LRU | Longest time since last use | Recent interests | A spike can displace lasting favourites |
| LFU | Fewest uses | Lasting popularity | Old favourites can linger |
| TTL | Items past expiry | Bounded cache lifetime | Many items can expire together |
- Expire copies with a TTL.
- After a database write, delete or update the cached copy.
- A concurrent read can restore old data, so deletion alone does not guarantee perfect freshness.
Readers find an expired page; one reload reaches the database while other readers wait for the fresh copy.
- Let one request rebuild while others wait or receive an acceptable old copy.
- Refresh a popular copy shortly before it expires.
- Add a small random amount to expiry times to spread different keys across time.
Examples
Bakery orders
The menu is cached for one hour. After a price change, the app removes the menu key so another read reloads it.
Commit the database change before deleting the key. Keep a TTL as a fallback if deletion fails.
1. Update the menu price in the database.
2. Commit the transaction.
3. Delete cache key menu:today.The insight
Invalidate on write for data that must change promptly.
Concert tickets
A huge show’s event page expires at 10:00:00. In one second, 12,000 requests try to reload it.
Choose one request to rebuild. Let other readers briefly receive the previous page only when its contents remain safe to show.
12000 readers → One coordinated refresh → Database.
Random expiry times help different keys avoid expiring together. They do not stop a crowd rebuilding this single key.
The insight
Protect hot keys from stampedes.
Podcast hosting
An LRU memory cache of episode details kept evicting evergreen episodes during a news spike; LFU kept the always-popular ones.
A news spike fills the cache with recently opened episodes. LRU can discard older episodes that listeners return to every day.
| Policy | Result in this workload |
|---|---|
| LRU | Favours the news spike |
| LFU | Retains frequently requested evergreen episodes |
The insight
Pick the eviction policy that matches how popularity behaves.
Trade-offs
Pros
- Make room for useful copies.
- Refresh changed data.
- Control reload spikes.
Cons
- Invalidation can race with reads.
- Reload coordination adds work.
- Old copies can still appear.
Use when
- Set expiry for copies that can become stale.
- Coordinate reloads for very popular keys.
Avoid when
- Avoid complex invalidation for tolerant data.
- Avoid serving old safety-critical data during refresh.
Quick check
Takeaways
- Eviction makes room; invalidation removes or updates stale copies.
- Use TTL as a fallback for stale cached data.
- Coordinate reloads to protect a popular key.