Secondary (L2) Cache with NHibernate – Taking Performance to the Next Level

The fast, reliable and mature O/R mapping features that NHibernate comes with provides a remarkable reduction in the overall development cycle. However, as the application grows in complexity and the data on which they thrive upon becomes complex, the generality of such OR mapping engines may lead to various performance and scalability bottleneck. Applications cannot scale linearly with the increasing transactional requirements. Caching is one approach that provides just the right solution to address such issues. Infect the fact that caching does provide a remarkable performance boost to applications performance is recognized by the makers of NHibernate. NHibernate provides some caching infrastructure. By default it provides a standalone InProc (in process) first-level (L1) cache, or session-level cache. However in addition, , NHibernate also supports a second-level cache to be plugged in to exploit the benefits of a sophisticated enterprise-grade caching infrastructure.

Why is Secondary (L2) Cache Required with NHibernate

NHibernate provides a basic, not-so-sophisticated in-process L1 cache out of box. However, it doesn’t provide features that a caching solution must have to have a notable impact on the application performance. Some of the areas which NHibernate lacks in are:
1.Cannot handle ASP.NET worker process recycling: Worker process recycling destroys and recreates the entire cache which has a negative performance impact on your app.
2.Cannot handle ASP.NET web gardens:ASP.NET app runs in multiple worker processes in a web garden configuration. This creates multiple in-process copies of the cache that are not synchronized with each other and lead to data integrity errors.
3.Cannot handle ASP.NET web farms: ASP.NET apps frequently run in web farms for scalability reasons. A stand-alone cache is unaware of multiple servers and cache instances and is not synchronized. This leads to data integrity problems.
4. Cannot handle application scalability:If your application (ASP.NET, .NET web service, distributed app, grid computing app, or an N-tier app) needs to scale up and run on multiple servers, your cache must be distributed. Otherwise, it would lead to data integrity problems.
5.Cannot handle large cache size: In-process cache means it cannot grow more than what your app process can handle. A distributed cache on the other hand can grow in size through distribution.

Thus it surly requires a secondary level caching infrastructure which can not only address the above mentioned problems but also boost its performance.

NCache – Pluggable Cache for NHibernate

NCache provides just the right solution to problems highlighted above. It is extremely fast, enterprise-grade and highly scalable level-2 distributed cache for NHibernate. It is an in-memory object cache for .NET that lets you improve NHibernate performance and scalability without any code change. Some of the immediate benefits are:

• Scale up with Mirrored, Replicated, Partitioned, and Client Cache topologies
• Software appliance for NHibernate (no code change required)
• Database synchronization of cache thru SqlDependency

NCache acts as a 2nd Level distributed cache that lets you improve your application performance and also scales out very nicely when your application needs to scale out. Below are some of the benefits of using clustered sessions:
1.Handles ASP.NET worker processes well: NCache can be used out-of-process. This means multiple ASP.NET worker processes can share a common out-of-process cache. This also addresses worker process recycling problem because the cache is not affected by it.
2.Handles ASP.NET web farms: NCache creates a clustered cache that can live on multiple servers. This allows your ASP.NET apps and .NET web services to run in web farm configuration without worrying about cache synchronization issues.
3. Highly scalable: You have a rich set of clustering topologies (replicated, partitioned, and client nodes) that allows you to scale up to 100’s of web servers in your farm without compromising on performance.
4.No single point of failure (high availability): Clustering the cache allows you to eliminate all single points of failures from your environment. This makes your application highly available.
5.100% Native .NET: NCache is a 100% native .NET product. This means that you can expect us to stay on top of all additions and changes to .NET as compared to a non-.NET solution.


One Response to Secondary (L2) Cache with NHibernate – Taking Performance to the Next Level

  1. cameron says:

    Very well explained article thanks for sharing this useful information. I wasn’t using any second level cache before but when I started using NHibernate integration for Second Level Cache with NCache I found enhancement in my app performance and scalability.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: