Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to live without transactions?

Most of the popular NoSQL databases (MongoDB, RethinkDB) do not support ACID transactions. They are very popular today within developers of different systems.

The problem is: how to guarantee data consistency without transactions? I thought that data consistency is one of the main things in production. Am I wrong? Maybe there is some technics to restore data consistency?

I would like to use RethinkDB for my project, but I'm scare about missed transactions.

like image 635
gyzerok Avatar asked Oct 20 '22 02:10

gyzerok


1 Answers

I do not know much about RethinkDB, so this answer is primarily based on MongoDB.

  1. while MongoDB can not provide atomic operations on multiple documents at the same time, it does guarantee atomicity for a single operation which affects one document. That means when one query changes multiple fields of the same document, you can be sure that all these changes will be performed at the same time. Combined with the MongoDB philosophy of keeping a consistent dataset in one document instead of spreading it over many rows of different related tables, this removes many situations where you would need transactions in a relational database.

  2. not every project needs complex transactions. Sure, there are some domains where it is essential (like most situations where you deal with money), but in other cases it isn't actually that big of a deal when some data is inconsistent for a few milliseconds. You need to consider how important data consistency is for your project. When you come to the conclusion that there are many situations where you do need transactions, then by all means, stick to SQL.

  3. In a pinch, MongoDB can simulate multi-document transactions by using the two-phase commit model. It's not easy to implement, it's not easy to work with, it does not result in a pretty data model, but it is a valid workaround when you have a project which would be perfect for MongoDB in all regards except for that one use-case which just can't do without transactions.

like image 103
Philipp Avatar answered Nov 01 '22 13:11

Philipp