Distributed caching - hot spots and cache invalidation
Distributed caching reduces repeated backend reads, but hot spots, stale data and invalidation require careful cache-aside design, ownership and operational controls.
Distributed caching - hot spots and cache invalidation
Reading a fund price from a database on every request is becoming expensive when the same price is requested by many investors. Adding a local memory cache can improve one server, but the next request may reach another instance and read the database again. A distributed cache is useful, but it is also introducing expiry, invalidation, serialization and stampede problems.
“There are only two hard things in Computer Science: cache invalidation and naming things.”
In this article, we are going to see cache-aside, read-through and write-through patterns, Azure Redis Cache, IDistributedCache, TTL with jitter, cache stampede protection and hot-key handling. The goal is not to cache everything; it is to cache the right data with a clear correctness rule.
TL;DR Summary
- Cache-aside lets the application read the cache and load the store on a miss.
- Read-through and write-through put more cache responsibility behind an abstraction.
- TTL and invalidation must be designed together, otherwise prices become stale.
- Jitter avoids many keys expiring at exactly the same time.
- Hot keys need replication, request coalescing or a local L1 cache.
- Never cache per-user secrets or authorization decisions without a safe boundary.
Challenges
Repeated reads
Fund factsheets and current prices can be requested many times while the source changes less frequently. The database is paying for repeated reads, and latency is reaching the customer even when the answer is already known.
Stale values
A cache is storing a copy, so a price update must decide how quickly the copy becomes invalid. A long TTL reduces database load but can show an old unit value. A very short TTL improves freshness but removes much of the cache benefit.
Cache stampede and hot spots
When a popular key expires, many requests can load the same database row simultaneously. A hot key such as one popular fund can also overload one cache partition while other capacity is unused.
What is distributed caching?
Distributed caching stores temporary application data in a shared cache service which can be accessed by multiple application instances.
In simple terms, it is a shared fast copy of data. Azure Redis Cache and StackExchange.Redis are common choices, and ASP.NET Core 1.1 provides IDistributedCache through Microsoft.Extensions.Caching.Redis.
The following diagram depicts the cache-aside request flow.

The purple path is a cache hit: the request returns the cached fund price without reaching the database. A miss loads the source, stores a bounded copy and returns the result.
Why distributed caching is important?
Distributed caching is important because it is reducing repeated source reads and improving response latency across multiple application instances. It is also making the data flow more complex, so freshness and failure behaviour must be explicit.
In our case, a current price can be cached for a short TTL when the valuation process publishes invalidation after a new price is accepted. A user-specific permission result should not be placed in the same shared cache without including the correct identity boundary.
Comparison of cache patterns
| Pattern | Read / write flow | Trade-off |
|---|---|---|
| Cache-aside | App reads cache, then source on miss | Simple, application owns invalidation |
| Read-through | Cache loads source on miss | Centralized loading, more cache integration |
| Write-through | App writes cache and source together | Fresh cache, coupled write path |
| Write-behind | Cache writes source later | Fast response, complex durability and failure |
Cache-aside is a good starting point for a fund read API because the application understands the source and can invalidate after a successful write.
Implement cache-aside
Register Redis-backed IDistributedCache in ASP.NET Core 1.1,
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedRedisCache(options =>
{
options.Configuration = "<redis-connection-string>";
options.InstanceName = "fund-api:";
});
}
Read and populate the cache,
public async Task<FundPrice> GetAsync(string fundId)
{
var key = "price:" + fundId;
var cached = await cache.GetStringAsync(key);
if (cached != null)
{
return JsonConvert.DeserializeObject<FundPrice>(cached);
}
var price = await repository.GetLatestAsync(fundId);
var json = JsonConvert.SerializeObject(price);
await cache.SetStringAsync(key, json, new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(30)
});
return price;
}
Use a serialization format with a version and bounded size. Add a prefix such as fund-api: so keys from different applications are not colliding in one Redis instance.
TTL and invalidation
TTL is protecting the system when an invalidation message is lost, but it should not be the only freshness rule. On a successful price write, remove or update the related key. If price changes are published as an event, the handler can invalidate the cache after the source transaction is committed.
Add jitter to expiry,
var ttl = TimeSpan.FromSeconds(30 + random.Next(0, 10));
Jitter is reducing a synchronized expiry wave. Keep the maximum stale period within the business requirement and monitor cache age.
Invalidation should be connected to the business write, not to a later support task. When a price import succeeds, publish or call the invalidation path after the source commit. If the invalidation fails, retain the TTL safety net and alert the team rather than silently claiming that the cache is fresh.
Cache failures should have a pulse check. Temporarily stop the cache in a test environment, confirm the API can read from the source within its timeout, then restore the cache and verify that a fresh value is populated. This is showing whether the cache is an optimization or an accidental single point of failure.
Measure hit ratio by key group and endpoint, not only as one global number. A high overall hit ratio can hide a hot price key, a low-value factsheet key or a per-user request which should never have been cached.
Protect against stampede
Use a lock or request coalescing for a popular key so one request loads the database while other callers await the result. An early refresh can also reload the key before expiry. The lock must have a timeout and failure path; a distributed lock is another system to operate.
For a hot key, replicate the value, use a local L1 memory cache in front of Redis or change the access pattern. Do not simply increase the global cache size, because the hot key may still be concentrated on one partition.
Selection criteria
Choose a distributed cache when multiple instances need the same temporary copy and the source can tolerate the defined stale window. Choose local memory cache for data which is cheap to rebuild and does not need cross-instance consistency. Use the source directly when correctness is more important than the latency benefit.
Check key cardinality, serialization size, eviction policy, Redis tier, network latency and failure behaviour. A cache outage should have a deliberate fallback to the source or a clear error, not an accidental infinite retry.
Must Do / Should / Don’t
Must Do
- Must define TTL, invalidation and stale-data behaviour.
- Must bound serialized values and include a cache-key namespace.
- Must protect hot keys and cache stampede paths.
- Must avoid caching secrets and per-user authorization decisions by default.
Should
- Should start with cache-aside and a simple read model.
- Should add TTL jitter and monitor hit rate, age and evictions.
- Should invalidate after successful source writes or events.
Don’t
- Don’t cache every database query without a freshness rule.
- Don’t assume a cache hit means the value is current.
- Don’t let one hot key consume all cache capacity.
- Don’t make cache availability a hidden single point of failure.
Wrap up
In this article, we have looked at distributed caching, cache-aside, read-through, write-through, Redis, IDistributedCache, TTL jitter, hot keys and stampede protection.
Thanks for reading this article. In the next article, I will walk you through Architecture Decision Records and documenting the why.
Related Posts
How to design and implement Future State Architecture? - Part 2
Design a Future State Architecture in three views, logical components, technology choices and solution architecture, and plan the move from the current state.
What is Future State Architecture and why do we need it? - Part 1
A Future State Architecture is a shared destination, not a big-bang rewrite. Learn why we need it, how to think about it in seven steps and the principles behind it.
Architecture Decision Records (ADR) - documenting the "why"
Architecture Decision Records preserve the context behind technical choices, showing how to write, store, review and evolve an ADR through a fund-domain example.