S
S
Stanislav Menshov2019-08-22 01:15:24
MongoDB
Stanislav Menshov, 2019-08-22 01:15:24

How to ensure read consistency?

Hello! Interested in solving the issue in the API service with a stack:
- NodeJS 10
- MongoDb 4.00
- Mongoose ODM
The essence is this: the hotel reservation service provides room availability. But if in 1 second or less 2 users book the last place, then the reservation will work for two at once, since the first request for access to the reservation will work faster than the second one, which will subtract and save. For example:
Two people apply for 1st place and at the same second they both apply at the same time. Both read the available places and both have the last place available. But for the first one, the reservation works earlier and the place is updated, (read = 1-1) - you will write down 0 (not available) for the second one in the same way. It turns out both booked 1 place.
I think you need to dig into Mongo to set up consistency. What do you think?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
res2001, 2019-08-22
@SWEBB

When writing a reservation in the selector, additionally check the condition so that the place is still available.

I
Ivan Shumov, 2019-08-22
@inoise

It is necessary to dig in the direction of ACID transactions, blocking of records and updates of records with conditions. (On the example of SQL: update table set free = 0 where id=1 and free =1 and updated =13677742126)
I seem to have learned in ACID in the latest releases

D
de1m, 2019-08-22
@de1m

There was a similar situation only with logs, which were also written to the database. Here it is necessary to make a queue, that is, everything is thrown into the queue and everyone in the queue checks whether there is free space.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question