Transactions in NoSQL

Last Updated : 26 Jun, 2026

Transactions in NoSQL databases ensure that one or more operations are executed as a single unit of work, helping maintain data integrity even when failures occur. Modern NoSQL databases support transactions to provide reliable and consistent data management for applications.

  • Ensure multiple operations are completed successfully or rolled back together.
  • Improve data consistency and reliability across related documents or records

Transaction Commands

The following commands are used to manage transactions and control database operations in NoSQL databases.

BEGIN TRANSACTION

Starts a new transaction.All operations performed after this command become part of the transaction.

BEGIN TRANSACTION;

INSERT

Adds a new record to the database.

INSERT INTO Employees (EmpID, Name, Department)
VALUES (101, 'John Smith', 'IT');

UPDATE

Modifies an existing record.

UPDATE Employees
SET Department = 'HR'
WHERE EmpID = 101;

DELETE

Removes a record from the database.

DELETE FROM Employees
WHERE EmpID = 101;

COMMIT

Permanently saves all changes made during the transaction.

COMMIT;

ROLLBACK

Cancels all changes made during the transaction and restores the previous state.

ROLLBACK;

Example of a Transaction

BEGIN TRANSACTION;

INSERT INTO Employees (EmpID, Name, Department)
VALUES (101, 'John Smith', 'IT');

UPDATE Employees
SET Department = 'HR'
WHERE EmpID = 101;

COMMIT;

ACID Properties of Transactions

ACID is a set of properties that ensure database transactions are processed reliably and maintain data integrity.

Atomicity

Atomicity ensures that all operations in a transaction are completed successfully. If any operation fails, the entire transaction is rolled back.

Example: During a bank transfer, money should be deducted from one account and added to another. If one step fails, neither change is applied.

Consistency

Consistency ensures that a transaction brings the database from one valid state to another, following all rules and constraints.

Example: If the total balance in two accounts is ₹10,000 before a transfer, it should remain ₹10,000 after the transfer.

Isolation

Isolation ensures that multiple transactions running at the same time do not interfere with each other.

Example: Two users updating the same account simultaneously should not cause incorrect results.

Durability

Durability ensures that once a transaction is committed, its changes are permanently stored, even if a system failure occurs.

Example: After an online payment is confirmed, the transaction record remains saved even if the server crashes immediately afterward.

Working of ACID Transactions

We discuss the working of ACID transactions by taking the example :

a_100
Transaction Example

We have to send 50 from S1 to S2 so we have to know here how the ACID transaction works in such an operation:

1. Atomicity: The atomicity ensures that the full operation takes place or is aborted back. It ensures that both operations take place or neither even one cannot take place.

  • First operation  S1 : A - 50;
  • Second operation  S2 : B + 50;

2. Consistency: Consistency property ensures that the integrity of transactions is maintained properly. Such as:

  • Before transaction S1 : A + B = 150. ( A = 100, B = 50).
  • After transaction : S2 : A + B = 150. ( A = 50, B = 100).

3. Isolation: Isolation ensures that if the two transactions are going on no other transaction can be accessed between them. It means two operations can be done at one time and no other operation or task can interfere with them.

Such as if two operations such as A - 50 and B + 50 are working no third transaction can interfere with them.

4. Durability: If all transactions are completed it cannot go back to its previous steps.

Single-Document Transactions

Many NoSQL databases guarantee atomic operations on a single document.

Example:

{  "account_id": 101,  "balance": 5000}

Updating the balance is treated as a single atomic operation.

Multi-Document Transactions

Some NoSQL databases support transactions across multiple documents.

Example: Money Transfer

Account A Balance = 5000Account B Balance = 3000Transfer Amount = 1000
  • Deduct 1000 from Account A.
  • Add 1000 to Account B.
  • Commit the transaction.
  • If any step fails, all changes are rolled back.

Distributed Transactions

Distributed transactions involve multiple databases, collections or nodes in a distributed NoSQL system. They ensure that all operations across different systems are completed successfully as a single transaction.

Example: Online Order Processing

  • Product Inventory = 50 Units
  • Customer Payment = ₹1000
  • Order Status = Pending

Transaction Steps:

  1. Reduce the inventory by 1 unit.
  2. Process the customer payment.
  3. Update the order status to Confirmed.
  4. Commit the transaction.

Applications of Transactions in NoSQL

  • Banking Systems: Secure money transfers between accounts.
  • E-Commerce: Process orders, payments and inventory updates.
  • Inventory Management: Maintain correct stock levels.
  • Reservation Systems: Prevent double booking of seats or rooms.
  • Financial Applications: Ensure accurate payment and transaction records.
  • Healthcare Systems: Manage patient records reliably.
Comment