MongoDB Races, Locks, and Transactions
Aram Hăvărneanu
aram.havarneanu at canonical.com
Wed Jun 27 22:47:33 UTC 2012
Hello team,
Our current mstate code is racy. For example, a service might dissapear
before we try to add a service unit to it. One way of dealing with these
issues is with transactions. As you well know, MongoDB does not have
transactions. While we could implement transactions using multi-phase
commits (http://cookbook.mongodb.org/patterns/perform-two-phase-commits/),
I propose a much simpler mechanism to deal with our problems.
We add an integer version field to our documents. This field is set
to -1 for every relevant document before any operation that involves
multiple queries. After the series of queries is completed, the field is
incremented from the value it had before we set it to -1. MongoDB has
a compare-and-swap primitive that allows us to do this set/increment
atomically.
The version field as described serves two purposes, it acts as a lock,
and it also can be used for cache invalidation.
This is just one idea, I'd like to hear your thoughts about it and/or
about different implementations.
--
Aram Hăvărnanu
More information about the Juju-dev
mailing list