MongoDB Distributed Transactions with examples – Guidelines

MongoDB Distributed Transactions with examples

Today in this article we shall see a few MongoDB Distributed Transactions with examples -Guidelines with examples.

MongoDB supports multi-document distributed transactions.

With the support of distributed transactions MongoDB, transactions can be used across multiple operations, collections, databases, documents, and shards.

ACID transactions are a key demanding capability for any business-critical transactional system.

Today in this article, we will cover below aspects,

Distributed transactions can be used across the below-listed items,

  • Multiple operations

  • Collections

  • Databases

  • Documents

  • Shards (MongoDB 4.2)

Guidelines

  • Transactions are not supported for capped collection.

  • Transactions can not be done with read/write to collections in the configadmin, or local databases.

  • Transactions operation cannot write to system.* collections.

  • For MongoDB 4..2 and less – You can not create collections and indexes in transactions.

  • For MongoDB 4.4 – You can create collections and indexes in transactions.

  • You can not create new collections in cross-shard write transactions.

Transactions and Sessions

  • Create a transaction for a session.

 
using (var session = await _context._mongoClient.StartSessionAsync())
            {
                // Begin transaction
                session.StartTransaction();
       
                
            }

  • Have one open transaction for a session.

  • When using the drivers, each operation in the transaction must be associated with the session. Refer to your driver-specific documentation for details.

  • Abort the transaction explicitly for each session.

Example

Below is an example using C# gives you a brief idea about steps

  
    using (var session = await _context._mongoClient.StartSessionAsync())
            {
                // Begin transaction
                session.StartTransaction();
                 // add your logic for CRUD here
                 //
                 await session.CommitTransactionAsync();
                
            }

Transactions and Write Concern

Write concern describes the level of acknowledgment requested from MongoDB for write operations.

This could be for mongod or replica sets or sharded clusters.

  • Do not explicitly use write concern for the individual write operations for a transaction.

  • Transactions use the transaction-level write concern to commit the write operations.

  • The commit operation for a sharded cluster transaction use {w: "majority", j: true} writes concern. This is regardless of the written concern specified for the transaction.

Transactions and Read Concern

The readConcern the option allows you to control the isolation properties and consistency of the data read from replica sets and replica set shards.

Set the read concern at the transaction level and not at the individual operation level.

Do not explicitly set the read concern for the individual operations in a transaction

Any read concern set at the collection and database level is ignored inside the transaction.

Transactions and Distinct Operation

  • For unsharded collections, you can use the db.collection.distinct()

  • For sharded collections, you cannot use the db.collection.distinct()

I shall update the above list with more details in the coming days until then stay tuned…

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *