I have the following problem, I am implementing a queue abstraction with RavenDB in a stateless REST server.
Suppose I have 2 REST calls A and B
When call A is happening I query the queue for the last item ( item A ) and give it to call A.
If call B is called at the same time as call A – which can happen with REST calls, I need to prevent the program from giving call B the same item as A meaning item A should be “locked” by the call A.
A standard multi-threaded protection here would be a simple lock, how do I translate this idea to my situation with RavenDB and REST?
P.S I use Nancy for the REST server
That should not be too hard unless I missed something:
Introduce a flag (boolean property) on your items, e.g. “Processed”.
Inside your action, open up a new RavenDB session and enable optimistic concurrency on it (
DocumentSession.Advanced .UseOptimisticConcurrency).Get the next unprocessed item and then immediately update its processed flag to true.
Call
.SaveChangeson your session -> if that succeeds (you don’t get aConcurrencyException), you can safely return the item as the result of the request. If not, load the next item.