Normally we will use the Distributed Cache. However, if we make too many requests at the same time (I/O exhausted) or the size of data is huge (>100kb → timeout).
If we use InMemory, when data is in one instance, we have to clear (and refresh) data in other instances by using the pub/sub mechanism.
Redis only have geo-replication feature when we scale it up to a Premium package in Azure. But it will cost a lot! What we should do about this is that, we should look back at how we use cache and implement 2 level caching (meaning using both In Memory and Distributed).
The best solution and strategy for this that. when we wanted to store an item into the cache, we would also store it in an in-memory cache. The in-memory only lives for 1 second. When we retrieve the cached item, we will take it from the in-memory first. If the in-memory is expired, we will take it from redis, store in the in-memory and return to the client.
The diagrams will be follow
So when we implement we should wrap IDistributedCache and IMemoryCache in one service named CacheService.In this service we should check in Memory Cache first then Redis Cache.
0 Comments
Post a Comment