Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 42 ยท Caching and Application Patterns

Cache Invalidation and Stampedes

Learn how to invalidate Redis caches safely and stop cache stampedes with locks, early refresh, stale-while-revalidate, negative caching and hot-key tactics.

Putting data in a cache is easy. Keeping it correct and keeping it from falling over are the hard parts. A famous joke says there are only two hard problems in computer science: naming things and cache invalidation. The joke lands because a cache is a copy, and every copy can drift away from the original.

This lesson covers two linked problems. Invalidation is deciding when a cached value must stop being used. A stampede (also called a thundering herd or dogpile) is what happens when a popular key expires and hundreds of requests all rebuild it at the same moment, hammering the database you were trying to protect. If you have not seen the basic cache-aside flow yet, read caching patterns first.

Real Redis cannot run on this page. Commands such as SET key value NX PX 5000 appear as read-only snippets, and every runnable example uses a small JavaScript stand-in with a fake clock, so the timing is deterministic. The stand-ins model the cache and the database, not a real network.

Four ways to invalidate

Expire Let a TTL decide.
Delete on write Remove the key when the data changes.
Version Change the key name, ignore the old one.
Events Publish a change and let caches react.

Most systems combine a TTL with one of the others.

1 / 2

Invalidation strategies

Time-based expiry. Every cached value gets a TTL (SET product:7 ... EX 300). It is simple and bounded: even if you forget everything else, wrong data lives at most five minutes. Choose the TTL from how stale users can tolerate, not from how long the query takes. Add a small random jitter (for example 300 seconds plus or minus 10%) when many keys are filled together, otherwise they all expire together, which is a stampede on a larger scale (a cache avalanche).

Delete on write. When your code changes a product, it also runs DEL product:7. The next read rebuilds from the database. Prefer delete over update: writing the new value into the cache from the write path races with concurrent readers and can leave an older value behind. Delete only after the database transaction commits. If you delete first, a reader can slip in, load the old row, and cache it again.

Even delete-after-commit has a small window: a reader that loaded the old row just before your commit can write it back just after your delete. A short TTL is the safety net that bounds this. Some teams also repeat the delete a second later ("delayed double delete").

Versioned keys. Instead of deleting many keys, put a version in the name: catalog:v12:category:5. To invalidate the whole catalog, INCR catalog:version and build new keys with the new number. Old keys are never read again and simply expire. This is also the cleanest way to invalidate groups ("everything about tenant 9") without scanning.

Event-driven invalidation. Services publish "product 7 changed" on pub/sub or a stream, and every cache owner deletes its key. This scales across many applications but needs a reliable channel, because plain pub/sub can lose messages (see pipelining and pub/sub). Keep a TTL anyway.

The stampede

Imagine product:7 is read 1,000 times per second and the query takes 200 ms. The moment its TTL runs out, the first request misses and starts a query. So does the second, the third and every other request that arrives during those 200 ms: about 200 identical queries instead of one. If the database slows down under that load, the queries take longer, more requests pile up and the problem feeds itself.

The simulation below uses a fake clock. Twenty requests arrive one millisecond apart at an empty cache. The "naive" version is plain cache-aside. The "lock" version lets the first request claim lock:product:7 with SET NX PX semantics and makes the others poll the cache.

JS Stampede versus a rebuild lock

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The lock cuts the database work from twenty queries to one, and nobody waits longer than the single query takes. In real Redis the lock must have an expiry (the PX 5000 part) so a crashed rebuilder cannot block everyone forever. Releasing it safely is the topic of queues, locks and leaderboards.

sequenceDiagram
    participant A as Request A
    participant R as Redis
    participant D as Database
    participant B as Request B
    A->>R: GET product:7 (miss)
    A->>R: SET lock NX PX 5000 (OK)
    B->>R: GET product:7 (miss)
    B->>R: SET lock NX PX 5000 (nil)
    A->>D: query
    B->>R: wait, then GET again
    D-->>A: row
    A->>R: SET product:7, DEL lock
    B->>R: GET product:7 (hit)

Only the request that wins the lock touches the database; the rest wait and read the fresh value.

Refresh early, probabilistically

