Ayende @ Rahien

It's a girl

A guide into OR/M implementation challenges: The Session Level Cache

Continuing to shadow Davy’s series about building your own DAL, this post is about the Session Level Cache.

The session cache (first level cache in NHibernate terms) exists to support one main scenario, in a single session, a row in the database is represented by a single instance. That means that the session needs to track all the instances that it loads and be able to search through them. Davy does a good job covering how it is used, and the implementation is quite similar to the way it is done in NHibernate.

Davy’s implementation is to use a nested dictionary to hold instances per entity type. This is done mainly to support  RemoveAllInstancesOf<TEntity>(), a method that is unique to Day’s DSL. The reasoning for that method are interesting:

When a user executes a custom DELETE statement, there is no way for us to know which entities were actually removed from the database. But if any of those deleted entities happen to remain in the SessionLevelCache, this could lead to buggy application code whenever a piece of code tries to retrieve a specific entity which has already been removed from the database, but is still present in the SessionLevelCache. In order to deal with this scenario, the SessionLevelCache has a ClearAll and a RemoveAllInstancesOf method which you can use from your application code to either clear the entire SessionLevelCache, or to remove all instances of a specific entity type from the cache.

Personally, I think this is using an ICBM to crack eggshells. But I am probably being unfair. NHibernate has much the same issue, if you issue a Delete via SQL or HQL queries, NHibernate doesn’t have a way to track what was actually delete and deal with it. With NHibernate, it doesn’t tend to be a problem for the session level cache, mostly because of usage habits than anything else. The session used to do so rarely have to deal with entities loaded that were deleted by the query issue (and if it does, the user needs to handle that by calling Evict() on all the objects manually). NHibernate doesn’t try to support this scenario explicitly for the session cache. It does support this very feature for the second level cache.

It make sense, though. With NHibernate, in the vast majority of cases deleting is going to be done using NHibernate itself, rather than a special query. With Davy’s DAL, the usage of SQL queries for deletes is going to be much higher.

Another interesting point in Davy’s post is the handling of queries:

When a custom query is executed, or when all instances are retrieved, there is no way for us to exclude already cached entity instances from the result of the query. Well, theoretically speaking you could attempt to do this by adding a clause to the WHERE statement of each query that would prevent cached entities from being loaded. But then you might have to add the cached entity instances to the resulting list of entities anyways if they would otherwise satisfy the other query conditions. Obviously, trying to get this right is simply put insane and i don’t think there’s any DAL or ORM that actually does this (even if there was, i can’t really imagine any of them getting this right in every corner case that will pop up).

So a good compromise is to simply check for the existence of a specific instance in the cache before hydrating a new instance. If it is there, we return it from the cache and we skip the hydration for that database record. In this way, we avoid having to modify the original query, and while we could potentially return a few records that we already have in memory, at least we will be sure that our users will always have the same reference for any particular database record.

This is more or less how NHibernate operates, and for much the same reasoning. But there is a small twist. In order to ensure query coherency between the data base queries and in memory entities, NHibernate will optionally try to flush all the items in the session level cache that have been changed that may be affected by the query. A more detailed description of this can be found here, Davy’s DAL doesn’t do automatic change tracking, so this is not a feature that can be easily added with this prerequisite.

Comments

Jamie Penney
08/27/2009 03:40 AM by
Jamie Penney

Hi Ayende,

Firstly, I am enjoying your commentary on this series of posts. It is informative to read about the design process that has gone into NHibernate by contrasting it with a completely new ORM.

However, for this post do you think you could fix up the indentation or somehow indicate which parts are your thoughts, and which are from Davy's article? I found it a little confusing which was which.

Cheers,

Jamie

Stephen
08/28/2009 08:47 PM by
Stephen

Couldn't you run the same criteria / linq against the cache when deleting entities?

Ayende Rahien
08/28/2009 08:57 PM by
Ayende Rahien

Stephen,

That would require me to do an O(N) operation against the cache for each delete.

And that even assumes that I can express the delete in memory

Stephen
08/28/2009 09:04 PM by
Stephen

I understand, but lets say you could express the delete in memory surely the cache could be made to be quite sophisticated and handle deletes more sql for example.

Ayende Rahien
08/28/2009 09:25 PM by
Ayende Rahien

Stephen,

I think that you drastically underestimate the effort required. By seven orders of magnitude, at least

Stephen
08/28/2009 10:17 PM by
Stephen

I should send you a patch for that? ;)

Comments have been closed on this topic.