When implementing a typical meeting room booking system with RDBMS, there will be two operations:

-- Check for any existing bookings that overlap with the period of noon-1pm
SELECT COUNT(*) FROM bookings WHERE room_id = 123 AND
end_time > '2015-01-01 12:00' AND start_time < '2015-01-01 13:00';

-- If the previous query returned zero:
INSERT INTO bookings
(room_id, start_time, end_time, user_id)
VALUES (123, '2015-01-01 12:00', '2015-01-01 13:00', 666);

code from: Designing Data Intensive Applications (page 249) -- Martin Kleppmann

And we'll need to set the isolation level to serializable to avoid the case where two users think there are no conflicts and insert two conflicting bookings.

My question is:

Assuming that we model each booking time slot as a document, how do we implement a system that can avoid conflicting bookings in MongoDB?

  • I am more curious about how MongoDB handles concurrent transactions.

  • Let's say I want to allow users to book any timeslot possible, not just the predefined time slot.

  • Suppose user A wants to book timeslot T1, and B wants to book timeslot T2, and T1 and T2 overlap with each other. It's very possible two users get to the booking pages at the same time, and check with MongoDB and find no conflicts, and they both decide to insert the two timeslots that conflict with each other into DB.

  • In RDBMS, we set the isolation level to solve this issue, what about MongoDB?


Browse other questions tagged or ask your own question.