Crysknight
Crysknight

Reputation: 341

Synchronous routes in node.js (regarding express.js)

Well, let's say, I have an auction app (built on express + mongodb), using which, user places orders. My app compares the placed order to others and nulls out the corresponding ones. The problem is, that I need to make the route, that takes in the orders, synchronous for every user, so that the route could process only one request at the time. Because if it will happen asynchronously, I will eventually get the situation in which two users will buy the same order. If there is any way to do this using express or some plug-in, I would be very grateful for suggesting it

Upvotes: 0

Views: 65

Answers (1)

Madhvesh S
Madhvesh S

Reputation: 139

There is something called locking in mongoDB, you can lock your collection to read or write mode so that other requests will wait untill the lock is released.

make sure you release the lock after successful transaction. it may lead to a dead lock or else.

https://docs.mongodb.com/manual/faq/concurrency/#which-operations-lock-the-database

Upvotes: 1

Related Questions