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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With