Ok,
(no actual code yet, I don't have time to code right now :( )
I have a project currently using an RDBMS and I'd like to port it to
CouchDB. One of the things I do is lock a table, choose a free
resource from a query on a static table and the session list, assign
the resource to a new session and unlock the table.
How would I be able to do the same thing with CouchDB given that 2
sessions could start at the same time? I do have the advantage that
simultaneous starters would contact the same CouchDB instance.
I was thinking of using sums: make a view that calculates the sum of
resources. A resource record would count as +1 and an in-use record
would be -1.
Then when you reserve a resource, you save the in-use record. After
saving, look up the sum for the resource you reserved. If it's not
equal to 0, then use a stable algorithm to determine who has to
release the resource again.
Would this close the race condition? Note that no documents are
overwritten at reservation time, each reservation doubles as the event
log. When the session clears up, the document that represents it is
updated to release the resource.
Does this work? Is there a better way to do it?
Thanks,
Wout.
|