nhibernate/NHibernate-Caches

Best cache for cluster production environment

robertovaldesperez opened this issue · 3 comments

Hi guys, I have an application that I would like to add cache to, what is the best cache to use in a cluster production environment?

There is no such thing as "best" overall. There is more or less suitable for your specific case, and it is hard to tell what is it for you.

Read the documentation here.

First you have to know if the data you intend to put in cache needs to be in sync among your cluster nodes. If no, better go for an in memory non distributed cache. If your cached data is readonly, it is likely a good choice. Personally, I use RtMemoryCache for this.

If the cached data can be updated by some node and the other nodes need to have the updated data without waiting for cache expiration, then better use a distributed cache, or no cache at all.
If the cached data is frequently updated or frequently missing in cache, the caching layer will cause a lot of additional IO, so additional delays, which may never be compensated by the cache hit cases.
This repository supplies providers for Memcache, Redis or SqlServer. If going for Redis, better use StackExchangeRedis. CoreDistributedCache is a generic solution less capable about Redis than StackExchangeRedis.

If the cached data can be updated by some other processes and you need your cache to be invalidated in such case, there is the SysCache2 cache which can handle that with Microsoft SqlServer, by getting notified directly by the SqlServer. But it is available only for the .Net Framework (v4.8 max). This cache is mainly a memory cache but with which you can avoid staled cache data thanks to invalidation notifications from the Sql Server.

hi @fredericDelaporte
thanks for your answer.
In my project I have a bit of everything, a database in which the data is updated infrequently and another in which there are tables that are updated very frequently and others that not so much and some once a year. Could I configure RtMemoryCache for a single database table?

Only entities and collections mapped as cacheable may be cached, so yes, if you want to use only one cache provider.

But if you want to use different cache providers for different entities mapped in the same session factory, you cannot. Instead you may use different cache regions with different caching strategies, according to the provider you choose. I will not get into details.

If you use many session factories, each session factory can use a different cache provider.