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.