System Design Academy

Saving…

Lesson 3 of 9 in Module 40 of 54 lessons complete

Eviction & Invalidation

Module 4, lesson 3Concept10 min

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.
RuleRemoves whatGood forWeakness
LRULongest time since last useRecent interestsA spike can displace lasting favourites
LFUFewest usesLasting popularityOld favourites can linger
TTLItems past expiryBounded cache lifetimeMany 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 a popular missing copy.

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.

Invalidate the menu after a saved price change.
Text
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.

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

A popular cached page expires and thousands of requests hit the database at once. What is this called?
Which policy evicts the item not used for the longest time?

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.

Try the quick check above whenever you feel ready.

Previous: Caching Strategies