Milan Jovanović explains why .NET 9's HybridCache fails to propagate cache invalidation across multiple nodes and demonstrates a Redis Pub/Sub fix.
Want to master Clean Architecture? Go here: bit.ly/3PupkOJ
Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
Get the source code here: patreon.com/milanjovanovic
In this video, I reveal a critical issue with the newly released stable version of HybridCache library and demonstrate how to fix it. The problem occurs in multi-node environments where cache invalidation fails to propagate to all instances.
What you'll learn:
- Why HybridCache doesn't work properly in distributed systems
- How cache invalidation only affects the current node and level 2 cache (Redis)
- Step-by-step implementation of a Redis Pub/Sub solution to synchronize cache invalidation
- Creating a background service to listen for cache invalidation events
- Alternative solutions if you prefer not to implement this yourself
I demonstrate the issue using a clear diagram showing how when one server invalidates a cache entry, the other servers' level 1 caches still contain stale data. The solution leverages Redis' publish/subscribe mechanism to broadcast invalidation messages to all application nodes.
If you don't want to implement this yourself, you can either:
1. Set a very short local cache expiration time in HybridCache configuration
2. Use FusionCache as an alternative with its built-in Redis backplane support
Complete with code samples and a demonstration using multiple application instances with .NET Aspire.
Check out my courses:
milanjovanovic.tech/courses
Read my Blog here:
milanjovanovic.tech/blog
Join my weekly .NET newsletter:
milanjovanovic.tech
Chapters
HybridCache: The Problem
Drawing some boxes
Solving the problem - Option 1
Solving the problem - Option 2 (FusionCache)
Solving the problem - Option 3 (Channels)
Why it's here
2 people recommend this
What people say. Recommended as a practical guide to fixing L1 cache synchronization issues when using HybridCache in multi-node environments.
j
u/jespersoein r/dotnet • Recommends
On “Hybrid cache invalidate L1 cache?”
If you’re using Redis as L2, this solution works great: link to this video
With other L2 systems they might have a similar broadcast functionality. If not, it shouldn’t be too hard to implement using something like MQTT.