Enhancing the RavenDB consistency model
Join the DZone community and get the full member experience.Join For Free
from the get go, we have built ravendb to be as easy to use as possible. quite explicitly, wherever possible, we aimed to make it as simple as possible for someone familiar with .net. the fun part is sometimes looking at people’s expectations and answering them is a pleasure, such as this one:
the reasons for this exchange being amusing is that ravendb doesn’t have the concept of expensive queries. for the most parts, queries are merely scanning through an already computed index. ravendb does no computation and very little work to answer your queries, so the notion of an expensive query is meaningless, there aren’t any.
that said, this behavior does comes at a cost, it means that we have to do the work of computing the indexes at some stage, and in our case, we have chosen to do so on the background. what that means, in turn, is the whole notion of queries potentially returning stale results. as it turned out, there is a actually a surprisingly few cases where you can’t tolerate some staleness in your application, and that makes ravendb highly suited for a large number of scenarios.
there is, however, one common scenario that is annoying, the create –> list flow. as a user, i want to complete the create screen:
and then i want to see the list screen:
and naturally i expect to see the just created item in the list. that is the one scenario where people usually run into ravendb’s consistency model, and it brought a few complaints every now and then. if the timing is just wrong, you may be able to issue the next request before the addition to the database had the chance to index, resulting in a missing update.
usually, the advice was to add a waitfornonstaleresultsasofnow(), which resolved this issue, so we didn’t really consider this any further. it was just one of those things that you had to understand about the way ravendb worked.
recently, however, we got a bug report for this exact scenario, and the user couldn’t just use waitfornonstaleresultsasofnow(), or to be rather more accurate, he was already using it, but it wasn’t working. we eventually figured out that the problem was clock synchronization between the server and client computers. that forced us to re-consider how to approach this. after looking at several alternatives, we ended up creating a new consistency model for ravendb.
in this consistency model, we are basically ensuring a very simple metric, “you can query anything that you have written”, instead of relying on the time being the same everywhere, we have decided to use the etags that are being reported back to the server. using this approach, you can basically ignore the difference between ravendb and the traditional relational database, since it will behave (externally), in much the same fashion.
you can enable this mode on a per query basis:
or you can enable this for all queries:
i am pretty proud of this feature, since it simplify a lot for most users, and it provides a very effective (and simple) way to approach consistency. this is especially true if we are talking about multiple clients working on the same database (which is the case in the issue that was raised).
whenever each client is writing, they will have to wait for their changes to be indexed, but they won’t have to wait for the changes from any other client. that matches the user’s expectation, and also allow ravendb to answer most queries without doing any waiting whatsoever.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.