Nov 13, 2008 / nhibernate ~ caching
NHibernate in Action - Impressive chapter on caching

I'm currently editing Chapter 5 for NHibernate in Action (we hope to be in print in a 2-4 weeks, so this is the last time!).

Chapter 5 is about transactions and caching. There's a lot of information on the 2nd level cache, including process-scoped and cluster-scoped caches for distributed scenarios.

Despite that I've reviewed/edited this chapter a few times, I've not used NHibernate's 2nd level cache until recently. So I'm reviewing and learning at the same time!

Hats off to the original authors and Pierre Henri for writing such a strong chapter. The case-study style scenarios are particularly useful for working out optimal caching strategies.

For example, I just learnt that an app I'm working on could benefit from the nonstric-read-write cache usage:

<cache usage="nonstrict-read-write"/>

....instead of...

<cache usage="read-write"/>

Nonstrict-read-write means that when you save an object to the database, NHibernate will invalidate it in the cache too, but it does it asynchronously meaning there's no guarantee of exactly when it's removed from the cache. Of course, this introduces the chance that another process could read stale data from the cache (albeit only a few milliseconds stale!). This is a tradeoff, and the benefit is performance - because it's asynchronous NHibernate won't wait for both DB and Cache to confirm the changes.

I'm looking forward to benchmarking the various caching options to see just how much my app can gain.

Right now my 2nd level cache is provided by the ASP.NET Session Cache, but in a few weeks I'm hoping to switch to a distributed cluster of Velocity caches. That will be fun!...


You may also like...