Atomicity and Transactions

Data corruption in case of system crashes, power failures, and other errors is a nightmare in real production systems with heavy traffic.

We will see how Atomicity and Transactions will solve this problem with the example below.

We will make a simple ticket booking system with a high chance of race conditions.

Requirements: Every day we have 2 tickets available and more than 2 users try to book tickets at the same time.

Version 1:

If we don't have this 10-second wait time, the difference between the count query in DB and the insert query in DB will be a few milliseconds and for testing, we will have to hit the API within milliseconds for which we have to use load testing tools like JMeter, artillery, etc

So a 10-second wait time is added in the above code to make parallel request testing easy.

Result:

When different users hit version 1 API within 10 seconds for the same date, more than 2 tickets get created in this case.

Version 2:

In this method, we save the count of tickets for all dates in different collection ticketCountV2 and use the atomic operator $inc with the condition of $lt. This code will only book max 2 tickets for a particular date no matter how many users are trying to book simultaneously.

query 1: decrement available tickets for a day in ticketCountV2 collection

query 2: creating ticket in ticketsV2 collection

The problem with this is when our server restarts for some random reason like a power failure between 2 different queries, the system will allow the booking of fewer than 2 tickets which is also a problem.

Version 3:

Database transactions to rescue: If there is a power failure after the first database query, the database will roll back changes automatically since the transaction will not be committed.

Happy path:

  • step 1: decrement available tickets for a day in the ticketCountV2 collection
  • step 2: Create a ticket in the ticketsV2 collection
  • step 3: commit the transaction

In case of power failure between 2 queries:

  • step 1: decrement available tickets for a day in the ticketCountV2 collection
  • step 2: Create a ticket in the ticketsV2 collection
  • power failure
  • DB will automatically rollback since there is no commit for the transaction

Source Code: https://github.com/riddheshganatra/Atomicity-and-Transactions

Share this with anybody you think would benefit from this. Have any suggestions or questions? Feel free to message me on LinkedIn.

We at Code B would be happy to help if you need help.

--

--

Software Architect, Full Stack Web developer, MEAN/MERN stack, Microservices, etc

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Riddhesh Ganatra

Software Architect, Full Stack Web developer, MEAN/MERN stack, Microservices, etc