The Thundering Herd
A cache stampede, also called a dogpile, happens when a popular cached value expires and many requests miss at once. They all rush to recompute the same value, hammering the database and possibly knocking it over just when traffic is highest.
Locking
Let only one request recompute a missing value while others wait or serve a slightly stale copy. A short lived mutex on the key ensures a single rebuild rather than thousands. The winner repopulates the cache and everyone else reads the fresh value.
Early Recompute
Refresh a value before it expires. As an entry nears its expiry, a probabilistic check lets one request rebuild it early so the entry never actually goes cold under load.
Stale While Revalidate
Serve the stale value immediately while a background task refreshes it. Users never wait, and the database sees only one refresh request instead of a herd.
Key idea
A stampede floods the database when a hot key expires, and you prevent it by letting only one request rebuild the value, refreshing early, or serving stale data while revalidating.