MongoDB Transaction
By: Onlykiosk Dev Tech
Tags: Mongo Shell, Transactions, Mongo, Mongodb

Starting from this lesson, we will learn how to use transaction. First, we need to talk about ACID.

ACID represents the four basic characters of a transaction operation. It stands for atomicity, consistency, isolation and durability.

In the content of databases, a sequence of database operations that satisfies the ACID properties is called a transaction. So one transaction usually consists of several database operations.

Transaction guarantees data validity despite errors power failures and other mishaps.

Atomicity is the most important character.

An atom is defined as the smallest individual particle of a matter. It means undividable.

Atomicity means operations of the same transaction are bound together and behave as if they were a single unit. These operations will either succeed completely or fail completely.

For the transaction to be successful, every operation must be successful. If one operation fails, no matter how insignificant it is, the whole transaction will abort and fail. In simple words, atomicity means all or nothing.

The most typical application scenario of atomicity is monetary transfer.

Let’s say we bought something using a bank card.

The bank will first withdraw money from the buyer’s account. Withdrawing money means updating the balance field of the buyer’s account.

The second step is to save money to seller’s account. This is also an update operation on the balance field.

These two operations must be bound together, meaning they must succeed completely or fail completely.

The second operation should only start after the first one has succeeded.

If the second operation failed, the first option should be revoked. This way, no side will lose money.

MongoDB ensures an operation on a single document is atomic.

For example, we have a document here.

We call the update method to update the message, location and nation fields.

The updates of the three fields are bound together. For the update method to be successful, all three fields must be updated successfully.

Transaction is not available in MongoDB until version 4.0. Before version 4, the single-document atomicity plays the role of transaction.

For example, if we need to bind the operations on two documents together, we can put the two documents inside the same document. The single-document atomicity ensures the two operations either succeed completely or fail completely.

Consistency means any given database transaction must change affected data only in allowed ways. A transaction can only bring the database from one valid state to another valid state.

Isolation is for dealing with concurrency.

Let’s say we buy something using a bank card. Money will be withdrawn from our account which means our balance field will be updated to a lower value.

If at the same time, we receive a payment. Our balance will be updated to a higher value.

These two operation work on the same field and cannot take place simultaneously. So we need to isolate them and only execute one operation at a time.

Durability means the result of a transaction is persistent.

Persistence means a state can outlive the process that created it. It requires on-disk storage.

So what can we get from this?

Transaction needs the wiredTiger engine to ensure its results can live through a system collapse or power failure.