Search code examples
mongodbtransactionsdistributed-transactions

Why is replica set mandatory for transactions in MongoDB?


As per MongoDB documentation, transactions only works for replica sets and not single node. Why such requirement? Isn't it is easier to do transaction stuff on a single node rather than a distributed system?


Solution

  • Implementation of transactions uses sessions which in turn require an oplog. Oplog is provided by replica sets for data synchronization between nodes.

    Isn't it is easier to do transaction stuff on a single node rather than a distributed system?

    This is true but in practice, MongoDB positions itself as a high-availability database therefore there are rather few production deployments using a standalone server (as far as I know this isn't even an option in Atlas, for example). Hence lack of transaction support on standalone servers typically doesn't affect anything.

    Conversely, implementing transactions only on standalone servers would not address the needs of the vast majority of MongoDB deployments/customers that use replica sets and sharded clusters.

    For development purposes you can run a single-node replica set which gives you an oplog required for sessions and transactions but still only one mongod process.