Introduction
Caching is one of the most important techniques used in modern .NET applications to improve performance, reduce database load, and provide faster responses to users. Traditionally, developers have used IMemoryCache for in-memory caching and IDistributedCache for distributed caching systems like Redis or SQL Server.
With .NET 9, Microsoft introduced HybridCache, a powerful and modern caching abstraction that combines the benefits of both in-memory and distributed caching into a single unified solution.
In this article, we will understand what HybridCache is, how it works, and how it is different from IMemoryCache and IDistributedCache in simple words.
What is HybridCache in .NET 9?
HybridCache is a new caching API introduced in .NET 9 that combines two layers of caching:
In-memory cache (fast, local to application)
Distributed cache (shared across multiple instances)
It automatically manages both layers, giving you the best performance and scalability without writing extra code.
In simple terms, HybridCache = Memory Cache + Distributed Cache working together.
Why Do We Need HybridCache?
Before HybridCache, developers had to manually manage two caching systems:
First check IMemoryCache
If not found, check IDistributedCache
If still not found, fetch from database
Then update both caches manually
This approach:
Increases code complexity
Leads to duplication
Can cause inconsistencies
HybridCache solves all these problems by providing a single API that handles everything internally.
How HybridCache Works
HybridCache follows a layered caching strategy:
First, it checks the in-memory cache
If not found, it checks the distributed cache
If still not found, it fetches data from the source (like database or API)
Then it stores the data in both caches automatically
This process ensures:
Faster access for repeated requests
Consistency across multiple servers
Reduced load on database
Key Features of HybridCache
1. Unified API
You only use one interface instead of managing multiple caches.
2. Automatic Synchronization
HybridCache automatically keeps memory and distributed cache in sync.
3. Improved Performance
Frequently accessed data is served from memory, which is extremely fast.
4. Scalability
Distributed cache ensures that multiple application instances share the same data.
5. Simplified Code
No need to manually handle fallback logic between cache layers.
Example: Using HybridCache in .NET 9
Here is a simple example to understand how to use HybridCache:
builder.Services.AddHybridCache();
public class ProductService
{
private readonly HybridCache _cache;
public ProductService(HybridCache cache)
{
_cache = cache;
}
public async Task<string> GetProductAsync(string productId)
{
return await _cache.GetOrCreateAsync(
key: $"product_{productId}",
factory: async entry =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5);
// Simulating database call
await Task.Delay(1000);
return "Product Data from Database";
});
}
}
In this example:
The cache first checks memory
Then distributed cache
If not found, it executes the factory method
Stores result in both caches
What is IMemoryCache?
IMemoryCache is a caching mechanism that stores data in the application's memory.
Features:
Very fast access
Data stored locally
Not shared across servers
Limitations:
Data is lost when application restarts
Not suitable for distributed systems

Join the conversation! Your thoughts help the community grow.