Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nhibernate 2nd level caching issues / questions when moving from a single web server to a multiple web server load balanced environment

My previous setup was a single web server and a single database server. I was using nhibernate 2nd level caching to cache stuff to avoid lots of calls going to the database. This has worked great as i was using this this assembly

nhibernate.caches.syscache

and added this code to turn on the second level caching (using the regular syscache provider):

       return configuration
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ApplicationMap>().Conventions.Add(typeof(Conventions)))
            .ExposeConfiguration(
                c => {
                    c.SetProperty("proxyfactory.factory_class", proxyFactory);
                    c.SetProperty("cache.provider_class", "NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache");
                    c.SetProperty("cache.use_second_level_cache", "true");
                    c.SetProperty("cache.use_query_cache", "true");
                    c.SetProperty("expiration", "86400");
                })
            .BuildSessionFactory();

I have now migrated over to a new environment that has multiple webservers and i am trying to understand the implications of this (I still have a single db server).

Since the cache was being stored on the webserver before, now it would seem like i have 2 parallel caches on each webserver which themselves may not be in sync and may cause out of date updates, etc.

What is the best solution to get the benefits of the caching i had before but also take advantage of the resiliency of the web server load balancing that is provided with this new setup?

like image 897
leora Avatar asked Jul 05 '11 18:07

leora


2 Answers

Ayende blogged about 2nd level cache usage in NHibernate. You will need to use a distributed cache in a web farm scenario. For example SysCache2 (which relies on ASP.NET cache and which could be configured to use a distributed provider) or MemCache. Here's an article illustrating how you could configure memcached.

like image 57
Darin Dimitrov Avatar answered Nov 19 '22 02:11

Darin Dimitrov


Well i've used memcached and it works like a charm. You may have to tweak the size of the cache but other than that it's hassle-free. Setup a memached node on each webserver node and maintain that configuration as you add up nodes

like image 22
Jaguar Avatar answered Nov 19 '22 01:11

Jaguar