Why Distributed Cache as NHibernate Second Level Cache?

NHibernate is a very popular object-relational mapping (ORM) solution for .NET applications because it simplifies your database programming. As a result, many applications using NHibernate are high traffic in nature and therefore face scalability bottlenecks in the database.

To tackle this, NHibernate provides a caching infrastructure so that applications can use an in-memory cache store instead and database is not exhausted anymore due to such high request load.  NHibernate caching includes first level cache and second level cache.

NHibernate First Level Cache and its Limitations

NHibernate First Level (1st level) Cache provides you a basic standalone (in-proc) cache which is associated with Session object and is limited to the current session only. 1st level cache is used by default to reduce the number of SQL queries on the database per session. But, there are a lot of limitations with this 1st level cache as follows:

  • Each process has its own 1st level cache that is not synchronized with other 1st level caches so data integrity cannot be maintained.
  • Cache size is limited to the process memory and cannot scale.
  • Worker process recycling causes cache to be flushed. Then, you have to reload it which reduces your application performance.

Solution: A Distributed Cache for NHibernate

NHibernate Second Level Cache exists at the Session Factory level, which means multiple user sessions can access a shared cache. Additionally, NHibernate 2nd level cache has a plug-able architecture so you can plug-in a third party distributed cache to it without any programming. NCache has implemented NHibernate Second Level Cache Provider and you can use it as your distributed cache for NHibernate without any code changes.

Following example shows you how to use NCache in your NHibernate application as its 2nd level cache:

        <property name="cache.provider_class">Alachisoft.NCache.Integrations.NHibernate.Cache.NCacheProvider, Alachisoft.NCache.Integrations.NHibernate.Cache
        <property name="proxyfactory.factory_class"> NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate </property>
        <property name="cache.use_second_level_cache">true

        <region name="AbsoluteExpirationRegion" cache-name="myCache" priority="Default" expiration-type="sliding" expiration-period="180" use-async="false" />
        <region name="default" cache-name="myCache" priority="default" expiration-type="none" expiration-period="0" />

Here are some important benefits of NCache as NHibernate 2nd level cache:

  • NCache synchronizes across processes & servers: NCache is a shared cache across multiple processes and servers. This ensures that cache is always consistent across multiple servers and all cache updates are synchronized correctly so no data integrity issues arise.
  • Scalable cache size: NCache pools memory of all cache servers together. So, you can grow the cache size by simply adding more servers to the cache cluster. This means your cache can grow to 100′s of gigabytes and even terabytes.
  • Application process recycling does not affect the cache: Your application processes now become stateless as all the data is kept in an out-proc distributed cache. So, application process recycling has no impact on the cache.
  • Linear scalability to handle larger transaction loads: You can handle larger transaction loads without worrying about your database becoming a bottleneck. This is because the distributed cache scales linearly and reduces your database traffic by as much as 90%.
  • Use Client Cache to store data In-proc: NCache provides support of client cache, which is a synchronized local cache and can also run in-proc within your worker process. You can use it to attain more performance within your NHibernate applications.

As you can see, a distributed cache like NCache enables you to use NHibernate and still run your application in a multi-server configuration. You can also scale your application and handle high transaction loads by using NCache as NHibernate Second Level Cache.

Download a fully working 60-day trial of NCache Enterprise and try it out for yourself.

This entry was posted in 3rd party integrations, Distributed Cache, NHibernate and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

6 + = fourteen

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>