Imagine your application has a popular API endpoint.
You cache its response in Redis for 5 minutes to avoid hitting the database repeatedly.
Everything works perfectly.
Until the cache expires.
Suddenly, 10,000 requests arrive at the same time. They all find an empty cache and start querying your database.
Congratulations. Your cache just caused a database traffic spike.
This problem is called a Cache Stampede , also known as the Thundering Herd .
The Usual Solution: Distributed Locks
A common solution is to use a distributed lock.
When the cache expires:
The first request acquires a lock.
It fetches fresh data from the database.
Other requests wait or use stale data.
The cache gets updated.
It works, but now you're managing locks, timeouts, and failure cases.
What if we could refresh the cache before it expires?
Meet XFetch
XFetch is a clever algorithm based on probabilistic early expiration .
Instead of waiting until the cache expires, it randomly decides whether to refresh it…