Advertisement
Intermediate ASP.NET Core, Caching

How does HybridCache (introduced in .NET 9) unify in-memory and distributed caching, and what problems does it solve?

Short answer

HybridCache provides a single API layering a fast local in-memory cache in front of a distributed cache like Redis, automatically handling cache stampede protection (request coalescing for concurrent identical cache misses) and serialization -- giving you both the speed of local…

Save ASP.NET Core mock interview
Advertisement
AModel answer
HybridCache provides a single API layering a fast local in-memory cache in front of a distributed cache like Redis, automatically handling cache stampede protection (request coalescing for concurrent identical cache misses) and serialization -- giving you both the speed of local caching and the consistency of a distributed cache without hand-rolling the two-tier coordination logic yourself.
builder.Services.AddHybridCache();

public class ProductService(HybridCache cache) {
    public async Task<Product> GetProductAsync(int id) {
        return await cache.GetOrCreateAsync($"product-{id}", async ct => {
            return await _repository.GetByIdAsync(id, ct);
        }, new HybridCacheEntryOptions { Expiration = TimeSpan.FromMinutes(10) });
    }
}
Real-world example
A team previously hand-rolling a two-tier cache (check IMemoryCache, fall back to Redis, populate both on miss) replaces dozens of lines of custom coordination logic with a single HybridCache.GetOrCreateAsync call after upgrading to .NET 9.
Likely follow-ups
  • How does HybridCache handle stampede protection across multiple server instances, not just within one process?
  • What serialization options does HybridCache support?
Related topics