Skip to content

Thundering herd

The thundering herd problem occurs when a large number of clients or processes simultaneously hit a system (usually after a cache expiry or a service restart), causing a burst of load that the system cannot handle gracefully.

Definition

What it is

When a cache holds a hot key and that key expires, every request that arrives before the next fill misses the cache and hits the origin. If the key had 10,000 requests per minute, the origin now sees 10,000 simultaneous queries. Most databases cannot sustain this for even one second. Latency spikes; timeouts cascade; the system either recovers (slowly) or falls over.

The same pattern appears with connection pool resets, DNS TTL expiry, TLS cert rotation, service restarts (all workers reconnect at once), and cron jobs scheduled at :00:00.

Why it matters

A single hot key expiry can take down a production database. Teams often discover the problem in the worst possible way — during peak traffic. The fix is small (a lock) but only if you know it exists.

Prevention strategies

Strategy

Mechanism

Best for

Lock

First request takes mutex, others wait

Critical hot keys

Single-flight

Only one refresh; others get stale

Library-level

Jitter TTL

Randomize expiry ±10%

Even distribution

Cache warming

Pre-fill before expiry

Known hot keys

Stale-while-revalidate

Serve stale while refreshing

HTTP responses

The thundering herd flow

sequenceDiagram
  participant C as Clients (N)
  participant Cache
  participant DB
  Note over Cache: T+0 key expires
  C->>Cache: GET
  Cache-->>C: MISS
  C->>DB: query (N times)
  Note over DB: CPU saturates
  DB-->>C: slow or timeout
  Note over Cache: One refill succeeds
  C->>Cache: GET
  Cache-->>C: HIT

How to prevent it

Killing the herd

  1. Single-flight / mutex

    In Redis: SET lock:key 1 NX EX 10. Winner refills, others wait or serve stale.

  2. Jitter TTL

    If TTL is 60s, use 60 ± 6s. Spreads expiry across 12 seconds.

  3. Stale-while-revalidate

    Serve stale for up to 5 minutes while refreshing. Users never wait.

  4. Warm cache before expiry

    Cron every 30s re-fills known hot keys before they expire.

  5. Backpressure

    Limit concurrent origin queries via a semaphore. Reject excess.

  6. Never broadcast

    Push invalidation sends "revalidate", not "evict". Let the cache refill on next read.

Common failures

Origin

The image comes from animal stampedes: a small disturbance triggers a mass simultaneous flight. In computing the term was used informally in the 1990s (thundering herd on file locks) and formalized in HTTP caching discussions (notably by the Squid project and Cloudflare). The W3C and IETF discuss it in cache-related RFCs (RFC 7234).

A thundering herd is when a large number of processes all try to acquire the same lock or hit the same resource at the same time.

— Beej's Guide to Unix IPC, 2024 (paraphrase)
1990s · thundering herd en locks Unix
2000s · cache stampede en web
2010s · jitter TTL adoptado
2022 · RFC 9111 discute cache herd

Evidence

FAQ

Thundering herd vs cache stampede?

Same problem, different name. "Cache stampede" emphasizes the cache; "thundering herd" emphasizes the simultaneous requests.

When does it happen?

Cache expiry, service restart, DNS TTL, cert rotation, cron at :00.

Does CDN prevent it?

Partly. The CDN absorbs the burst if it has the key cached; if not, it forwards everything.

Can I just use a longer TTL?

Longer TTL reduces frequency, not the burst. Jitter + locks are the real fix.

Connections

Etymology

Origin: metaphor

  • thundering (adjective): from Old English þunor, "thunder"
  • herd (noun): from Old English heord, "group of animals"

Semantic network

Thundering hperformancCacheCache invaStale-whilsingle-fliBackpressu

● broader ● narrower ● related

Broader

  • performance

Related

Sources