Caching is essential for optimizing application performance by lowering data retrieval latency and reducing the number of database or API calls. In .NET 9.0, Microsoft has introduced improved caching strategies, including Hybrid Caching, which merges the advantages of in-memory and distributed caching. This blog delves into Hybrid Caching in .NET 9.0, covering its benefits, implementation, and best practices.
What is Hybrid Caching?
Hybrid Caching is a strategy that combines in-memory and distributed caching to optimize performance and scalability. It leverages the speed of in-memory caching while maintaining persistence and scalability through distributed caching solutions like Redis or SQL Server.
Advantages of Hybrid Caching
- Enhanced Performance: Stores frequently accessed data in memory, minimizing response time.
- Scalability: Ensures data consistency across multiple servers through distributed caching.
- Increased Reliability: Implements failover mechanisms to prevent data loss during system failures.
- Cost Efficiency: Reduces dependence on costly database queries, optimizing resource usage.
- Prevention of Cache Stampede: Efficiently manages cache misses to avoid multiple simultaneous database requests.
Cache Stampede. Issue and Solution
What is Cache Stampede?
A cache stampede occurs when multiple concurrent requests attempt to access the same data from the cache but encounter a cache miss. As a result, all requests simultaneously query the original data source, potentially overwhelming it with excessive load.
How Hybrid Caching Prevents Cache Stampede?
Hybrid Caching in .NET 9.0 helps mitigate cache stampede through:
- Locking Mechanisms: Ensuring only one request retrieves data from the source while others wait.
- Stale Data Serving: Temporarily providing slightly expired data while fresh data is fetched in the background.
- Preemptive Refreshing: Updating cache entries before expiration to minimize sudden cache misses.
Implementing Hybrid Caching in .NET 9.0
The source code can be downloaded from GitHub
Hybrid Caching in .NET 9.0 can be implemented using Microsoft.Extensions.Caching.Hybrid is currently an experimental package that is under preview. Below is a step-by-step guide to integrating it into your application:
1. Install the Required Package
Begin by installing the required NuGet package:
Microsoft.Extensions.Caching.StackExchangeRedis
Microsoft.Extensions.Caching.Hybrid

Note: I installed the Redis from this gitHub
2. Configure Caching Service
Modify the Program.cs file to configure Hybrid Caching.
using HybridCachingIndotNET9._0;
using Microsoft.Extensions.Caching.Hybrid;
using Scalar.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddSingleton<HybridCacheService>();
builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration = "your redis connection string";
options.InstanceName = "HybridCache";
});
#pragma warning disable EXTEXP0018 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
builder.Services.AddHybridCache(options =>
{
// Maximum size of cached items
options.MaximumPayloadBytes = 1024 * 1024 * 10; // 10MB
options.MaximumKeyLength = 512;
// Default timeouts
options.DefaultEntryOptions = new HybridCacheEntryOptions
{
Expiration = TimeSpan.FromMinutes(30),
LocalCacheExpiration = TimeSpan.FromMinutes(30)
};
});
#pragma warning restore EXTEXP0018 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();


Join the conversation! Your thoughts help the community grow.