Skip to content
HLD Learn/Caching for Speed
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Cache Invalidation & Eviction

4 min read

You'll learn to

  • -Explain LRU and LFU eviction
  • -Explain the cache stampede problem

Eviction: What Happens When the Cache Is Full

A cache is finite memory, so once it's full, adding something new means evicting something old. Least Recently Used (LRU) evicts whatever hasn't been touched in the longest time (simple and usually a good default). Least Frequently Used (LFU) evicts whatever has been accessed the fewest times overall, which is better when "hot" items stay hot for a long time and you don't want a single recent-but-rare access to save something from eviction.

The Same Accesses, Two Eviction Policies

A capacity-4 cache sees the same 10 accesses. Watch LRU and LFU disagree on what to keep.

ABCDABEACF

Cache contents (capacity 4)

A

-

-

-

Access A: miss, room available. Insert A at the front.

access 1 / 10

Invalidation: The Other Hard Problem

A cache is only useful if it's telling the truth. TTL-based invalidation (expire after N seconds) is simple but means you're always serving data that could be up to N seconds stale. Event-driven invalidation (bust the cache key the moment the underlying row changes) is accurate but adds real complexity: now every write path has to remember to also talk to the cache.

Cache Stampede

When a single hot key expires, every request that was previously served instantly from cache suddenly misses at the same moment, and all of them hit the database simultaneously, which can be enough load on its own to take the database down. Common fixes: a distributed lock so only one request repopulates the cache while others wait briefly, or jittering TTLs slightly per key so they don't all expire in the same instant.

A useful mental model: caching doesn't remove the hard problem of keeping data correct; it just relocates it from "how fast can I read" to "how do I know when to forget."

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

Ready to Build This?

Level 3: URL Shortener is a caching-heavy, read-dominant system.