Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't MongoDB have transactions? (pre-v4.0)

Update: MongoDB supports transactions from version 4.0.

Original question:

As far as I know, MongoDB doesn't support transactions, and there are no plans for implementing such support. What is the reason for this?

like image 768
Rogach Avatar asked Aug 04 '13 11:08

Rogach


People also ask

Are transactions supported in MongoDB?

For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions. With distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards.

Does MongoDB support ACID transactions?

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.

Why MongoDB is not ACID compliant?

It does not comply with the definition of atomic that we know from relational database systems, in particular the link above. In this sense MongoDB does not comply with the A from ACID. MongoDB is C onsitent by default. However, you can read from secondary servers in a replica set.


1 Answers

Having no transaction is a trade-off to allow MongoDB to be scalable.

The purpose of a transaction is to make sure that the whole database stays consistent while multiple operations take place. But in contrary to most relational databases, MongoDB isn't designed to run on a single host. It is designed to be set up as a cluster of multiple shards where each shard is a replica-sets of multiple servers (optionally at different geographical locations).

A transaction can potentially affect lots of hosts of the database. That means that the transaction would have to be synchronized between all of these hosts. This would mean quite a lot of overhead and would scale very badly when increasing the size of the database by adding more servers.

The MongoDB FAQ explains it like this:

MongoDB does not have support for traditional locking or complex transactions with rollback. MongoDB aims to be lightweight, fast, and predictable in its performance. This is similar to the MySQL MyISAM autocommit model. By keeping transaction support extremely simple, MongoDB can provide greater performance especially for partitioned or replicated systems with a number of database server processes.

like image 76
Philipp Avatar answered Dec 11 '22 08:12

Philipp