ZiggyCreatures / FusionCache

FusionCache is an easy to use, fast and robust hybrid cache with advanced resiliency features.
MIT License
2.95k stars 145 forks source link

Support for External Cache Invalidation Across Microservices #452

Open AlirezaEiji191379 opened 2 months ago

AlirezaEiji191379 commented 2 months ago

Hi, first of all, thanks for this awesome library!

I’m working on a system that has two microservices: A and B.

Here’s the challenge:

When data is updated or deleted in microservice A, we want the cached version in microservice B to be invalidated as well.

Since both services are using FusionCache (even though A doesn’t cache this entity), is there a way to trigger a cache invalidation in B via a Backplane mechanism (like Redis or anything else), from A?

I was thinking of using Redis Pub/Sub or a custom webhook from A to B to manually call .Remove() in B — but I wonder if FusionCache’s Backplane could help automatically propagate invalidations across services?

Would love your input on the best way to handle this with FusionCache. Thanks a lot 🙏

jodydonetti commented 2 months ago

Hi @AlirezaEiji191379 , thanks I'm glad you're linking FusionCache!

Regarding your question: are you using FusionCache with only L1 or also with L2?

AlirezaEiji191379 commented 2 months ago

Hi @AlirezaEiji191379 , thanks I'm glad you're linking FusionCache!

Regarding your question: are you using FusionCache with only L1 or also with L2?

using L1 and L2

jodydonetti commented 2 months ago

Hi @AlirezaEiji191379

Here’s the challenge:

When data is updated or deleted in microservice A, we want the cached version in microservice B to be invalidated as well.

Since both services are using FusionCache (even though A doesn’t cache this entity), is there a way to trigger a cache invalidation in B via a Backplane mechanism (like Redis or anything else), from A?

Yes! When microservice A update something, instead of not doing anything related to FusionCache, it can do something like this:

cache.Set("Foo", value, options => options.SetSkipMemoryCache());

This will tell FusionCache to update "foo" BUT skip L1 (memory cache) while doing so (since you don't need to cache it there): this in turn will update L2 (distributed cache) and send a backplane notification to all the other nodes.

When microservice B receives the notification, it will do what is described here, meaning:

Hope this helps, let me know if you try this.