Redis is often placed in front of a database to reduce database load and improve application response times. In a normal cache-aside implementation, the application checks Redis first and queries the database only when the requested value is not available in the cache.
That sounds simple, but there is a production problem that can appear when a popular cache entry expires or gets evicted.
If hundreds or thousands of requests miss the same key at almost the same time, those requests can all query the database. Redis is doing exactly what it was asked to do, but the database suddenly receives a request volume that the cache was supposed to prevent.
This situation is commonly called a cache stampede or thundering herd.
The problem is not that cache misses are bad. Cache misses are expected. The problem is allowing many requests to perform the same expensive cache rebuild at the same time.
This article explains how the problem happens and how to protect a C# application from it using techniques such as request coalescing, distributed locking, stale-while-revalidate, TTL jitter, and refresh-ahead caching.
Understanding the Problem
A typical cache-aside flow looks like this:
Client
|
v
ASP.NET Core Application
|
v
Redis
|
| Cache hit
v
Return cached value
|
| Cache miss
v
Database
|
v
Store result in Redis
|
v
Return resultFor an ordinary request, this works well.
For example:
Request 1 -> Redis hit -> Return value
Request 2 -> Redis hit -> Return value
Request 3 -> Redis hit -> Return valueNow consider what happens when the cached value expires.
Request 1 -> Redis miss -> Database
Request 2 -> Redis miss -> Database
Request 3 -> Redis miss -> Database
Request 4 -> Redis miss -> Database
Request 5 -> Redis miss -> DatabaseAll five requests are asking the database for the same information.
If the application has many instances, the situation can become much worse.
Instance A -> Redis miss -> Database
Instance B -> Redis miss -> Database
Instance C -> Redis miss -> Database
Instance D -> Redis miss -> DatabaseThe database may suddenly receive hundreds of identical queries.
Cache Miss, Cache Stampede, and Cache Penetration Are Different
These terms are sometimes mixed together, but they describe different problems.
Problem | What happens | Typical cause |
|---|---|---|
Cache miss | Requested key is not in Redis | Expiration, eviction, cold cache |
Cache stampede | Many requests miss the same key and hit the database together | Popular key expiration |
Cache penetration | Requests repeatedly ask for data that does not exist | Invalid IDs or uncached negative results |
Cache avalanche | Large numbers of cache entries expire together | Same TTL or mass cache expiration |
A normal cache miss is not necessarily a problem.
A cache stampede is a concurrency problem caused by many requests rebuilding the same cache entry simultaneously.
The Naive Cache-Aside Implementation
A simple C# implementation might look like this:
public async Task<Product?> GetProductAsync(
int productId,
CancellationToken cancellationToken)
{
var cacheKey = $"product:{productId}";
var cached = await _redis.GetStringAsync(cacheKey);
if (!string.IsNullOrEmpty(cached))
{
return JsonSerializer.Deserialize<Product>(cached);
}
var product = await _db.Products
.AsNoTracking()
.FirstOrDefaultAsync(
x => x.Id == productId,
cancellationToken);
if (product is not null)
{
var json = JsonSerializer.Serialize(product);
await _redis.StringSetAsync(
cacheKey,
json,
TimeSpan.FromMinutes(10));
}
return product;
}The code is valid for a basic cache-aside implementation.
The problem appears under concurrency.
Suppose a product is extremely popular and its Redis entry expires.
Ten requests can execute this sequence:
Request 1: GET product:100 -> MISS
Request 2: GET product:100 -> MISS
Request 3: GET product:100 -> MISS
Request 4: GET product:100 -> MISS
Request 1 -> Database
Request 2 -> Database
Request 3 -> Database
Request 4 -> DatabaseEach request performs the same database operation.
The database work is duplicated even though only one request actually needed to rebuild the cache.
Why Popular Keys Are More Dangerous
Not every cache key creates the same amount of risk.
Consider two keys:
product:918273and:
homepage:featured-productsThe first might receive a few requests per minute.
The second could receive thousands of requests per second.
If the second key expires, the application can generate a sudden database spike.
This is why cache protection should focus on hot keys rather than treating every cache entry exactly the same way.
Solution One: Request Coalescing
One of the simplest solutions is to make requests for the same key share the same refresh operation.
This is sometimes called single-flight or request coalescing.
The idea is:
Redis MISS
|
+---------+---------+
| | |
Request Request Request
\ | /
\ | /
+-------+-------+
|
One DB operation
|
Cache result
|
All requests receive
the resultIn a single application instance, a keyed ConcurrentDictionary can coordinate requests.
For example:
private readonly ConcurrentDictionary<string, Lazy<Task<Product?>>> _refreshes = new();
public async Task<Product?> GetProductAsync(
int productId,
CancellationToken cancellationToken)
{
var cacheKey = $"product:{productId}";
var cached = await _redis.GetStringAsync(cacheKey);
if (!string.IsNullOrEmpty(cached))
{
return JsonSerializer.Deserialize<Product>(cached);
}
var refresh = _refreshes.GetOrAdd(
cacheKey,
_ => new Lazy<Task<Product?>>(
() => LoadProductAndCacheAsync(
productId,
cacheKey,
cancellationToken)));
try
{
return await refresh.Value;
}
finally
{
_refreshes.TryRemove(cacheKey, out _);
}
}The actual database and Redis refresh can be placed in a separate method:
private async Task<Product?> LoadProductAndCacheAsync(
int productId,
string cacheKey,
CancellationToken cancellationToken)
{
var product = await _db.Products
.AsNoTracking()
.FirstOrDefaultAsync(
x => x.Id == productId,
cancellationToken);
if (product is not null)
{
var json = JsonSerializer.Serialize(product);
await _redis.StringSetAsync(
cacheKey,
json,
TimeSpan.FromMinutes(10));
}
return product;
}Now concurrent requests for the same key can share the same refresh task.
There is an important limitation.
This dictionary exists only inside one application process.
If the application has five instances, each instance has its own dictionary.
Instance A -> local lock
Instance B -> local lock
Instance C -> local lock
Instance D -> local lockRedis can still receive multiple refresh operations from different instances.
For a distributed application, another strategy is needed.
Solution Two: Distributed Locking With Redis
A Redis-based distributed lock can allow one application instance to rebuild a cache entry while other instances wait or use another response strategy.
The basic concept is:
Instance A -> Acquires lock
Instance B -> Cannot acquire lock
Instance C -> Cannot acquire lock
Instance A -> Database
Instance A -> Redis
Instance A -> Releases lock
Instance B -> Reads Redis
Instance C -> Reads RedisA Redis lock can be created with SET using the NX and PX options.
With StackExchange.Redis, the operation can look like this:
var lockKey = $"lock:{cacheKey}";
var lockToken = Guid.NewGuid().ToString("N");
var acquired = await _redis.StringSetAsync(
lockKey,
lockToken,
TimeSpan.FromSeconds(10),
When.NotExists);When.NotExists ensures that the lock is created only when another process does not already hold it.
The lock should always have an expiration.
Without an expiration, a crashed application could leave a permanent lock behind.
Releasing the lock also needs care.
An application should not simply delete the lock key.
Imagine this sequence:
Instance A -> Acquires lock
Instance A -> Processing takes too long
Lock expires
Instance B -> Acquires new lock
Instance A -> Finishes
Instance A -> Deletes lockInstance A has now deleted a lock owned by Instance B.
The safer approach is to release the lock only if the stored token still belongs to the current process.
A Lua script can perform the check and delete atomically:
if redis.call('get', KEYS[1]) == ARGV[1] then
return redis.call('del', KEYS[1])
else
return 0
endThe token acts as ownership information.
Distributed locks are useful, but they should not automatically become the default solution for every cache miss.
A lock that is held while a slow database query runs can create another bottleneck.
For many applications, stale-while-revalidate is a better approach for high-traffic data.
Solution Three: Stale-While-Revalidate
With stale-while-revalidate, the application can continue serving an older value while one request refreshes the cache.
Instead of treating expiration as:
Expired -> Delete -> Database -> Cachethe application can use:
Fresh -> Serve normally
Stale -> Serve old value
+
Start refreshThis avoids making every user wait for the database.
Join the conversation! Your thoughts help the community grow.