LESSON 06 · Cache stampedes

Survive a popular key expiring

When a popular cache entry expires, every concurrent reader can miss at once, so rebuilds must be limited and expiries spread out.

SYSTEM VIEW

The failure path

Diagram view
Reads—
DB queries—
Readers wait—

Watch one hot cache key expire under 5,000 reads a second, then cut the rebuilds from 1,000 to one.

  1. Readers ask for product:42 at 5,000 reads a second. The App servers find it in the Cache, so the Database sees none of them. Then the key expires, and every read after that misses.

    Reads
    5,000/s
    Rebuild
    200 ms

    What changed

    • 5,000 reads/s of product:42
    • product:42 expires
  2. Until the first rebuild finishes, every read misses: 5,000 a second for 0.2 s is about 1,000 readers, and all of them query the Database. Those queries slow it, so rebuilds take longer, more readers miss, and the spike feeds itself.

    Misses
    5,000/s × 0.2 s = 1,000
    Each miss
    queries the Database

    What changed

    • About 1,000 misses
    • 1,000 database queries
    • Slower DB, more misses
  3. Single-flight lets one caller rebuild a key while the others wait for that result. Go’s singleflight does this within a process, so each of the 50 App servers sends one rebuild: at most 50 queries, not 1,000.

    Per server
    1 rebuild
    50 servers
    at most 50 queries

    What changed

    • Single-flight in each App server
    • At most 50 queries, not 1,000
  4. For one rebuild across the fleet, an App server takes a short-lived lock in Redis with SET NX and an expiry. The holder rebuilds product:42 with one query while the others wait. If the holder crashes, the expiry frees the lock.

    Lock
    SET NX + expiry
    Holder crashes
    the lock frees itself

    What changed

    • Redis lock, held by one server
    • 1 query for the whole fleet
  5. Jittered expiry gives each key a TTL of 300 s plus or minus 10%, so keys loaded together expire between 270 and 330 s and their rebuilds spread out. It helps many keys but not one hot key: product:42 still has a single expiry moment.

    TTL
    300 s ± 10%
    Many keys
    expire over 270–330 s
    One hot key
    still one expiry

    What changed

    • Jitter: TTL 300 s ± 10%
    • Many keys expire over 270–330 s
    • product:42 still has one expiry
  6. After the soft expiry, readers get the old product:42 at once while one Refresher rebuilds it, so nobody waits and the Database sees one query. The cost is a bounded period of old data: fine for a product description, not for a permission check.

    Soft expiry
    serve the old value
    Hard expiry
    longer
    Not for
    a permission check

    What changed

    • Redis lock
    • Refresher: one background job
    • Soft expiry: serve the old value
    • Readers don’t wait

Space plays · ← → step · Home resets

THE IDEA

How it works

A cache stampede happens when many readers miss the same entry together and all rebuild it. For example, a key is read 5,000 times per second and takes 200 ms to rebuild. When it expires, about 1,000 readers miss before the first rebuild finishes (5,000 times 0.2), and all query the database. If those queries slow the database, rebuilds take longer, more readers miss, and the spike feeds itself. Scale to a Million Users warns of the same burst when a cache node restarts, and News Feed’s hot timeline and post entries carry the same risk.

Single-flight lets one caller rebuild a key while the others wait for that result. Go’s singleflight package does this within a process, so 50 servers means at most 50 queries, not 1,000. For one rebuild across the fleet, take a short-lived lock in Redis using SET with NX and an expiry. The expiry matters: if the holder crashes, the lock frees itself.

Jittered expiry adds randomness to each entry’s TTL, such as 300 seconds plus or minus 10 percent, so keys loaded together do not expire together. It helps many keys but not one hot key, which still has a single expiry moment.

Serving stale handles the hot key. Give each entry a soft expiry and a longer hard expiry. After the soft expiry, readers get the old value at once while one background worker refreshes it, the idea behind HTTP’s stale-while-revalidate. The cost is a bounded period of old data, which is fine for a product description but not for a permission check.

DECISION POINT

A leaderboard key is read by 300 app servers and takes a 3-second query to rebuild. It expires every 60 seconds, and the database spikes each minute. A teammate proposes adding 10 percent jitter to the TTL. What do you do?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • One expiring hot key can send every concurrent reader to the database.
  • Single-flight limits rebuilds, jitter spreads many keys, and stale serving shields readers from the rebuild.
  • Serve stale data only where a bounded period of old values is acceptable.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

A key is read 2,000 times per second, and a rebuild takes 500 ms. With no protection, about how many readers query the database when it expires?

Choose an answer. You’ll see the explanation once you get it right.

Next chapter: Rate Limiter

0 of 3 checkpoints answered correctly to complete this lesson.