Menu

Cache Stampede (Thundering Herd) in Node.js & Redis: Preventing Database Meltdowns on Cache Misses
πŸ“°

Cache Stampede (Thundering Herd) in Node.js & Redis: Preventing Database Meltdowns on Cache Misses

DEV Community: redisΒ·subashthiruppathyΒ·3 days ago
#NdlKspsP
#devtoredis#devto#dev#redis#const
Reading 0:00
15s threshold

You added Redis in front of an expensive query, and it worked beautifully. The dashboard that took 800 ms now returns in 3 ms. The database CPU dropped from 70% to 10%. Everyone is happy.

Then, one afternoon, the database CPU spikes to 100%, latency goes vertical, and the incident channel lights up. The graphs show a sharp, periodic pattern: calm, spike, calm, spike. Nothing deployed. Traffic didn't change.

What happened is a cache stampede (also called the thundering herd or dogpile problem). A hot key expired, and for the next few hundred milliseconds, every request that wanted it missed the cache at the same moment and ran the expensive query itself.

This post explains exactly how that happens and builds up a set of defenses in Node.js with ioredis , from a ten-line fix to a production-grade helper.

Anatomy of a stampede

Here's the classic cache-aside code:

async function getLeaderboard () { const cached = await redis . get ( ' leaderboard ' ); if ( cached ) return JSON .…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.