Search code examples
node.jsmongodbmongoosemongodb-query

Document Read and insert with locking/transaction in nodejs with mongodb


In reservation system, only 5 different user can create bookings. If 100 user call booking api at same time than how to handle concurrency with locking. I am using nodejs with mongodb. I went through mongo concurrency article and transactions in mongodb, but cannot find any sample coding solution with locking.

I have achieved solution with Optimistic concurrency control (when there is low contention for the resource - This can be easily implemented using versionNumber or timeStamp field).

Thank you in advance for suggesting me solution with locking.

Now the algorithm is:

Step 1: Get userAllowedNumber from userSettings collection.

//Query
db.getCollection('userSettings').find({})

//Response Data
{ "userAllowedNumber": 5 }

Step 2, Get current bookedCount from bookings collection.

//Query
db.getCollection('bookings').count({ })

//Response Data
2

Step 3, if bookedCount <= userAllowedNumber then insert in bookings.

//Query
db.getCollection('bookings').create({ user_id: "usr_1" })

Solution

  • I had indepth discussion about locking with transaction in mongodb community. In the conclusion, I learn and found the limitation of transaction. There is no lock we can use to handle concurrent request for this task.

    You can see the full Mongodb community conversation at this link https://www.mongodb.com/community/forums/t/implementing-locking-in-transaction-for-read-and-write/127845

    Github demo code with Jmeter testing shows the limitation and not able to handle concurrent request for this task. https://github.com/naisargparmar/concurrencyMongo

    New suggestion are still welcome and appreciate