DEV Community

Pratik
Pratik

Posted on

Why your Cache won't save you from a "Thundering Herd" πŸ‚

We've all been there: you add Redis, and your latency drops. But have you planned for the Thundering Herd?
The Problem A Thundering Herd occurs when a massive, synchronized search of processes or user requests hits a backend resource at once.
The Pseudo-Code Risk Most of us write logic like this:

let data = await cache.get(key);
if (!data) {
data = await db.query(query); // The Bottleneck!
await cache.set(key, data);
}
return data;

If 100k users hit this code at the same time while the cache is empty, you don't get one DB queryβ€Š-β€Šyou get 100,000 concurrent DB queries. This causes latency spikes, cascading failures, and potential total system downtime.
How to Fix It (The Senior Dev Way)
Distributed Locks: Only the request that grabs the lock can hit the DB.
Jitter: Never let your services retry at the same time. Add a few seconds of randomness to "spread" the load.
Single In-Flight: Use request coalescing to merge 1,000 identical requests into 1 single database call.
Early Re-caching: Don't wait for the cache to expire. Use a background process to refresh the data before it hits the TTL (Time to Live).

Real-world examples include:
Flash Sales: Millions clicking "Buy Now" at 12:00 PM.
Home Page Banners: When a global banner expires, every visitor triggers a cache miss at once.
Service Reboots: Hundreds of services trying to build connection pools the moment a DB comes back online.

How do you handle high-concurrency at your job? Let's discuss in the comments! πŸ‘‡

Top comments (0)