The Microsoft.Extensions.Caching.Hybrid package (available in .NET 9) provides a hybrid caching mechanism that allows you to combine both in-memory caching (for fast local access) and distributed caching (such as Redis) for microservices running in multiple pods.
How It Works in a Microservices Setup with Multiple Pods? If data is stored in-memory also, each pod uses its own memory, so if data is refreshed or updated in one pod, how to invalidate the in-memory cache in other pods so it do not return the stale data?
As far as I know, when we use in-memory for data storage in multiple pods application, there is no way the data changed in one pod would get changed in other pods also without any custom implementation. But, want to see if there is anything handled for this usecase within the HybridCache library itself which could handle the multiple pods scenario well and data gets refreshed in each pod?
But, want to see if there is anything handled for this usecase within the HybridCache library itself which could handle the multiple pods scenario well and data gets refreshed in each pod?
No, currently there is nothing of the sorts. This is covered in the docs in Cache storage section:
When invalidating cache entries by key or by tags, they are invalidated in the current server and in the secondary out-of-process storage. However, the in-memory cache in other servers isn't affected.
It would be even hard to workaround at the moment since the is no publicily exposed APIs to invalidate (remove) only local cache data since HybridCache.RemoveAsync
will clear both local and distributed cache. But if you are OK with rewriting local one via HybridCache.SetAsync
then you can create some pub/sub (for example on Redis) which will handle "remote" cache invalidation.
This issue is discussed in [API Proposal]: Cache Synchronization for Hybrid Caching in Multi-Node Environments issue at github.
Also you can look into FusionCache
which AFAIK already has such feature.