Ayende @ Rahien

Refunds available at head office

Aggressive caching: Pacified

RavenDB’s aggressive caching allows RavenDB Clients to skip going to the server and  serve requests directly off the client cache. That means that you can answer queries very  quickly, because you never even have to leave your process memory space.

The downside to that was, of course, that you might be showing information that have changed behind your back.

That is why we usually wrote aggressive caching code like this:

using (session.Advanced.DocumentStore.AggressivelyCacheFor(TimeSpan.FromMinutes(5)))
{
   var user =  session.Load<User>("users/1");
   Console.WriteLine(user.Name);
}

We gave it some duration in which it was okay to skip going to the server. So we had a maximum of 5 minutes when we had the cached information in place.

That was nice, but it was awkward. And not really nice thing to do in general. In particular, it meant that you had to wait 5 minutes for things to actually show up in the application. That can be... frustrating, because it looks like the system isn’t really doing something. On the other hand, it also means that you still have to query the server when the duration is over, and by necessity, the durations are relatively short.

We decided to change that. Now, when you are using aggressive caching, RavenDB will automatically subscribe to changes from the server. When that happens, we will be able to use the notifications to know when we need to re-check on the server.

That means that you can set an aggressive cache duration of much longer period, and that we will know, automatically and without you needing to do anything.

It is a small touch, but an important one. Things just get better Smile.

Tags:

Posted By: Ayende Rahien

Published at

Originally posted at

Comments

dario
03/18/2013 10:23 AM by
dario

Why there is no 'evict'?

Harry McIntyre
03/18/2013 10:35 AM by
Harry McIntyre

Now that sounds good. If the client is disconnected, is the cache cleared automatically? (i.e. is there any way the client could miss the notification?)

Ayende Rahien
03/18/2013 10:42 AM by
Ayende Rahien

Harry, No, the client will try to reconnect automatically. But it is a possibility, that is why it is still an active decision on your part.

Māris Krivtežs
03/18/2013 11:00 AM by
Māris Krivtežs

If server sends notifications about changes, why cache duration is needed? If cache is expired application will be notified.

Ayende Rahien
03/18/2013 11:03 AM by
Ayende Rahien

Māris, For cases where you have actual disconnection. Note that we also have just AggressiveCache() method.

Harry McIntyre
03/18/2013 11:28 AM by
Harry McIntyre

Is there some way you can hook into the disconnection event so you could opt in to invalidate-on-disconnect?

Ayende Rahien
03/18/2013 11:29 AM by
Ayende Rahien

Harry, Yes, that is available in the API.

Darius
03/18/2013 01:15 PM by
Darius

Lol, why the word aggressive? :) It doesn't add anything, it's still just normal caching. I'd go with simpler using (session.Advanced.DocumentStore.CacheFor.Minutes(5))

or smth similar.

David Boike
03/18/2013 01:39 PM by
David Boike

@Darius Because Raven also includes automatic caching of all documents. If you request a document that already exists in cache, Raven will tell the server "I would like this document, but I already have version X in my cache" using the standard HTTP mechanics of passing the last modified date. If the document has not been modified, the server will send back a 304 Not Modified, and the client will give you back the cached version. So that is the "normal" caching, and the "aggressive" caching is the additional choice to skip talking to the server at all.

@Ayende, this is AWESOME by the way, thank you!

John Clayton
03/18/2013 05:31 PM by
John Clayton

So is the API for using aggressive caching still the same with the exception of being able to use a longer timeout?

configurator
03/19/2013 12:16 AM by
configurator

Awesome feature. It really sounds like it should be smarter about disconnects, with at least an option to automatically clear the cache - or possibly a smarter way to find out what needs to be cleared.

But really, awesome feature.

Catalin Pop
03/19/2013 09:17 AM by
Catalin Pop

One definition for "aggressive" is : "deliberately unfriendly behavior involving forcing, constraining or overruling"

There's no overruling here being done by the client if the server can invalidate the caches, which means "Aggressive" doesn't seem to be appropriate name in this case.

My 2c

Ayende Rahien
03/19/2013 09:18 AM by
Ayende Rahien

Catalin, But the aggressive caching ISN'T talking to the server.

Shifatullah
03/27/2013 05:07 AM by
Shifatullah

Since the functionality being called through store, is cache attached to store or to session? and if we close the session, will it clear the cache?

Ayende Rahien
03/27/2013 08:16 AM by
Ayende Rahien

Shifatullah, This is attached to the store, not the session. Session are very short lived, and caching stuff on them makes very little sense.

Jean Pierre
03/28/2013 06:56 PM by
Jean Pierre

I am working on an application using RavenDB, using the latest packages from nuget. I haven't seen this functionality working. Is there anything special that needs to be set for this to work? Thanks.

Ayende Rahien
03/28/2013 09:01 PM by
Ayende Rahien

Jean, This is only available on 2.5.

Comments have been closed on this topic.