2

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you please share a reference of FusionCache with this feature implemented? I tried in local machine with Docker Desktop setup with multiple pods, but in that also only the current server cache was invalidated and not from all the pods? I want to check if I missed anything because it didn't worked for me with FusionCache as well.
@MaitriDave - "Can you please share a reference of FusionCache with this feature implemented?" - the link to the FusionCache github is already there and as far as I understand the feature is called "Backplane". P.S. If answer works for you - feel free to accept and upvote it =)
Hi @MaitriDave , FusionCache creator here. I just discovered this thread, and even if some time passed I'll add my notes for future readers: as mentioned by GuruStron , in FusionCache you should use the Backplane. You can use FusionCache directly, or if you prefer it as an implementation of HybridCache via an adapter (and yes, the Backplane will work even when using it via the HybridCache adapter). Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.