简体   繁体   中英

How to deal with a race condition

I'm pretty new to web development. From what I've read on race conditions I thought with node or JS they wouldn't be possible because of it being single threaded, but I see that is.. I guess wrong. With this little example can someone explain how it would work.

If there is a bank account with $1000 dollars in it and two people charge the account at the exact same second hitting the server at the exact same time. First person charges $600 and the second person charges $200.

The first charge would do $1000 - $600 leaving the balance at $400. But since the second charge hit at the exact same time it would do $1000 - $200 leaving the balance at $800. When obviously the balance should now be $200.

From my understanding that would cause a race condition, no? How would you set this up to avoid this problem? I don't need exact code just maybe someone to explain this to me, or pseudo code.

Thanks in advance.

EDIT: I'll edit it for how the code would be set up initially causing the race condition.

Like the post below said. The code would be set up so that when the account is hit it would subtract the amount and give the new balance. Obviously that would cause the race condition.

Your example cannot be answered specifically without seeing the exact code being used as there are safe ways to write that code and unsafe ways to write it.

node.js is single threaded, but as soon as a request makes an async call, then other requests can run while that async request is being carried out. Thus, you can have multiple requests in flight at the same time. Whether or not this causes a "race condition" depends entirely upon how you write your code and, in your particular case, how you access the database.

If you write code like this (pseudo-code):

 get total from database
 subtract from total
 write new total to database

And, the calls to the database are asynchronous (which they likely are), then you definitely have a race condition because in between the time you get the total and write the total, other requests could be attempting to access the same total value and attempting to modify it and one request will either not have the latest total value or the two will stomp on each other's results (one overwriting the other).

If, on the other hand, you have a database that can do an atomic modification of the total value in the database as in:

subtract x from total in database

Then, you will be protected from that particular race condition.


Because node.js is single threaded, it is not as complicated to write safe code in node.js as it is in a multi-threaded web server. This is because there is only one path of Javascript executing at the same time. So, until you make some sort of asynchronous I/O call, no other request will literally be running at the same time. This makes accessing shared variables in your node.js app massively simpler than in a true multi-threaded web server environment where any access to a shared variable must be protected by a mutex (or something similar). But, as soon as you make an async call, you must be aware that at that point in time, other requests can run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM