1

Let's say I have a global key-value pair and operations on it returns a promise(actually I am using Redis), and it stores the value of coins left.

Route /path decrements the coins with the number in the request if the quantity left is greater than 0.

Initially, it has a value of say, 10. Now if two requests came simultaneously for 10 and 9 could both of them read length as 10 and inconsistency occur?

What I know is, promises have callbacks and when the first request to getQuantity() is made, the event loop can process the next request and this request then can read quantity as 10, which could result in inconsistency.

app.get("/path",(req,res)=>{

 const decrementBy=req.value;
 const quantity=await obj.getQuantity(); 
 if(quantity-decrementBy>=0){
  await setQuantity(quantity-decrementBy);
 }
 res.send();

})
4
  • 1
    Yes, this is a race condition because of the use of await. You should be using an atomic operation in redis for decrementing the value. Though JS is single threaded, when you await a promise, other code gets to run and you could very well get two pieces of your code both fetching the value of 10 here and then each trying to decrement it from there, one overwriting the other. You should be using an atomic decrement operation in redis to prevent this.
    – jfriend00
    Commented May 15, 2020 at 21:40
  • @jfriend00 Is there some way for read lock in Redis, or I have to use database transactions in this scenario. Also please suggest other solutions, if any. Commented May 15, 2020 at 21:46
  • I don't know redis much myself, but it looks like you should use the DECRBY command which will do an atomic decrement without ever retrieving the value yourself and without a race condition. A simple Google search finds plenty of discussion of locks such as here and here.
    – jfriend00
    Commented May 15, 2020 at 22:05
  • You may also want to look at MULTI and EXEC for atomic transactions.
    – jfriend00
    Commented May 15, 2020 at 22:11

3 Answers 3

2

To maintain consistency you could use a lock for your quantity where quantity will remain locked until one of the requests is complete.

2
  • I first wanted to ensure, could there be inconsistency in this scenario? Commented May 15, 2020 at 21:20
  • 1
    Yes, if 2 requests come exactly at the same time, it can happen. In fact, I worked on an e-commerce site where I faced a similar issue.
    – Nafeo Alam
    Commented May 15, 2020 at 21:32
1

There will be inconsistency if more than one requests are coming at the same time.

Chances of inconsistency ∝ Number of I/O operation with in request and Number of node processes accessing redis.

As redis is not a transactional data store so redis does not fulfill ACID properties. In order to maintain consistency you can use locking (https://redis.io/topics/distlock). For nodeJs node-redlock is the client to use.

0

Node.js is single threaded. So no two pieces of code can execute simultaneously. Only one of them will execute at any one time. The non-deterministic part is you cannot predict which request will be processed first.

Asynchronous I/O means parallel wait, not parallel code execution.

2
  • What I want to say is,req1 with value 10 and req2 with 9 is received simultaneously at the server.Now say, req1 is executed first. Now getLength() for req1 is executed and further execution waits for the promise to resolve, and by that time event loop is completely free to process req2, and therefore, getLength() for req2 is called. So both should give 10 which is inconsistent. Commented May 15, 2020 at 21:28
  • But, the OP's code is a race condition. They get the value with an asynchronous operation, do something with it and then set it with another asynchronous operation. That's capable of a race condition. Two pieces of their code could both get the value 10. As I'm sure you know (but didn't see this in the OP's code), single threadedness only protects you between lines of code when NOT awaiting an asynchronous operation between them.
    – jfriend00
    Commented May 15, 2020 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.