Multi-Document ACID Transactions in MongoDB

This chapter dives into how MongoDB implements multi-document ACID transactions, enabling applications to maintain data consistency across multiple documents and collections. We’ll discuss concepts, implementations, practical use cases, and advanced techniques for optimizing transactions in MongoDB.

Introduction to Multi-Document Transactions

What are Multi-Document Transactions?

  • Multi-document transactions allow a series of operations on multiple documents across collections or databases to be completed in an atomic way, ensuring ACID compliance.
  • With multi-document transactions, either all operations succeed, or none do, preserving data consistency.

 Why Are Multi-Document Transactions Important?

  • Applications often require operations that span multiple documents, such as banking transactions (transferring funds between accounts) or e-commerce transactions (placing an order and updating stock).
  • They ensure data integrity by allowing complex workflows to operate without risk of partial updates or inconsistencies.

ACID Properties in MongoDB Transactions

Overview of ACID Properties

  • Atomicity: Ensures all operations in a transaction are completed or rolled back as a single unit.
  • Consistency: Guarantees the database adheres to all defined rules before and after the transaction.
  • Isolation: Ensures that transactions are not visible to each other, providing a controlled environment.
  • Durability: Guarantees that completed transactions are permanently recorded, even in case of system failures.

ACID Properties and MongoDB

  • MongoDB supports multi-document ACID transactions within replica sets, ensuring data integrity.
  • Transactions are designed to provide these properties across distributed collections in a replica set environment.

Setting Up for Multi-Document Transactions

Prerequisites for Transactions

  • Transactions require MongoDB 4.0 or later for replica sets and MongoDB 4.2 or later for sharded clusters.
  • Configure MongoDB with replica sets or sharding to enable transaction functionality.

Basic Transaction Setup

  • Ensure the database environment is properly set up to support transaction logging, snapshot isolation, and other prerequisites.

Implementing Basic Multi-Document Transactions

Structure of a Transaction

  • A transaction starts with startSession and startTransaction, followed by a series of read/write operations and concludes with either commitTransaction or abortTransaction.

Example of a Basic Transaction

  • Let’s say we have a bank account transfer scenario where we need to deduct funds from one account and add them to another.
				
					const session = client.startSession();
session.startTransaction();

try {
    await db.collection("accounts").updateOne(
        { _id: "A" }, { $inc: { balance: -100 } }, { session }
    );
    await db.collection("accounts").updateOne(
        { _id: "B" }, { $inc: { balance: 100 } }, { session }
    );

    await session.commitTransaction();
    console.log("Transaction committed successfully");
} catch (error) {
    await session.abortTransaction();
    console.error("Transaction aborted due to error:", error);
} finally {
    session.endSession();
}

				
			
  • Explanation: The above transaction deducts $100 from account “A” and adds it to account “B.” The transaction will commit if both operations are successful; otherwise, it will abort.

Using

commitTransaction and abortTransaction

  • commitTransaction: Finalizes the transaction, making changes permanent.
  • abortTransaction: Rolls back the transaction, restoring the initial state of the data.

Read and Write Concerns in Transaction

Configuring Write Concern for Durability

  • Write concerns allow you to specify acknowledgment levels for write operations. For transactions, use { writeConcern: { w: "majority" } } to ensure durability.

 Configuring Read Concern for Isolation

  • MongoDB provides various readConcern levels. For transactions, snapshot read concern is typically used to ensure the transaction operates on a consistent snapshot of the data.
				
					session.startTransaction({
    readConcern: { level: "snapshot" },
    writeConcern: { w: "majority" }
});

				
			

Multi-Document Transactions in Sharded Clusters

Transactions Across Shards

  • MongoDB supports transactions across multiple shards starting from version 4.2.
  • Sharded transactions allow applications to perform multi-document ACID operations even when data is distributed across different shards.

Example of a Sharded Cluster Transaction

  • Suppose a transaction spans two shards to update user data and order details.
				
					const session = client.startSession();

try {
    session.startTransaction();
    
    await db.collection("users").updateOne({ userId: "A" }, { $set: { status: "active" } }, { session });
    await db.collection("orders").updateOne({ orderId: "1" }, { $set: { status: "confirmed" } }, { session });
    
    await session.commitTransaction();
    console.log("Transaction in sharded cluster committed successfully");
} catch (error) {
    await session.abortTransaction();
    console.error("Transaction aborted:", error);
} finally {
    session.endSession();
}

				
			

Performance and Latency in Sharded Transactions

  • Sharded transactions might experience higher latency due to coordination across multiple nodes. Limiting transaction duration and scope can improve performance.

Handling Errors in Multi-Document Transactions

Error Types in Transactions

  • Transient Errors: Temporary errors that may resolve upon retrying.
  • Non-Transient Errors: More severe errors that likely require transaction abortion and investigation.

Implementing Retry Logic for Transactions

  • MongoDB provides error labels like "TransientTransactionError" to help identify transient errors, which can be automatically retried.
				
					async function runTransactionWithRetry(session, transactionFunction) {
    while (true) {
        try {
            await transactionFunction(session);
            break;
        } catch (error) {
            if (error.hasErrorLabel("TransientTransactionError")) {
                console.log("Retrying transaction...");
            } else {
                throw error;
            }
        }
    }
}

				
			

Best Practices for Multi-Document Transactions

Minimize Transaction Duration

  • Short transactions help avoid locking issues and improve concurrency.

Use Transactions Selectively

  • Avoid overuse of transactions. Use them only when atomicity across multiple documents is essential.

 Limit the Number of Operations in a Transaction

  • Keep the number of read/write operations minimal to reduce the risk of transaction timeouts or rollbacks.

Monitor Transaction Performance

  • Use MongoDB’s monitoring tools like mongostat, mongotop, or MongoDB Atlas to keep an eye on transaction metrics.

Case Study: E-commerce Application with Multi-Document Transactions

Overview of Requirements

  • In an e-commerce application, transactions can help manage order placements by ensuring stock availability, deducting inventory, and confirming payment in a single operation.

Implementing Multi-Document Transactions for Order Processing

				
					const session = client.startSession();
session.startTransaction();

try {
    // Check if product is in stock
    const product = await db.collection("inventory").findOne({ productId: "12345" }, { session });
    if (product.stock > 0) {
        await db.collection("inventory").updateOne({ productId: "12345" }, { $inc: { stock: -1 } }, { session });
        await db.collection("orders").insertOne({ orderId: "1001", productId: "12345", status: "confirmed" }, { session });
        
        await session.commitTransaction();
        console.log("Order placed and stock updated successfully");
    } else {
        throw new Error("Out of stock");
    }
} catch (error) {
    await session.abortTransaction();
    console.error("Transaction aborted:", error);
} finally {
    session.endSession();
}

				
			

Explanation of the Code

  • The transaction ensures that if the product is in stock, the order is placed, and stock is deducted.
  • If there is an error (e.g., out of stock), the transaction is aborted, preserving data integrity.

Multi-document transactions in MongoDB allow applications to handle complex workflows requiring atomic operations across multiple documents and collections. By implementing these transactions, you can ensure data consistency and integrity across a variety of use cases, from banking to e-commerce. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India