Until version 4, MongoDB lacked the ability to carry out ACID transactions. ACID transactions enable database operations (creating, reading, updating and deleting data) to be more resilient and dependable. This is necessary especially in finance-related applications that have to do with transferring money among users.

What is ACID?

ACID is an acronymn for Atomicity, Consistency, Isolation and Durability. When a database transaction (a group of CRUD operations on a database) is said to be ACID, it means that the database is always left in a valid state even in the event of unexpected errors that occur during the transaction.

For example, if user A initiates an operation to transfer money from their account to user B’s account, a group of operations (a transaction) would occur in this order:

  1. deduct the amount from user A’s balance,
  2. add it to user B’s balance

If after operation 1, an error occurs and 2 could not be effected, the database would be in an invalid state because funds have been deducted from user A but have not been added to user B. In the event of an error occurring between operation 1 and 2 preventing 2 from being effected, operation 1 should be reverted to keep the database in a valid state. If a revert happens, the transaction is said to be ACID 1.

Setting up the MongoDB Server with run-rs

It is expected that you have installed MongoDB already and you have a running MongoDB server.

When the default MongoDB application is installed on a local computer and the application is set up to run a MongoDB server, the server is not set up with the replication 2 feature by default. run-rs is an npm package that will help start a MongoDB server that is enabled with the replication feature.

In the MongoDB Transactions API documentation, replica sets are required for ACID transactions in MongoDB. Replica sets are present in MongoDB databases created by MongoDB Atlas by default.

In order to set up a MongoDB server to run with replica sets locally, follow the steps below

  1. Using your preferred JavaScript package manager (pnpm, yarn, npm,) install run-rs globally. This article uses pnpm.
pnpm add -g run-rs
  1. Now that run-rs is installed, run the package in a terminal. Ensure to run it using the MongoDB application that you have installed already using the --mongod flag. Also ensure that you run it with the --keep flag becasue every time run-rs runs anew, it purges the data that existed in the database. Lastly, make sure that run-rs is run on a different port than 27017. 27017 is the port that the default MongoDB server runs on when installed. Two servers cannot run on the same port. You can choose another port such as 27000.
run-rs --mongod --portStart 27000 --keep

On successful run, you should see this on the terminal

run-rs MongoDB successfully running

run-rs MongoDB successfully running

Connecting your Node.js Application to the MongoDB Server

You should have MongoDB Compass installed so that you can view the results of this on a user interface.

We are going to set up an Express project with MongoDB, using Mongoose as the ORM, to test out database transactions using the MongoDB server we have just set up.

The project will have users seeded with funds and it will have an API endpoint that effects the transfer of funds from one user to another. The transfer operation will be managed by transactions in MongoDB. mongodb-transactions has been set up for your ease with the API documentation in the README.

Make sure that the run-rs MongoDB server is running with run-rs --mongod --portStart 27000 --keep and start the Express project in another terminal. You can view the content of the database when you open up your MongoDB Compass application and use mongodb://localhost:27000/?replicaSet=rs&readPreference=primary&ssl=false as the connection string.

Try to transfer funds from a user with a lower balance than the transfer amount and see the outcome.


  1. What are ACID Properties in Database Management Systems? ↩︎

  2. replication - A feature allowing multiple database servers to share the same data, thereby ensuring redundancy and facilitating load balancing. See MongoDB Glossary of Terms ↩︎