What are race conditions and why do they happen?
I won't lie, I often heard the term race conditions but always assumed I understood what it meant, but when I tried to explain it, I just couldn't find the right words. I've always understood race conditions, speaking in the context of making network requests, as an undesired behavior that occurs when multiple requests are in-flight that modify the same or similar things.
This has always been an assumption of mine though, hence I dug into what race conditions really are and wrote this article.
By definition: A race condition happens when two or more operations are happening at the same time, and compete for which one will be executed first.
Race conditions are common in multi-threaded applications.
Race conditions can cause unexpected bugs or behaviors in our programs.
I made a visual below. Let's say for instance user spam clicks a button that makes requests to the server.
At least my understanding of race conditions is more clear, and I hope yours too.