0

At the startup of an application that uses RavenDB, I need to load the full collection of documents of a specific type and loop through them. The number of documents should always be small (< 1000).

I can do this via:

session.Query<MyType>();

but I want to ensure that the results I am getting are immediately consistent.

It seems that this case falls between a Load() and a Query(), since (I think) the query is intended to be eventually consistent, and the load is intended to be immediately consistent?

But in this case, there should be no index involved (no filtering or sorting), so will using Query() be immediately consistent?

Phil Sandler
  • 27,544
  • 21
  • 86
  • 147

1 Answers1

2

Query() is always immediately inconsistent. session.SaveChanges() stores only to the document store. The indexes are always updated asynchronously later, although for the most part very very fast!

This is generally a poor modeling design and a code smell with a document database. Since you are mentioning that is at application startup and a relatively small amount, it sounds like reference information that changes infrequently. Could you enclose all of this in a single document that contains a List<MyType> instead?

Failing that, you can try the LoadStartingWith() command, which is still fully ACID, and give it the prefix that fits all the necessary documents, although up to 1000 is still a lot and I don't know if that method will return ALL results or cut it off at some point.

If you must use Query(), you will have to use one of the variations of .WaitForNonStaleResults() (the other variants take into consideration what you are waiting for rather than requiring all indexing to be complete) to get consistent results, but that should be fairly safe if the documents change infrequently. Although I really do hate using this method in nearly all its forms, preferring to use any of the above more preferable methods.

David Boike
  • 18,545
  • 7
  • 59
  • 94
  • Thanks David. The first option is tough--I had it this way originally, but ran into concurrency issues. I may have to wait for non stale results (which I agree is not ideal). What index does a Query() with no sort and no predicate use? Is there a sort of baseline index for each document type? – Phil Sandler Sep 02 '14 at 21:22
  • The server will build a temporary index for a query that doesn't specify one on the fly. That means the first time you use it, it is going to get stale results, or wait for quite some time while it builds it if you specify a WaitFor. The server will eventually promote a well-used temporary index to a full index on its own, and remove temp indexes that fall into disuse. – David Boike Sep 03 '14 at 16:06