ThatBrianDude
ThatBrianDude

Reputation: 3190

MongoDB 4.0 Transactions: ACID Read + Write?

So MongoDB 4.0 comes with multi document transactions. My question is, does this enable the same functionality you get with SQL Procedures?

The use case would be

  1. locking a collection
  2. reading from collection
  3. writing to collection according to results from 2.
  4. commiting/aborting

Common db drivers will usually just stack up any commands you issue until you call commit and then run them all after another on the db's machine. So any read I run in my server code are run before the transaction is actually commited and therefore other connections could alter data in between read and write operations.

Will MongoDB 4.0 cover this functionality?

Upvotes: 9

Views: 5053

Answers (1)

Wan B.
Wan B.

Reputation: 18845

Will MongoDB 4.0 cover this functionality?

The short answer is yes for atomicity.

In MongoDB, Transactions (also called multi-documents transactions) are associated with a session. That is, you start a transaction for a session. At any given time, you can have at most one open transaction for a session.

You can not lock the entire collection for writes. You may want to create multiple transactions to ensure that writes are not interlacing/overriding between your processes. MongoDB uses Optimistic Locking instead of Pessimistic Locking.

So any read I run in my server code are run before the transaction is actually commited and therefore other connections could alter data in between read and write operations

Similarly in MongoDB multi-document transactions. For example, using mongo shell:

s1 = Mongo().startSession() 
sessionTest = s1.getDatabase("databaseName").test;
s1.startTransaction() 
sessionTest.find({a:"foo"})
> {_id: ObjectId(..), a:"foo", b:1}

// Let's update the record outside of the session (i.e. another process)
db.test.update({a:"foo"}, {$set:{b:2}})

sessionTest.update({a:"foo"}, {$set:{b:9}})
// You'll get a WriteConflict error because the the document has been modified outside of the session. 

Also note that while the transaction is open, no data changes made by operations in the transaction is visible outside of the transaction.

  • When a transaction commits, all data changes are saved and visible outside the transaction and the transaction ends.
  • When a transaction aborts, all data changes made by the writes in the transaction are discarded without ever becoming visible and the transaction ends.

See also Atomicity Example.

Worth noting that MongoDB is a distributed database, so you also need to be aware of the different options for consistency. You can specify these options when initiating Session.startTransaction() depending on the use case :

Multi-document transactions support read preference primary and all operations in a given transaction must route to the same member.

You may also be interested in Engineering Chalk and Talks: MongoDB Transactions videos which contain some technical explanations behind MongoDB transactions.

Upvotes: 11

Related Questions