← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Thundering Herd Problem

concurrency Intermediate

TL;DR

Thundering herd: many processes simultaneously wake up to handle one event — all compete, one wins, the rest wasted work. Common after cache expiry or server restart.

Explanation

Cache stampede (a thundering herd variant): cache entry expires → hundreds of concurrent requests all miss → all hit the DB simultaneously → DB overloaded → all queries slow/timeout → all requests fail. Solutions: (1) Cache locking (mutex on cache miss — only one regenerates). (2) Cache stale-while-revalidate (serve stale during regeneration). (3) Probabilistic early expiry (randomly regenerate before expiry). (4) Cache warming before expiry. Another variant: all workers wake on a queue event — only one gets the job but all CPU-spin. Fix: use SKIP LOCKED or randomised polling intervals.

Common Misconception

Thundering herd only affects caches — it affects any situation where many processes simultaneously target the same resource: queue workers, connection pools, server restarts.

Why It Matters

Cache stampede can take down a production database in seconds — a perfectly healthy service under normal load collapses when the cache expires.

Common Mistakes

  • Long TTL to avoid expiry — just delays the stampede.
  • Not locking cache regeneration — every request races to regenerate.
  • Simultaneous queue worker start — all workers poll at the same time.

Code Examples

✗ Vulnerable
// Cache stampede:
$value = $cache->get('expensive_query');
if ($value === null) {
    $value = $db->runExpensiveQuery(); // All 500 concurrent requests hit this
    $cache->set('expensive_query', $value, 3600);
}
✓ Fixed
// Mutex on cache miss:
$value = $cache->get('expensive_query');
if ($value === null) {
    $lock = $redis->set('lock:expensive_query', 1, ['NX','EX'=>10]);
    if ($lock) {
        $value = $db->runExpensiveQuery();
        $cache->set('expensive_query', $value, 3600);
        $redis->del('lock:expensive_query');
    } else {
        // Wait briefly then re-check cache:
        usleep(100000);
        $value = $cache->get('expensive_query') ?? $fallbackValue;
    }
}

Added 23 Mar 2026
Views 25
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 1 ping S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Perplexity 6 ChatGPT 3 Unknown AI 3 Ahrefs 3 Meta AI 2 Google 2 Qwen 1
crawler 18 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Medium
⚡ Quick Fix
Lock cache regeneration with a distributed mutex. Use stale-while-revalidate. Stagger cache TTLs with random jitter. Warm caches proactively before expiry.
📦 Applies To
web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
cache->get.*null.*db->
Auto-detectable: ✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: File Tests: Update
CWE-400 CWE-362

✓ schema.org compliant