MongoDB 4.0
is just around the corner and with rc0 we can get a good idea of what we can expect in the GA version. MongoDB 4.0 will allow transactions to run in a replica set and, in a future release, the MongoDB transaction will work for sharded clusters. This is a really big change!
Multi-statement transactions are a big deal for a lot of companies. The transactions feature has been in development since MongoDB version 3.0; in version 3.6 logical sessions were added. In an earlier blog post we highlighted a few details from what was delivered in 3.6 that indicated that 4.0 would have transactions.
There are a few limitations for transactions and some operations are not allowed yet. A detailed list can be found in the MongoDB documentation of the Session.startTransaction() method.
One restriction that we must be aware of is that the collection MUST exist in order to use transactions.
A simple transaction will be declared in a very similar way to that we use for other databases. The caveat is that we need to start a session before starting a transaction. This means that multi-statement transactions are not the default behavior to write to the database.
Download MongoDB 4.0 RC (or you can install it from the repositories).
|
1 |
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-4.0.0-rc1.tgz |
Uncompress the files:
|
1 |
tar -xvzf mongodb-linux-x86_64-ubuntu1604-4.0.0-rc1.tgz<br> |
Rename the folder to mongo4.0 and create the data folder inside of the bin folder:
|
1 |
mv mongodb-linux-x86_64-ubuntu1604-4.0.0-rc1 mongo4.0<br>cd mongo4.0<br>cd bin<br>mkdir data |
Start the database process:
Important: in order to have multi-statement transactions replica-set must be enabled
|
1 |
./mongod --dbpath data --logpath data/log.log --fork --replSet foo<br><br> |
Initialize the replica-set:
|
1 |
> rs.initiate()<br>foo:Primary> use percona<br>foo:Primary> db.createCollection('test')<br> |
Start a session and then a transaction:
|
1 |
session = db.getMongo().startSession()<br>session.startTransaction()<br>session.getDatabase("percona").test.insert({today : new Date()})<br>session.getDatabase("percona").test.insert({some_value : "abc"}) |
Then you can decide whether to commit the transaction or abort it:
|
1 |
session.commitTransaction() <br>session.abortTransaction() |
If the startTransaction throws the IllegalOperation error, make sure the database is running with replica set.
MongoDB 4.0 implements snapshot isolation for the transactions. The pending uncommitted changes are only visible inside the session context (the session which has started the transaction) and are not visible outside. Here is an example:
Connection 1:
|
1 |
foo:PRIMARY> use percona<br>switched to db percona<br>foo:PRIMARY> db.createCollection('test')<br>{<br> "ok" : 1,<br> "operationTime" : Timestamp(1528903182, 1),<br> "$clusterTime" : {<br> "clusterTime" : Timestamp(1528903182, 1),<br> "signature" : {<br> "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),<br> "keyId" : NumberLong(0)<br> }<br> }<br>}<br>foo:PRIMARY> session = db.getMongo().startSession()<br>session { "id" : UUID("bdd82af7-ab9d-4cd3-9238-f08ee928f31e") }<br>foo:PRIMARY> session.startTransaction()<br>foo:PRIMARY> session.getDatabase("percona").test.insert({today : new Date()})<br>WriteResult({ "nInserted" : 1 })<br>foo:PRIMARY> session.getDatabase("percona").test.insert({some_value : "abc"})<br>WriteResult({ "nInserted" : 1 }) |
Connection 2: starting second transaction in its own session:
|
1 |
foo:PRIMARY> use percona<br>switched to db percona<br>foo:PRIMARY> db.test.find()<br>foo:PRIMARY> db.test.find()<br>foo:PRIMARY> session = db.getMongo().startSession()<br>session { "id" : UUID("eb628bfd-425e-450c-a51b-733435474eaa") }<br>foo:PRIMARY> session.startTransaction()<br>foo:PRIMARY> session.getDatabase("percona").test.find()<br>foo:PRIMARY> |
Connection 1: commit
|
1 |
foo:PRIMARY> session.commitTransaction() |
Connection 2: after connection1 commits:
|
1 |
foo:PRIMARY> db.test.find()<br>{ "_id" : ObjectId("5b21361252bbe6e5b9a70a4e"), "today" : ISODate("2018-06-13T15:19:46.645Z") }<br>{ "_id" : ObjectId("5b21361252bbe6e5b9a70a4f"), "some_value" : "abc" }<br> |
Outside of the session it sees the new values, however inside the opened session it will not see the new values.
|
1 |
foo:PRIMARY> session.getDatabase("percona").test.find()<br>foo:PRIMARY> |
Now if we commit the transaction inside connection 2 it will commit as well, and we will have 2 rows now (as there are no conflicts).
Sometimes, however, we may see the transient transaction error when committing or even doing find() inside a session:
|
1 |
foo:PRIMARY> session.commitTransaction() <br>2018-06-14T21:56:29.111+0000 E QUERY [js] Error: command failed: {<br> "errorLabels" : [<br> "TransientTransactionError"<br> ],<br> "operationTime" : Timestamp(1529013385, 1),<br> "ok" : 0,<br> "errmsg" : "Transaction 0 has been aborted.",<br> "code" : 251,<br> "codeName" : "NoSuchTransaction",<br> "$clusterTime" : {<br> "clusterTime" : Timestamp(1529013385, 1),<br> "signature" : {<br> "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),<br> "keyId" : NumberLong(0)<br> }<br> }<br>} :<br>_getErrorWithCode@src/mongo/shell/utils.js:25:13<br>doassert@src/mongo/shell/assert.js:18:14<br>_assertCommandWorked@src/mongo/shell/assert.js:520:17<br>assert.commandWorked@src/mongo/shell/assert.js:604:16<br>commitTransaction@src/mongo/shell/session.js:878:17<br>@(shell):1:1<br> |
From the MongoDB doc we can read that we could retry the transaction back when we have this error.
If an operation encounters an error, the returned error may have an errorLabels array field. If the error is a transient error, the errorLabels array field contains “TransientTransactionError” as an element and the transaction as a whole can be retried.
What about transaction conflicts in MongoDB? Let’s say we are updating the same row. Here is the demo:
First we create a record, trx, in the collection:
|
1 |
use percona<br>db.test.insert({trx : 0}) |
Then we create session1 and update trx to change from 0 to 1:
|
1 |
foo:PRIMARY> session = db.getMongo().startSession()<br>session { "id" : UUID("0b7b8ce0-919a-401a-af01-69fe90876301") }<br>foo:PRIMARY> session.startTransaction()<br>foo:PRIMARY> session.getDatabase("percona").test.update({trx : 0}, {trx: 1})<br>WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) |
Then (before committing) create another session which will try to change from 0 to 2:
|
1 |
foo:PRIMARY> session = db.getMongo().startSession()<br>session { "id" : UUID("b312c662-247c-47c5-b0c9-23d77f4e9f6d") }<br>foo:PRIMARY> session.startTransaction()<br>foo:PRIMARY> session.getDatabase("percona").test.update({trx : 0}, {trx: 2})<br>WriteCommandError({<br> "errorLabels" : [<br> "TransientTransactionError"<br> ],<br> "operationTime" : Timestamp(1529675754, 1),<br> "ok" : 0,<br> "errmsg" : "WriteConflict",<br> "code" : 112,<br> "codeName" : "WriteConflict",<br> "$clusterTime" : {<br> "clusterTime" : Timestamp(1529675754, 1),<br> "signature" : {<br> "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),<br> "keyId" : NumberLong(0)<br> }<br> }<br>})<br> |
As we can see, MongoDB catches the conflict and return the error on the insert (even before the commit).
We hope this post, with its simple example how transactions will work, has been useful.
For more information on how multi-document ACID transactions are supported in MongoDB 4.0, watch our webinar, MongoDB 4.0 Features – Transactions & More. Other topics and features covered include future transaction improvements, non-blocking secondary reads and security improvements. We know data integrity is crucial for your business. Our blog, MongoDB: how to use the JSON Schema Validator, explains how to introduce schema validation checks at the database level to enforce the integrity of your data.