i want to prevent my api from multiple api calls at the same time (scenario below)
so, here is my case
- I have an API to decrease my product stock quantity. eg : initial bread = 5
- Let’s say there are 2 api request at the exact same time with each request 3 bread
- when it happens, it will only reduce my stock to 3, because they all get the same last stock, which is five
what i want is that one request is successfully decrease the bread stock to 3 and then the other one failed because i dont have enough bread in my stock
what is the best way to prevent this in nodejs ? thank you