Q: A Redis cache server is placed in front of a slow database. During a major marketing campaign launch, the cache suddenly purges its keys, and the database instantly crashes under the thundering herd of traffic. What is this phenomenon called, and how do you mitigate it?
This is called a Cache Stampede (or Thundering Herd). It happens when a highly popular cached key expires.
Want to master this scenario in a live sandbox? KodeKloud's PostgreSQL Database Administration & High Availability Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: "When an interviewer asks about this scenario, I explain how we balanced incident response with long-term prevention. The interviewer is testing: Cache stampede, cache penetration/misses, jitter.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third."
Initial Diagnostics & Root Cause Analysis
This is called a Cache Stampede (or Thundering Herd). It happens when a highly popular cached key expires. Instantly, thousands of concurrent requests hit the cache, find a miss, and *all* of them simultaneously bypass the cache and query the database to regenerate the exact same data, crashing the database.
- Adding Jitter: Instead of setting a strict 1-hour expiration on keys, add random jitter (e.g., 60 minutes + random 0 to 5 minutes) so keys expire staggeringly, preventing massive simultaneous purges.
- Mutex Locks: When a cache miss occurs, the first thread acquires a distributed lock (e.g., Redis
SETNX) for that specific key. It proceeds to query the database. The other 999 concurrent threads fail to get the lock, and sleep/poll for 50ms until the first thread populates the cache for them.
Remediation & Permanent Safeguards
Mitigation strategies:
- Adding Jitter: Instead of setting a strict 1-hour expiration on keys, add random jitter (e.g., 60...
- Mutex Locks: When a cache miss occurs, the first thread acquires a distributed lock (e.g., Redis ...
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Applying exclusive-lock DDL table migrations on live production databases during peak traffic windows when configuring General Redis cache DDL migrations that acquire an exclusive table lock block all concurrent queries, queuing connections until the database connection pool exhausts.
- Allowing high-frequency queries to trigger full table scans without appropriate composite indexing when configuring General Redis cache Sequential table scans saturate database CPU and disk I/O, evicting frequently queried data from the buffer cache and causing cascading timeouts.
- Disabling referential integrity constraints during bulk ETL imports without post-import consistency validation when configuring General Redis cache Skipping relational consistency checks risks introducing orphaned records and referential integrity violations into production tables.
- Relying on database snapshot backups that have never been verified through automated restoration drills when configuring General Redis cache Backups that have never been tested with automated restore verification frequently fail during real disaster recovery scenarios.