NCache as NHibernate Level 2 Cache for Scalability
NHibernate is a very popular object-relational mapping engine for .NET. It speeds up your development by relieving you from developing a significant portion of relational data persistence code.
NHibernate is frequently used in high transaction applications where performance and scalability are critical. To handle this, NHibernate provides built-in First Level (L1) cache that is in-process and tied to a user’s “Session Object”. NHibernate also provides a Second Level (L2) cache framework at “Session Factory” level (meaning at application level) where you can plug-in third party caches.
NCache is an extremely fast and scalable in-memory distributed cache for .NET applications. It lets you cache application data, reduce those expensive database trips, and improve your application performance and scalability. And, NCache provides an NHibernate Level 2 (L2) cache implementation. Please see details of NCache on NCache Homepage
Why Use NCache as NHibernate L2 Cache for Multi-Server Apps?
If your NHibernate application is running in a multi-server environment then you need distributed cache as a NHibernate L2 cache provider. And, NCache is an ideal NHibernate L2 Cache for multi-server configurations and you should use it for these reasons:
NCache synchronizes across servers: NCache ensures that its cache is consistent across multiple servers and all cache updates are synchronized correctly so no data integrity issues arise.
Cache size can grow: NCache pools memory of all cache servers together. So, you can grow the cache size by simply adding more servers to the cache cluster.
Linearly scale to handle extreme transaction loads: Database quickly becomes a bottleneck as you try to increase transaction load. NCache allows you to add more servers to the cache cluster as you need more transaction load so the cache is never a bottleneck.
Cache reliability through data replication: NCache lets you intelligently replicate cached data across multiple servers and without compromising performance. So, you won't lose any data even if a cache server goes down.
Use NCache as NHibernate L2 Cache without Programming
The best thing about using NCache with NHibernate is that there is no programming required on your part. You simply modify your app.config and specify NCache as your L2 cache provider. Here is an example of app.config changes:
By plugging in NCache NHibernate L2 cache provider through app.config changes, you are going to gain an enterprise level distributed cache for your application. Here are some features NCache provides for NHibernate apps.
Synchronize cache with database: Some data that is in the cache may be changed in the database without your application’s knowledge or involvement. And, for this data, you can specify the corresponding classes for database synchronization feature of NCache. This allows NCache to then connect with the database, monitor data changes, and then update the cache automatically to ensure that data in the cache is always synchronized with the database. NCache provides SqlDependency for SQL Server, OracleDependency for Oracle, and DbDependency for any OLEDB compliant databases.
Use Async operations: Many NCache operations have their asynchronous equivalent. These operations are useful when your application does not want to wait for the cache to be updated on multiple servers and wants the control returned to it immediately and the cache update to be done in the background. You can enable this feature with NHibernate as well.
Priority for cache items: NCache provides priority based evictions where cached items are evicted based on their priority. So, higher priority items are evicted in the end and in fact you can specify a priority that does not get evicted at all. You can enabled this feature through app.config changes for NHiberante.
Absolute Expiration: Absolute expiration in NCache is specified separately for each cached item and is a date-time value when NCache automatically expires the item. For NHibernate, NCache asks you to specify an “interval” value and then uses “Now() + interval” formulate to calculate the date-time value for absolute expiration.
Sliding Expiration: Sliding expiration in NCache is specified separately for each cached item and is an interval value. NCache expires the cached item if it has neither been fetched nor updated for this interval. You can specify this interval through app.config for NHiberante.
Dynamic Compact Serialization: Whenever any .NET object is cached in an out-of-process or distributed cache, it must first be serialized. And, regular .NET serialization is slow because it uses .NET Reflection at runtime. NCache provides a way for you to register your classes with NCache and NCache generates serialization code when your application connects to the cache. NCache then compiles this code in memory and uses it for serialization. This is almost 10 times faster than regular .NET serialization. And, you can use this feature with NHibernate. Just register your NHibernate Entity classes with NCache.
Use NCache Directly from NHibernate Apps
Although, using NCache as NHibernate L2 Cache is very quick and easy, it only provides you a limited set of NCache features. The reason for this is that NCache is bound by NHibernate L2 Cache provider specification that is based on least common denominator principle. There are numerous NCache features that you are unable to use if you decide to use NCache as NHibernate L2 Cache Provider.
The alternative for you is to use NCache directly from within your NHibernate application by making NCache API calls like all other non-NHibernate .NET applications do. Although, there is a small programming effort to take this path, you might decide that the benefits outweigh the extra programming effort.
NCache Features Missed in NHibernate L2 Cache
Below is a list of features you would miss if you used it as NHibernate L2 Cache versus if you used NCache directly from your application.
Groups and subgroups
Object Query Language (OQL) and LINQ based searches
Lock and Unlock of cached items
Cache item versioning
Read-through, Write-through, and Write-behind
Item based (update and remove)
General purpose (add, update, remove)
Runtime Data Sharing (.NET-to-.NET and .NET-to-Java)