8

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?

leora
  • 188,729
  • 360
  • 878
  • 1,366

2 Answers2

8

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • thanks for the links as they are great articles. I still have a few questions. 1. Where should this cache run (on one of the web servers or do i now need another server for this caching). If it has to be another machine, isn't this going to be slower given i now need a network hop to reach the cache? – leora Jul 06 '11 at 03:35
  • @ooo, ideally it should be another machines with lots of memory. You could also use your web servers but in general it is not recommended. – Darin Dimitrov Jul 06 '11 at 04:43
  • what about the point about having an extra network hop for getting to the cache (versus being on the same machine). Also, should that new machine just be dedicated to being a cache machine. – leora Jul 06 '11 at 05:02
  • @ooo, indeed the extra network hop might be a little slower compared to storing it on the same servers. But bearing in mind the quantity memory you would consume on your actual web servers if you used them for caching and the slowness of your actual applications that would result from this, this extra network hop is like speed of light. So, yes, absolutely dedicate separate caching servers when you go distributed. If it is for financial reasons you could start with the same servers but you should think to offload the caching when day. – Darin Dimitrov Jul 06 '11 at 05:05
  • Do you know if there is any good comparison (ease of setup, performance, etc) for each of the providers – leora Jul 06 '11 at 11:01
  • +1 for memcached, we use http://www.couchbase.com/products-and-services/membase-server for a server in our environment – DanP Jan 20 '12 at 13:48
1

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

Jaguar
  • 5,929
  • 34
  • 48