Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Thursday, 8 February 2024

MongoDB Transactions: Atomicity and Consistency

In MongoDB, transactions provide atomicity and consistency guarantees for data operations within a single document or across multiple documents in a replica set or sharded cluster. Here's how MongoDB ensures atomicity and consistency within transactions:


 Atomicity:


1. Single Document Transactions:

   - MongoDB supports multi-document transactions within a single replica set or sharded cluster. All operations within a transaction are atomic, meaning they either all succeed or all fail.


2. ACID Properties:

   - Transactions in MongoDB adhere to the ACID (Atomicity, Consistency, Isolation, Durability) properties.

   - Atomicity ensures that all operations within a transaction are atomic and are treated as a single unit of work.


3. Write Operations:

   - MongoDB transactions support multiple write operations (e.g., inserts, updates, deletes) within a transaction scope.

   - If any operation within the transaction fails, the entire transaction is rolled back, ensuring that the database remains in a consistent state.


 Consistency:


1. Read and Write Consistency:

   - MongoDB transactions provide read-your-writes consistency, meaning that a client reading data after a write operation within a transaction will see the updated data.

   - The MongoDB driver ensures that subsequent read operations within the same transaction see the effects of previous write operations within the transaction.


2. Isolation:

   - MongoDB transactions provide snapshot isolation, isolating the effects of concurrent transactions from each other.

   - Each transaction sees a consistent snapshot of the data at the start of the transaction, regardless of concurrent updates or transactions occurring on the same data.


3. Document-Level Locking:

   - MongoDB uses document-level locking for transactions, allowing multiple transactions to operate concurrently on different documents within the same collection.

   - This ensures that transactions can execute concurrently without blocking each other, improving throughput and scalability.


 Example:


javascript

const session = client.startSession();


try {

  await session.startTransaction();


  await collection1.updateOne({ _id: 1 }, { $set: { field1: "value1" } }, { session });

  await collection2.insertOne({ _id: 2, field2: "value2" }, { session });


  await session.commitTransaction();

} catch (error) {

  await session.abortTransaction();

  throw error;

} finally {

  session.endSession();

}

In this example:

- A session is started to encapsulate the transaction.

- Multiple write operations are performed within the transaction.

- If any operation fails, the transaction is aborted, and changes are rolled back.

- If all operations succeed, the transaction is committed, and changes are persisted.

By providing atomicity and consistency guarantees, MongoDB transactions ensure data integrity and reliability, enabling developers to build robust and transactional applications.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.