Transaction lock in MongoDB
Asked Answered
G

1

8

I am trying to develop a booking system, that books different assets after checking its availability. The system first tries to read records from the DB and checks if the slot being booked is available. If so, the system books the slot for them by inserting a new record into the system.

Now the problem is, if there are multiple users requesting booking, and since multiple requests to the db can interleave, it will be possible for this scenario to occur.

User 1 -> read request start
User 2 -> read request start
User 1 -> read request success (booking available)
User 2 -> read request success (booking available)
User 1 -> write a new record to the db (new booking)
User 2 -> write a new record to the db (new booking) but this conflicts with User 1.

To avoid this situation, I ideally have to acquire a collection level lock even before the read operation starts and only release the lock after the final write is done.

Is my understanding correct? If so can this be done in MongoDB?

Any solution with MongoDB, or Mongoose or @tsed/mongoose are very welcome.

Gassman answered 17/8, 2018 at 21:51 Comment(0)
S
8

There are no transaction support in the MongoDB before 4.0 version. For using transactions in 4.0 have a look on https://www.mongodb.com/transactions

In versions above 4.0 you can use findOneAndUpdate method to emulate it.

For example, you can do the modify query as this:

record = db.collection.findOneAndUpdate({"in_transaction": {"$exists": False}}, {"$set": {"in_transaction": true}})

Now even if you run two queries concurrently only one will return the document.

Shackleford answered 18/8, 2018 at 7:2 Comment(3)
findOneAndUpdate was introduced in v3.2 - docs.mongodb.com/v3.2/reference/method/…Regale
Technically speaking, 3.2 is above 4.0, indeed.Shackleford
Some additional info at mongodb.com/blog/post/…Virgina

© 2022 - 2024 — McMap. All rights reserved.