VahidN/EFCoreSecondLevelCacheInterceptor

distributed cache inconsistencies

IntranetFactory opened this issue · 0 comments

We used EFCoreSecondLevelCacheInterceptor on a single web server for months without problems. We wanted to scale out by adding more servers and one Redis Sentinel. We quickly recognized problems with records added on one server missing on the others.

We analyzed possible causes and found

  1. that ClearAllCachedEntries is called unexpectedly https://github.com/VahidN/EFCoreSecondLevelCacheInterceptor/blob/master/src/EFCoreSecondLevelCacheInterceptor/EFEasyCachingCoreProvider.cs#L164 wiping out the whole cache

  2. that when multiple servers modify the list of CacheDependencies at the same time in https://github.com/VahidN/EFCoreSecondLevelCacheInterceptor/blob/master/src/EFCoreSecondLevelCacheInterceptor/EFEasyCachingCoreProvider.cs#L91 the resulting list of keys was not consistent on all notes

We created a poc CacheProvider which interacts directly with Redis Sentinel and stores the CacheDependencies in Redis SortedSet:
https://gist.github.com/IntranetFactory/09338b44aa3c6ad7b2cab0284acfe14c

That provider does not need ClearAllCachedEntries and provides consistent distributed caching for us so far.

Is our assumption that https://github.com/VahidN/EFCoreSecondLevelCacheInterceptor/blob/master/src/EFCoreSecondLevelCacheInterceptor/EFEasyCachingCoreProvider.cs#L91 cause inconsistencies with distributed cache when multiple servers modify records in a table at the same time correct?