MongoDB ACID Transactions: Multi-Document Transactions Simplified 101

• February 17th, 2022

MongoDB ACID Transactions | Hevo Data

One of the common challenges that every growing business faces are the ability to efficiently handle the exponentially growing data. Apart from the Traditional Relational Databases, organizations are now using Document-oriented Open-source NoSQL Databases. There are several NoSQL databases out there, but MongoDB is the most commonly used, and it is available both as a Cloud Service and for Deployment on Self-Managed Systems.

ACID transactions guarantee that a database will be in a consistent state after running a group of operations. Most databases offer transactional guarantees for operations that impact a single record. However, not all databases support transactions that work across multiple records, which can be concerning to developers who are accustomed to using them. But, MongoDB supports multi-document MongoDB ACID transactions for the use cases that require them.

In this article, you will gain information about MongoDB ACID Transactions. You will also gain a holistic understanding of MongoDB, its key features, ACID Transactions, MongoDB ACID Transactions, and how to perform MongoDB ACID Transactions. Read along to find out in-depth information about undergoing MongoDB ACID Transactions.

Table of Contents

What is MongoDB?

MongoDB is a NoSQL database that was developed by MongoDB inc, which is schema-free. It was designed and created using c++ and javascript allowing for higher connectivity. It uses a collection of Documents and has an option for creating schemas as well. It doesn’t follow the same structure as a traditional database wherein the data is stored in form of rows.

Since general RDBMS are easier to use same is the case with MongoDB. MongoDB uses a NoSQL platform making it easier for individuals having less or no prior programming knowledge. MongoDB processes the data in a semi-structured format, allowing for processing large volumes of data in one go simultaneously. It can be hosted on mostly all the cloud platforms be it Google’s Cloud, Microsoft Azure, or even Amazons’ Web Services.

MongoDB uses Binary JSON and MQL as an alternative to SQL. BSON allows for data types such as the floating-point, long, date, and many more that are not supported by regular JSON. MQL offers additional capabilities when compared to regular SQL making it more relevant for MongoDB as it processes JSON-type documents.

MongoDB is a NoSQL Server in which data is stored in BSON (Binary JSON) documents and each document is essentially built on a key-value pair structure. As MongoDB easily stores schemaless data, make it appropriate for capturing data whose structure is not known. This document-oriented approach is designed to offer a richer experience with modern programming techniques.

To install MongoDB click here.

Key Features of MongoDB

Main features of MongoDB which make it unique are:

1) High Performance

Data operations on MongoDB are fast and easy because of their NoSQL nature. Data can be quickly stored, manipulated, and retrieved without any compromise on data integrity.

2) Scalability

In the Big Data era, MongoDB data can be distributed across a cluster of machines quickly and equally, free of bulkiness. The scalability of MongoDB handles a growing amount of data capably. Sharding is a process in MongoDB used to horizontally scale the data across multiple servers when the size of data increases.

3) Availability

Data is highly available with MongoDB as it makes multiple copies of the same data and sends copies of data across different servers. In case any server fails, data can be retrieved from another server without delay.

4) Flexibility

MongoDB can easily be combined with different Database Management Systems, both SQL and NoSQL types. Document-oriented structure makes MongoDB schema dynamically flexible and different types of data can be easily stored and manipulated.

To learn more about MongoDB, click this link.

Simplify MongoDB ETL with Hevo’s No-code Data Pipeline

A fully managed No-code Data Pipeline platform like Hevo Data helps you integrate and load data from 100+ different sources (including 40+ free sources) such as MongoDB to a Data Warehouse or Destination of your choice in real-time in an effortless manner. Hevo with its minimal learning curve can be set up in just a few minutes allowing the users to load data without having to compromise performance. Its strong integration with umpteenth sources allows users to bring in data of different kinds in a smooth fashion without having to code a single line. 

Its completely automated pipeline offers data to be delivered in real-time without any loss from source to destination. Its fault-tolerant and scalable architecture ensure that the data is handled in a secure, consistent manner with zero data loss and supports different forms of data. The solutions provided are consistent and work with different Business Intelligence (BI) tools as well.

Get Started with Hevo for Free

Check out why Hevo is the Best:

  • Secure: Hevo has a fault-tolerant architecture that ensures that the data is handled in a secure, consistent manner with zero data loss.
  • Schema Management: Hevo takes away the tedious task of schema management & automatically detects the schema of incoming data and maps it to the destination schema.
  • Minimal Learning: Hevo, with its simple and interactive UI, is extremely simple for new customers to work on and perform operations.
  • Hevo Is Built To Scale: As the number of sources and the volume of your data grows, Hevo scales horizontally, handling millions of records per minute with very little latency.
  • Incremental Data Load: Hevo allows the transfer of data that has been modified in real-time. This ensures efficient utilization of bandwidth on both ends.
  • Live Support: The Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
  • Live Monitoring: Hevo allows you to monitor the data flow and check where your data is at a particular point in time.
