What is a race condition
When two people change the same thing at the same moment, the result can go wrong. The answer is to take turns, one at a time.
When two people change the same cell
A note with a number on it is pinned to the wall; right now it says 10. Two people each want to add 1 to it.
One looks at the 10, works out 11 in their head, and writes 11 on the note. The other does exactly the same. But if they start at almost the same moment, both see the original 10 and both write 11.
Adding 1 twice should give 12, but the result is 11. One addition has vanished: an accident caused by changing the same thing at the same moment.
When this happens to a number many people update together, like a view count or a bank balance, a change disappears without a trace.
Changing the same thing at the same moment can throw off the result.
Same actions, different results
Two identical people each add 1 in exactly the same way; this time, let's change only the timing.
Taking turns: one sees 10, writes 11, and finishes; then the other sees that 11 and writes 12. The result is 12, which is right.
Overlapping: one reads 10, and before writing, the other also reads 10. Both write 11, and the result is 11, which is wrong.
The actions were literally identical. Timing alone, when one cut in, decided right from wrong.
That's why these problems are so hard to catch: nine times out of ten everything is fine, and then once in a while it goes wrong.
The same addition is right when done in turn and wrong when it overlaps.
What decided the result wasn't the actions but the timing: when one cut in.
Overlap them yourself
Two workers each add 1 to the same value. Adjust how much their starts overlap.
Put them far enough apart, and one finishes before the other starts, so the result is right. Bring them close, and one reads before the other has written, so one addition vanishes.
Move the slider and find the point where the result starts to go wrong. The two actions are the same, yet the result falls apart the moment they overlap.
Even the briefest overlap can make an addition vanish, so simply hoping the timing won't overlap is no real solution.
Change the amount of overlap to see when the result starts to go wrong.
Keep the two apart and the result is right; the more they overlap, the greater the risk.
Taking turns, one at a time
So how do we prevent it? The answer is simple: while something is being changed, let only one person touch it.
While one person reads, changes, and writes, the other waits, and the next person starts only when that whole bundle is done. It's like lining up for a one-person restroom: when someone is inside, the door is locked, and the next person goes in only after they come out.
This way nothing overlaps, so the result is always right. The trade-off is some waiting time: you accept a short line for the sake of safety.
The key is to guard reading, changing, and writing together as one bundle. Block only the moment of writing while leaving reading open, and the gap appears again.
While something is being changed, only one person touches it; the rest line up and wait.
Having people change the same thing one at a time removes the overlap and keeps the result safe.
Summing it up on one page
There are just three things to remember.
First, changing the same thing at the same moment causes accidents: just as one addition vanishes when two add at once, the result can go wrong.
Second, the problem is timing, not the actions: the same work is right when done in turn and wrong when it overlaps. Third, taking turns one at a time keeps things safe: if only one person touches it while it's being changed and the rest line up, the result is always right.
When several share something and things happen at the same time, order doesn't keep itself. We have to set it.
Simultaneous + shared means accidents, timing is the problem, one at a time is safe.