Handle Express requests one at a time

Viewed 590

In Express, is it possible to handle requests to a single route one at a time? I have a "checkout queue," and each time a checkout is created, stock is decreased. However, a simultaneous POST request to create a checkout checks the stock before stock is decreased in the first request, which causes overselling. Note: the request only takes about 300 ms, so using Bull or any other queueing technique seems a bit over-the-top. Is there any way I can configure a route to accept requests one-at-a-time?

I am using Firestore as the datastore.

3 Answers

You should use blocking operations on store level like transactions for Firestore.

Node.js by its nature, is a single-threaded application so it cannot execute multiple requests at once. What happens internally in its event loop,it can add callback into an event queue.

enter image description here

You can use async/await paradigm to handle multiple requests.

Related