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.