Database Reference
In-Depth Information
The transactional strategy used in all these cases can be described as compensation-
driven . 3 The compensation process in abstract works like this:
Atomically modify a document's state.
1
Perform some unit of work, which may include atomically modifying other
documents.
2
Ensure that the system as a whole (all documents involved) is in a valid state. If
so, mark the transaction complete; otherwise, revert each document to its pre-
transaction state.
3
It's worth noting that the compensation-driven strategy is all but necessary for long-
running and multistep transactions. The whole process of authorizing, shipping, and
canceling an order is just one example. For these cases, even an RDBMS with full trans-
actional semantics must implement a similar strategy.
There may be no getting around certain applications' requirements for multi-
object ACID transactions. But with the right patterns, MongoDB can pull some trans-
actional weight and might support the transactional semantic your application needs.
B.1.8
Locality and precomputation
MongoDB is frequently billed as an analytics database, and plenty of users store analyt-
ics data in MongoDB. A combination of atomic increments and rich documents seems
to work best. For example, here's a document representing total page views for each
day of the month along with the total for the month as a whole. For brevity, the follow-
ing document contains totals only for the first five days of the month:
{ base: "org.mongodb", path: "/",
total: 99234,
days: {
"1": 4500,
"2": 4324,
"3": 2700,
"4": 2300,
"5": 0
}
}
You can update the totals for the day and month with a simple targeted update using
the $inc operator:
use stats-2011
db.sites-nov.update({ base: "org.mongodb", path: "/" },
$inc: {total: 1, "days.5": 1 });
Ta k e a m o m e n t t o n o t i c e t h e c o l l e c t i o n a n d d a t a b a s e n a m e s . T h e c o l l e ct io n , sites-
nov , is scoped to a given month, and the database, stats-2011 , to a particular year.
3
Two pieces of literature covering compensation-driven transactions are worth studying. The original is Garcia-
Molina and Salem's “Sagas” paper ( http://mng.bz/73is ) . The less formal but no less interesting “Your Coffee
Shop Doesn't Use Two-Phase Commit” by Gregor Hohpe ( http://mng.bz/kpAq ) is also a great read.
Search WWH ::




Custom Search