Database Reference
In-Depth Information
ally, other simultaneous transactions should not see the data in an incomplete state where
either the debit or credit has not yet completed.
To address situations such as these, relational databases use atomic multistatement transac-
tions, where a group of updates to a database either all succeed (via COMMIT ) or all fail (via
ROLLBACK ). The drawback to multistatement transactions is that they can be quite slow if you
are using a distributed database. However, it is possible to maintain consistency across mul-
tiple servers in a distributed database using a two-phase commit protocol, summarized as fol-
lows:
1. Each server prepares to execute the transaction. In this stage, all the updates are com-
puted and guaranteed not to cause consistency violations within the server.
2. Once all servers have executed the “prepare” step, each server then applies the updates
that are part of the transaction.
The drawback to a two-phase commit is that it can significantly slow down your application.
Since each server guarantees that the transaction can be completed at the end of the prepare
step, the server will typically maintain a set of locks on data to be modified. These locks must
then be held until all the other servers have completed their prepare step, which may be a
lengthy process.
MongoDB, designed from the beginning with an eye toward distributed operation, “solves”
this problem by giving up on the idea of multidocument transactions. In MongoDB, each up-
date to a document stands alone.
Compound Documents
MongoDB's document model and its update operators combine to enable operations that
would require transactions in relational databases. For example, consider deleting an order
from a database where each order contains multiple line items. In a relational database, we
could use a transaction to ensure that the order “cleans up after itself”:
BEGIN
BEGIN TRANSACTION ;
DELETE
DELETE FROM
FROM orders WHERE
WHERE id = '11223' ;
DELETE
DELETE FROM
FROM order_items WHERE
WHERE order_id = '11223' ;
COMMIT
COMMIT ;
Search WWH ::




Custom Search