Sign up here for a 14-Day Free Trial!

What are ACID transactions?

ACID is an acronym of four properties, namely Atomicity, Consistency, Isolation, and Durability. Let’s understand each of these properties of the transaction. 

  • Atomicity implies that all commands within a transaction are treated as a single unit. Either all will be successful, or none of them will be. There won’t be cases of some successful commands and some failed commands
  • Consistency ensures that all the changes made in the database during the transaction are consistent with the constraints of the database
  • Isolation guarantees that concurrent transactions, i.e. transactions executing simultaneously, will not affect each other. The outcomes of one transaction will be unaffected by the other. Therefore, transactions that run concurrently appear to be running sequentially.
  • Durability takes care of ensuring that once the transaction is complete, the data will be persistent even in case of a system failure, i.e., the data is also written to a backing store.

If you wish to learn more about ACID transactions, the Wikipedia page is a good reference. The MongoDB article on ACID transactions is also really helpful.

What is an ACID transaction example?

Let’s consider an example in which a transaction impacts multiple records.

Assume you’re creating a function to transfer money from one account to another, with each account acting as its own record. You have a serious accounting problem if you successfully withdraw money from the source account but never credit it to the destination. You’d have the same problem if you credited the destination but never took money from the source to cover it.

To keep the system consistent, these two write operations must either occur simultaneously or concurrently fail.

await session.withTransaction(async () => {
   const subtractMoneyResults = await accountsCollection.updateOne(
       { _id: account1 },
       { $inc: { balance: amount * -1 } },
       { session });
   if (subtractMoneyResults.modifiedCount !== 1) {
       await session.abortTransaction();
       return;
   }
 
   const addMoneyResults = await accountsCollection.updateOne(
       { _id: account2 },
       { $inc: { balance: amount } },
       { session });
   if (addMoneyResults.modifiedCount !== 1) {
       await session.abortTransaction();
       return;
   }
});

Let’s examine each of the ACID properties in the given example:

  • Atomicity: Money must be removed from one account and added to another or the transaction will fail. Removing money from one account without adding it to another would result in inconsistency in the data.
  • Consistency: Consider the database constraint that an account balance cannot be less than zero dollars. All updates to an account balance within a transaction must result in a valid, non-negative balance, or the transaction would be aborted.
  • Isolation: Consider two simultaneous requests to transfer money from the same bank account. Running the transfer requests concurrently should produce the same results as running the transfer requests sequentially.
  • Durability: Consider a power outage that occurs shortly after a database confirms that money has been transferred from one bank account to another. Even if there was an unexpected failure, the database should still contain the most up-to-date information.
MongoDB ACID Transactions: Example | Hevo Data
Image Source

The above diagram depicts how the ACID properties influence the flow of money from one bank account to another.

What are MongoDB ACID Transactions?

Single-document updates have always been atomic in MongoDB. The Document model is well suited to storing related data that is accessed in a single Document (compared to the relational model where related data may be normalised and split between multiple tables). A well-designed Document schema, in most cases, allows you to work without the need for multi-Document transactions.

When multi-document ACID compliance is required, MongoDB transactions work across your cluster and perform as expected. Transactions have a performance overhead in a distributed system, so you need to keep your resource constraints and performance goals in mind.

Having understood what ACID transactions are, let’s now look at how to implement them MongoDB ACID Transactions. The following article will show how to implement MongoDB ACID transactions using Mongo Shell. 

Prerequisites for Performing MongoDB ACID Transactions

The prerequisites for performing MongoDB ACID Transactions are as follows:

  • You should have a MongoDB account. If you don’t have one, you can sign up for a free account.

The steps are as follows:

  • Step 1: Complete the steps, including choosing your cloud provider and region and create your cluster.
  • Step 2: Once created, you should be able to see the following options besides the cluster name:
MongoDB ACID Transactions: Cluster | Hevo Data
  • Step 3: Click on the ‘Connect’ button. Click on it, and follow the instructions to either download Mongo shell, or Mongo Compass (if you prefer a GUI). Mongo Compass has Mongo shell integrated. In this blog, Mongo Compass is used.
MongoDB ACID Transactions: Connect to Cluster | Hevo Data
  • Step 4: Once Mongo compass or shell is downloaded, use the connection string to connect to your database. You should be able to see your DBs and collections in the GUI (if using Mongo Compass).
  • Step 5: Next, in the web console, click on the ‘Browse Collections’ button and then click on the ‘Load Sample Data’ option.

Performing MongoDB ACID Transactions

You can carry out the following steps to perform MongoDB ACID Transactions.

Once your sample data is loaded, you should be able to see it in Mongo Compass. You will be using the ‘transactions’ Collection from the ‘sample_analytics’ Database.  You can browse the data in the GUI. You will see that it contains the transaction logs (buy/ sell) for each account. Looking at the transaction logs, the accounts appear to be Demat accounts.

