Video Transcript (English)
In this video, we will look at how ACID transactions work with the document model in MongoDB. We’ll cover the difference between single and multi-document ACID transactions in MongoDB and discuss when you should use a multi-document ACID transaction.
Remember, an ACID transaction is a set of database operations that must be atomic - that is, they must either happen all together successfully or fail together all at the same time.
Due to the nature of the document model, database operations that only affect one document are inherently atomic in MongoDB.
In other words, an operation, like updateOne, which is used to update one document is technically an ACID transaction.
This means that if we use updateOne to update multiple fields in a document, as we see in this example, either the ‘quantity’, ‘details’, and ‘tags’ fields will all be updated, or none of them will. These changes will all be visible to clients at the same time.
No extra steps are required to make sure that this operation has ACID properties!
In contrast, operations that affect multiple documents in MongoDB are not inherently atomic and do require some extra steps in order to be an ACID transaction.
For example, consider adding an item to a shopping cart in an ecommerce application. We use .updateOne method to add an item to a customer’s shopping cart. Then we need to use updateOne on the productInventory collection to make sure that the item is also deducted from the inventory to keep the database consistent.
Since these operations affect multiple documents - a shopping cart and a product inventory - these two write operations are not inherently atomic.
In order for the database to remain consistent we need to know that these operations need to be implemented as a multidocument ACID transaction.
The decision to use a multi-document ACID transaction requires in-depth knowledge of your application requirements and should not be taken lightly.
The reason for this is that MongoDB essentially ‘locks’ all documents involved in write operations in a multi-document transaction. As such, multi-document transactions incur a much greater performance cost and can affect the latency of your application.
Think about multi-document transactions as a precise tool that should only be used in the special circumstance when you need to make absolutely certain that multi-document operations are completed with ACID properties.
Here are the key take-aways from this video:
When working with MongoDB, all single-document operations are inherently atomic.
Therefore, no extra steps are needed to provide ACID properties to single-document operations.
However, if we need multi-document operations to have ACID properties, we then need to make sure that they're wrapped in a multidocument transaction.
Multi-document ACID transactions should be used as a precise tool only when ACID properties are necessary.