A lock still makes some users wait. Another idea: refresh before the key expires, so it never expires under load. The trick is to avoid every client deciding at the same instant. The "XFetch" approach lets each request decide independently with a random roll: the closer the expiry and the more expensive the rebuild, the likelier a refresh. The test is now - delta * beta * ln(random()) >= expiry, where delta is how long a rebuild takes and beta (default 1) makes refreshes more or less eager. Since ln(random()) is negative, the left side is pushed forward in time by a random amount, so some request crosses the expiry line early.

JS Probabilistic early refresh (XFetch)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

With one request per second, the refresh usually happens just before the deadline; with a hundred requests per second, the value is refreshed a bit earlier, because more dice are rolled. Either way, a single request starts the rebuild and then writes a fresh value with a new TTL, so the rest keep getting hits. In practice you store the rebuild duration next to the value so each request knows delta.

Stale while revalidate and negative caching

Stale-while-revalidate keeps two deadlines per entry: a soft TTL after which the value is stale, and a hard TTL after which it must not be used. Between the two, readers get the old value immediately while one of them (guarded by a short lock) refreshes it in the background. Users never wait, at the price of slightly old data. In Redis you can store the soft deadline inside the value (for example in a hash field) and use EXPIRE for the hard one.

Negative caching stores the answer "this does not exist". Without it, a request for a missing id always misses the cache and always reaches the database, so a crawler or an attacker asking for random ids bypasses your cache entirely. Cache a marker value for a short time (30 to 60 seconds is typical) so repeated lookups of absent data are cheap, and delete the marker when the row is created.

JS Stale while revalidate plus negative caching

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that the stale read at second 70 still shows price=10, which is the trade: instant answers, bounded staleness. The three lookups of the missing id 999 cost a single database read; only after the marker expires does the database get asked again.

Hot keys

Sometimes the problem is not an expiry but one key that is simply too popular. A single key lives on one Redis node (in Redis Cluster it belongs to one slot), so a viral product page can saturate that node while others sit idle. Options, roughly in the order you should try them:

  1. Add a tiny in-process cache in each application server with a TTL of one to five seconds. A thousand requests per second per server becomes one per few seconds.
  2. Spread the key: store copies as product:7:0 to product:7:7 and read a random suffix, so the load lands on several keys (and, in a cluster, several slots). The price is writing and invalidating every copy.
  3. Serve reads from replicas when slightly stale data is acceptable.

Find hot keys with redis-cli --hotkeys (needs an LFU eviction policy, see eviction and memory) or by sampling MONITOR briefly on a test system, never for long on production.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is deleting a cache key after a write usually safer than writing the new value into the cache?

    Writing from the write path races with readers that loaded older data. Deleting after the database commit makes the next read rebuild from the source, and a TTL bounds any remaining window.
  2. 2. What problem does a short-lived lock (SET NX PX) solve during a stampede?

    Only the request that wins the lock queries the database. The others poll the cache and find the fresh value, so one query replaces hundreds.
  3. 3. In stale-while-revalidate, what happens to a read that arrives between the soft and the hard TTL?

    The soft TTL marks the value as stale but still usable. The reader is answered at once and one request triggers the refresh.
  4. 4. Why cache the fact that a database row does not exist?

    Without a negative entry, every lookup of a missing id is a cache miss and a database query. A short marker absorbs that traffic.
  5. 5. A key is far busier than any other and saturates one Redis node. Which is a sensible first fix?

    A tiny local cache absorbs most reads before they reach Redis. Spreading the key across copies or reading from replicas are the next steps.

Summary

  • Combine a TTL with an explicit strategy: delete on write (after commit), versioned keys, or change events.
  • Add jitter to TTLs of keys filled together so they do not all expire at once.
  • A stampede is many requests rebuilding one expired key; a SET NX PX lock lets one rebuild while the others wait.
  • Probabilistic early refresh (XFetch) and stale-while-revalidate avoid the wait by refreshing before or while serving old data.
  • Negative caching absorbs repeated lookups of missing data with a short TTL.
  • Hot keys are cured with local caches, key copies or replica reads.

Next, what Redis does when memory runs out: Memory Limits and Eviction.