MongoDB ACID Transactions: Sample_analytics Transaction | Hevo Data
  • Step 1: If you have a look at the bottom of the screen, you should see Mongo Shell. Click on it.
  • Step 2: Since you will be dealing with the ‘sample_analytics’ database, you’ll first make MongoDB use it. Enter the ‘use sample_analytics’ command.
MongoDB ACID Transactions: use sample_analytics | Hevo Data
  • Step 3: Now, you can create a session within which your transaction will be performed.

The syntax is:

session=db.getMongo().startSession()
MongoDB ACID Transactions: Start Session | Hevo Data
  • Step 4: Now, within each session, the commands within a transaction should be enclosed within:
session.startTransaction() and session.commitTransaction()

While the transaction is ongoing (i.e., it is not committed), you can not see the differences brought about by the transaction in other tabs.

  • Step 5: Let’s see an example. Type in the following two commands into your Mongo shell:
session.startTransaction()
session.getDatabase('sample_analytics').transactions.updateOne({account_id:443178},{$set:{transaction_count:67}})
  • Step 6: Over here, you are updating the value of one Document (identified by account_id = 443178) in the ‘transactions’ Collection of ‘sample analytics’ Database. You are setting its transaction_count to 67 (it was 66 by default). If you run the above two transactions, you should see an acknowledgment from MongoDB.
MongoDB ACID Transactions:  Start Transaction | Hevo Data

Note that you still haven’t committed the transaction. If you go back to your GUI and search for this Document, you will find the transaction_count to be 66.

MongoDB ACID Transactions: Transaction Count | Hevo Data
  • Step 7: Now, commit the transaction, using session.commitTransaction(), and then search for the same Document again in the GUI. You will notice that the value has now changed to 67. Your transaction is now persistent, even in the case of system failures. 
MongoDB ACID Transactions: Commit Transaction | Hevo Data
  • Step 8: This was a single document transaction essentially. But you can very easily extend this to multi-document transactions. All you need to do it add more commands between session.startTransaction() and session.commitTransaction().

What if something goes wrong in the process, i.e. one of the transactions fails? You can simply call session.abortTransaction() and none of the Documents will be affected because the transaction wasn’t committed yet. 

Given below is an example of a multi-Document transaction:

session.startTransaction()
session.getDatabase('sample_analytics').transactions.updateOne({account_id:443178},{$inc:{transaction_count:1}})
session.getDatabase('sample_analytics').transactions.updateOne({account_id:443178},{$push:{transactions:{ date: "2005-07-07T00:00:00.000+00:00", amount:2000,transaction_code:"sell",symbol:"msft",price:"20",total:"40000"}}})
session.getDatabase('sample_analytics').transactions.updateOne({account_id:557378},{$inc:{transaction_count:1}})
session.getDatabase('sample_analytics').transactions.updateOne({account_id:557378},{$push:{transactions:{ date: "2005-07-07T00:00:00.000+00:00", amount:2000,transaction_code:"buy",symbol:"msft",price:"20",total:"40000"}}})
session.commitTransaction()

As you can see, this transaction reflects a trade of 20 Microsoft shares, sold from the account with id 443178, and bought into the account with id 557378. There are a total of four commands: two for increasing the transaction_count by 1, and two for updating the transactions array. 

This is how multi-document transactions work in the Mongo shell. If you are interested in implementing these in server-side languages like Nodejs or Python, you can check out MongoDB’s guide for further details on MongoDB ACID Transactions.

Conclusion

In this article, you have learned about MongoDB ACID Transactions. This article also provided information on MongoDB, its key features, ACID Transactions, MongoDB ACID Transactions, and how to perform MongoDB ACID Transactions in detail. For further information on MongoDB Replica Set Configuration, MongoDB Compass Windows Installation, MongoDB Count Method, you can visit the following links.

Hevo Data, a No-code Data Pipeline provides you with a consistent and reliable solution to manage data transfer between a variety of sources and a wide variety of Desired Destinations with a few clicks.

Visit our Website to Explore Hevo

Hevo Data with its strong integration with 100+ data sources (including 40+ Free Sources) allows you to not only export data from your desired data sources & load it to the destination of your choice but also transform & enrich your data to make it analysis-ready. Hevo also allows integrating data from non-native sources using Hevo’s in-built Webhooks Connector. You can then focus on your key business needs and perform insightful analysis using BI tools. 

Want to give Hevo a try?

Sign Up for a 14-day free trial and experience the feature-rich Hevo suite first hand. You may also have a look at the amazing price, which will assist you in selecting the best plan for your requirements.

Share your experience of understanding MongoDB ACID Transactions in the comment section below! We would love to hear your thoughts.

No-code Data Pipeline for MongoDB