SkillSprout
Chapter 12 · Lesson 3
Intermediate
3 min read

Invalidation and Cache Stampede

Overview

Caching is easy until the data changes. This lesson covers the two classic headaches: keeping cached data fresh enough, and the pile-up that happens when a popular entry expires.

Detailed Notes

What is cache invalidation

Invalidation means removing or updating cached data when the source changes, so users do not keep seeing old values. It is famously hard because the cache and the database are two separate places that cannot change at exactly the same moment.

The problem it solves

Two things go wrong in real systems:

  • Stale data: the database changed but the cache still holds the old value.
  • Cache stampede: a very popular entry expires, and thousands of requests all miss at the same instant. They all hit the database to rebuild the same value, and the database may collapse.

How it works

Keeping data fresh

  • TTL: the simplest approach. Accept staleness up to the TTL. Choose short TTLs for fast-changing data and long ones for stable data.
  • Explicit invalidation: delete the key when the data changes (as in the previous lesson).
  • Versioned keys: put a version in the key (for example product:42:v7). New data means a new key, so old entries just age out.

Preventing a stampede

  • Single-flight / locking: only one request rebuilds the missing entry; the others wait for it or briefly use the old value.
  • TTL jitter: add a small random amount to each TTL so many keys do not expire at the same second.
  • Refresh ahead: refresh hot entries in the background shortly before they expire.
  • Warm the cache: pre-load hot data at startup, so a cold, empty cache does not overload the database.

A related trap: if a key does not exist in the database, it will never be cached, so repeated requests for it always hit the database. The fix is negative caching: store "not found" for a short time.

A simple example

A shop's price list is printed and pinned on the wall. When prices change, someone must replace the sheet. If every employee threw away the sheet at exactly 9:00 and ran to the manager for the new prices, the manager would be swamped. A stampede is that rush.

System design example

A homepage cache entry takes 2 seconds to build and is read 10,000 times per second. Without protection, expiry causes about 20,000 identical rebuilds. With single-flight, one request rebuilds while the others wait about 2 seconds or use the old copy, and jittered TTLs stop all the hot keys expiring together.

Summary

  • Invalidation keeps cached data in line with the source; TTL is the simple default.
  • Delete or version keys when data changes.
  • A stampede is many misses rebuilding one entry at once.
  • Prevent it with locking, TTL jitter, refresh-ahead and warming.
  • Cache "not found" briefly to avoid repeated pointless database hits.

Architecture Diagram

Stampede-safe cache read
Architecture
Many requests
App servers
RedisTTL + jitter
Database

Only one request rebuilds an expired entry.

  1. 1A popular entry expires and many requests miss.
  2. 2One request takes a lock and rebuilds it.
  3. 3The others wait or use the old value.
  4. 4The cache is refilled with a jittered TTL.

Common Interview Questions

Chapter quiz

Caching · 5 questions

  1. 1.What is the main purpose of a cache?

  2. 2.How is hit ratio calculated?

  3. 3.Which write strategy risks losing data if the cache crashes?

  4. 4.Which policy evicts the entry unused for the longest time?

  5. 5.What is a cache stampede?

0 of 5 answered

Finished this lesson?

0 of 91 lessons done · 0% of the course