MichaCo / CacheManager

CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.
http://cachemanager.michaco.net
Apache License 2.0
2.33k stars 458 forks source link

Memcache sync in multilevel cache in distributed environment #293

Closed jayeshkalal closed 4 years ago

jayeshkalal commented 4 years ago

Hi,

I m trying to implement multilevel cache with Memcache and Redis in distributed environment. However, there is a use case where I am facing problem with cache syncing.

Assume the setup as below 1 Redis Instance 2 Servers with multilevel caching (S1-S2) with memcache (M1-M2) 2 Users sending request to the servers (U1-U2)

(Please find attached diagram as text visualization is getting distorted.)

                            |----------|
                            |       |
                            | Redis |
                            |       |
                            |__________|
                                |
        ______________________________|________________________________
        |                                               |
    |----------|                                        |----------|
    |       |                                       |       |
    | S1-M1 |                                       | S2-M2 |
    |       |                                       |       |
    |__________|                                        |__________|
        ^                                               ^
        |                                               |
        |                                               |
        |                                               |
        |                                               |
        U1                                              U2

When U1 gets key from redis it is getting stored in M1 cache. and if same is modified by U2 by requesting S2, M1 still holds the old value. At any point before expiration if U1 tries to get data from S1 it always receives old value as the M1 is not updated only M2 and Redis are upto date.

Is there any way to sync the M1 with latest values of redis ????

My multilevel cachehandler registration is like this :

var multiplexer = ConnectionMultiplexer.Connect("localhost:6380");
s => s .WithJsonSerializer() .WithDictionaryHandle() .WithExpiration(ExpirationMode.Absolute, TimeSpan.FromSeconds(6000)) .And .WithRedisConfiguration("redis", multiplexer) .WithRedisCacheHandle("redis"));

Please suggest if I am doing anything wrong......

RED

jayeshkalal commented 4 years ago

No Worries, I forgot to use BackPlane which uses redis Pub/Sub channel.

Thank you!!