Introducing HybridCache in .NET 9

This blog is presented as a part of C# Advent 2024. Follow the link to check out the rest of the excellent C# and .NET content coming out in 2 blogs per day between December 1 - 25.

Caching plays a crucial role in performance in web applications by reducing strain on backend systems. The new HybridCache library, available in preview in .NET 9, simplifies caching and addresses some limitations of previous approaches, making it easier for developers to implement efficient, reliable caching solutions.

Why a New Cache Library?

Existing caching options, like IMemoryCache and IDistributedCache, serve different purposes but come with challenges. IMemoryCache is fast but limited to in-process memory, which can lead to issues like cache inconsistency or data loss on application restarts. IDistributedCache solves these issues with external storage solutions like Redis, but it adds latency, serialization complexity, and dependency on external systems.

HybridCache combines the strengths of both approaches, providing:

  • Seamless in-process and distributed caching.
  • Automatic serialization and deserialization.
  • Stampede protection to handle multiple simultaneous requests for the same data.

Getting Started with HybridCache

Here’s are the steps to integrate HybridCache into your .NET 9 ASP.NET Core projects:

1. Add the Package

Include the Microsoft.Extensions.Caching.Hybrid NuGet package. NOTE: Once released, you will be able to add the package without the –prerelease flag.

dotnet add package Microsoft.Extensions.Caching.Hybrid --prerelease

2. Configure the Cache

Register HybridCache in Program.cs with the AddHybridCache method, where you can also set options like default expiration.

builder.Services.AddHybridCache();

3. Use the Cache

Replace existing caching logic with a HybridCache call:

var data = await hybridCache.GetOrCreateAsync(key, async () => 
{
    return await FetchExpensiveDataAsync();
});

Instead of managing separate Get and Set calls, you use the GetOrCreateAsync method, which checks for cached data and executes a provided callback to populate the cache if needed.

Features of Hybrid Cache

Unified API

HybridCache simplifies caching by providing a single API that works seamlessly for both in-process and out-of-process scenarios. It’s designed to replace IDistributedCache and IMemoryCache without requiring major changes to your code, making it easy to integrate or upgrade your caching strategy.

When an IDistributedCache implementation is configured, HybridCache leverages it as a secondary cache layer, giving you the best of both worlds: the speed of in-memory caching combined with the reliability and persistence of distributed caching.

Stampede Protection

One of the challenges with traditional caching is the risk of a “cache stampede,” where multiple requests hit the system simultaneously when a cached item expires or is missing. This can overwhelm your backend, as each request triggers the same expensive operation to regenerate the data.

HybridCache addresses this by coordinating requests. Only the first caller executes the factory method to repopulate the cache, while others simply wait for the result. This approach reduces unnecessary load and keeps your system running smoothly, even under heavy traffic.

Configurable Serialization

When you register the HybridCache service during startup, you can customize serialization, including type-specific or generalized serializers, such as protobuf or XML. By default, the library handles string and byte[] internally, and uses System.Text.Json for everything else.

builder.Services.AddHybridCache(options =>
    {
        options.DefaultEntryOptions = new HybridCacheEntryOptions
        {
            Expiration = TimeSpan.FromSeconds(10),
            LocalCacheExpiration = TimeSpan.FromSeconds(5)
        };
    }).AddSerializer<SomeProtobufMessage, 
        GoogleProtobufSerializer<SomeProtobufMessage>>();

Looking Ahead

HybridCache is in preview as part of the .NET 9 release, with general availability expected in future minor release soon.

Contact Trailhead to get started implementing HybridCache or to improve the caching in your .NET applications. Our experience in implementing efficient, scalable caching solutions ensures your applications will perform optimally, saving you time and effort while avoiding the most common pitfalls.

Picture of Jonathan "J." Tower

Jonathan "J." Tower

Jonathan, or J. as he's known to friends, is a founding partner of Trailhead Technology Partners, a custom software consulting company with employees across the U.S., Europe, and South America. He is Vice President of the .NET Foundation, a 13-time recipient of the Microsoft MVP award for his work with .NET and AI, and a frequent speaker at software conferences around the world. He doesn’t mind the travel, though, as it allows him to share what he's been learning and also gives him the chance to visit beautiful places like national parks—one of his passions. So far, he's visited 58 of the 63 U.S. national parks. J. is also passionate about building the software community. Over the years, he has served on several non-profit boards, including more than a decade as president of the board for Beer City Code, Western Michigan's largest professional software conference. Outside of work, J. enjoys hiking, reading, photography, and watching all the Best Picture nominees before the Oscars ceremony each year.

Free Consultation

Sign up for a FREE consultation with one of Trailhead's experts.

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Related Blog Posts

We hope you’ve found this to be helpful and are walking away with some new, useful insights. If you want to learn more, here are a couple of related articles that others also usually find to be interesting:

Slow .NET API? Redis Caching May Be the Answer

Just like venturing into the wilderness requires the proper gear, developing REST APIs with ASP.NET Core also requires the right tools and equipment for your application to perform at its best. At Trailhead, Redis caching is one such

Read More

Securing The Nuget Supply Chain: A Case Study

It's not an exaggeration to say that today, most of the code we ship is someone else's code. Sonatype releases an annual State of the Software Supply Chain report. In the 2024 edition of the report they estimate that upward of 90% of softwa

Read More

Our Gear Is Packed and We're Excited to Explore With You

Ready to come with us? 

Together, we can map your company’s software journey and start down the right trails. If you’re set to take the first step, simply fill out our contact form. We’ll be in touch quickly – and you’ll have a partner who is ready to help your company take the next step on its software journey. 

We can’t wait to hear from you! 

Main Contact

This field is for validation purposes and should be left unchanged.

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Thank you for reaching out.

You’ll be getting an email from our team shortly. If you need immediate assistance, please call (616) 371-1037.