RavenDB - How stale do you want it

Published on 2010-5-18

The problem

When a query is executed against an index in RavenDB, one of the key aspects of that query is checking the task queue to see if any tasks are currently pending against that index. It is this call that dictates whether IsStale is set as a flag on the return result from that query.

When a call to WaitForNonStaleResults is made in the .NET client, the client simply makes multiple requests against the query until IsStale is found to be false, or until the WaitForNonStaleResults call times out. Thus, the client can wait until there are no more tasks waiting to be executed against the index.

But wait, I hear you cry, what if new tasks are added against those indexes in the meantime? Surely this means that the the results will always be stale on busy servers?

The solution

Thankfully, support is baked into RavenDB to allow for this scenario, so a request can be made to retrieve up to date results as of a specified time called the “cut off”.

This is exposed in the .NET Client as alternatives to the WaitForNoneStaleResults call.

   1:  BlogEntry[] entries = documentSession.Query<BlogEntry>("BlogEntryByCategory")
   2:                          .WaitForNonStaleResultsAsOfNow(TimeSpan.FromSeconds(30))
   3:                          .Where("Category:RavenDb")
   4:                          .ToArray();

This particular version of the call will wait at the very most 30 seconds for non-stale data to be available as of the time the method was invoked. Thus, any data added after the method is invoked will not count towards whether the results count as being stale or not.

   1:  BlogEntry[] entries = documentSession.Query<BlogEntry>("BlogEntryByCategory")
   2:                          .WaitForNonStaleResultsAsOf(DateTime.Now.Subtract(TimeSpan.FromMinutes(10)), TimeSpan.FromSeconds(30))
   3:                          .Where("Category:RavenDb")
   4:                          .ToArray();

A similar strategy has been used here, only we don’t care about anything added after about 10 minutes ago.

Summary

You probably still wouldn’t use this when requesting views of your data for displaying on the front page of a website, but this can be used for processes which do care about non-stale data and are willing to wait for it.

2020 © Rob Ashton. ALL Rights Reserved.