MongoDb - ACID Multi-Document Transactions in MongoDB

Before version 4.0, MongoDB only guaranteed atomicity at the single-document level. That means one document update was safe and complete, but multiple related updates across collections were not automatically protected.

From MongoDB 4.0 (and expanded in 4.2), multi-document ACID transactions were introduced.


First, What is ACID?

ACID is a standard set of properties that guarantee reliable database transactions.

A – Atomicity

Either all operations in a transaction succeed, or none of them do.

Example:
If you transfer money from Account A to Account B:

  • Deduct from A

  • Add to B

If one fails, both are rolled back.


C – Consistency

The database always moves from one valid state to another.

If a rule says balance cannot be negative, transactions must respect that rule.


I – Isolation

Transactions running at the same time do not interfere in a way that causes corruption.

Users should not see partial updates from another transaction.


D – Durability

Once a transaction is committed, it will not be lost — even if the system crashes.

This is supported by journaling and replication.


Why Multi-Document Transactions Are Important

MongoDB is a document database. Often, you can design your schema so that related data stays in one document.

But in real-world systems, you may have:

  • Orders collection

  • Users collection

  • Payments collection

  • Inventory collection

A single business action might update all of these.

Without transactions:

  • If one operation fails midway, your data becomes inconsistent.

With transactions:

  • All operations are committed together or rolled back together.


Example Scenario

E-commerce Order Placement:

Steps:

  1. Create order document

  2. Deduct product stock

  3. Record payment

  4. Update user purchase history

If step 3 fails:

  • Order should not exist

  • Stock should not be reduced

Transactions ensure this safety.


How Transactions Work in MongoDB (Conceptually)

  1. Start a session

  2. Start a transaction

  3. Perform multiple operations

  4. Commit transaction
    OR
    Abort transaction

If commit succeeds → changes are permanent
If abort happens → everything is rolled back


Performance Consideration

Transactions add overhead.

Why?

  • Locking

  • Maintaining snapshot isolation

  • Writing additional metadata

Because of this:

  • MongoDB recommends good schema design first

  • Use transactions only when necessary

MongoDB is optimized for embedding related data in one document, which often removes the need for transactions.


Snapshot Isolation

MongoDB uses snapshot isolation for transactions.

This means:

  • Each transaction sees a consistent snapshot of the database.

  • It does not see intermediate changes from other transactions.

This improves reliability in concurrent systems.


When You Should Use Transactions

Use them when:

  • Updating multiple collections

  • Performing financial operations

  • Maintaining strong data integrity rules

  • Building enterprise applications

Avoid unnecessary transactions when:

  • A single document update is enough

  • High performance is more important than strict atomicity


Interview-Level Understanding

You should clearly know:

  • MongoDB supports ACID transactions since version 4.0

  • They work across multiple documents and collections

  • They use snapshot isolation

  • They impact performance

  • Proper schema design can reduce their need