What if millions of users crash your system just because your cache expired at the same time?
Why Cache stampede prevention in HLD? - Purpose & Use Cases
Imagine a popular website where thousands of users request the same data at the same time. Without any special handling, all these requests hit the database simultaneously when the cache expires.
This causes the database to overload, slowing down responses or even crashing the system. Manually trying to handle this by limiting requests or adding delays is complex and often fails under heavy load.
Cache stampede prevention techniques ensure that when cached data expires, only one request fetches fresh data while others wait or get stale data. This keeps the system stable and fast without overwhelming the database.
if cache expired: fetch data from DB update cache return cache data
if cache expired: if lock acquired: fetch data from DB update cache release lock else: wait for cache update return cache data
It enables systems to handle huge traffic spikes smoothly without crashing or slowing down.
A news website updating its homepage data every few minutes can serve millions of users without database overload by preventing cache stampedes.
Manual cache refresh causes database overload under high traffic.
Cache stampede prevention lets only one request refresh cache at a time.
This keeps systems fast, stable, and scalable during traffic spikes.