Basis Lab

Doing Two Things at Once

The Update That Vanished

Adding a counter looks like one action and is three. Two workers doing it at the same time can both read the same value, and one of the two additions disappears with nothing broken.

Here is the shortest possible version of the whole subject. A page view counter is stored somewhere. Two requests arrive at the same instant. Each one adds one. Afterwards the counter has gone up by one.

Nothing crashed. No error was logged. No component was unavailable. The code that did it is three lines long and obviously correct when you read it.

One operation, three steps

Adding one to a stored number is never one action. It is fetch the current value, compute a new value, store the new value. Those are three separate things with gaps between them, and the worker can be interrupted in either gap.

FIG 1Where the gap is, and what can arrive in it
There is no faulty step in this diagram. Each worker did precisely what it was told, in order, with no interruption to its own logic. The defect is in the arrangement rather than in either worker, which is why reading one worker's code carefully will never reveal it.
FIG 2Two workers incrementing the same counter, step by step
stepworker one's held valueworker two's held valuethe stored counterincrements requested so farwhat happened
100410Starting state. Nobody has read anything yet.
2410411Worker one reads. It now holds 41, and the stored value is still 41.
34141412Worker two reads, in the gap. It holds 41 as well, which is true at this instant and will not be true for long.
44241422Worker one writes its result. The stored counter is correct right now.
54242422Worker two writes its result, computed from a value that is now stale. Two requests, one increment.
5 steps
Follow the third column. It is correct at every single step, including the last one, in the sense that it always holds a value somebody computed honestly. What is lost is not a value but an event, and no record of the loss exists anywhere.

Why you will not find it in testing

The window between the read and the write is small. For an in-memory counter it may be a few tens of nanoseconds; for a counter in a database behind a network call it may be a few milliseconds. Two workers have to arrive inside the same window for anything to go wrong.

FIG 3The same code, the same window, and a hundred times the traffic
steptimes the operation runs in an hourpairs of runs that could land in one windowpairs, against the first rowwhat happened
1100100 times 99, halved, so 4,9501 timeThe traffic the code was written under and passed review at. Four thousand nine hundred and fifty chances to go wrong, each one far too unlikely to have been seen once.
21,0001,000 times 999, halved, so 499,500101 timesTen times the traffic, and not ten times the exposure. Every new operation can collide with every operation already there, so the count of pairs climbs with the square of the traffic.
310,00010,000 times 9,999, halved, so 49,995,00010,100 timesA hundred times the traffic and ten thousand one hundred times the chances. Nothing in the three lines of code is different from the first row, and this is where it becomes a daily event.
3 steps
The window from the read to the write is the same width in all three rows, because it is a property of the code and the code never changed. What moved is the middle column, and it moved a hundred times further than the traffic did. A race therefore arrives with growth rather than with a release, which is why nobody can name the change that introduced it.
FIG 4Expected collisions in an hour, against how wide the window is
0.002.004.006.008.000.0125.0250.0375.0500.0window width, in microseconds
a thousand operations an hourten thousand operations an hour
Each line is the pair count for that traffic times the chance one pair lands inside the window, which is the window divided by the 3,600,000,000 microseconds in an hour. Both pass through the origin, which is the reassuring part, and both are straight, which is the part that gets people: there is no window narrow enough to be safe, only one narrow enough that nobody has noticed yet. Ten times the traffic lifts the line by a hundred, not by ten, because it is pairs that collide and not operations.

A test suite runs the operation a few hundred times, usually in sequence, and passes. The system then runs it ten million times a day with real concurrency, and the counter drifts by a few tenths of a per cent a week. By the time anybody investigates, the evidence is a number that is slightly wrong and no log line anywhere that says why.

The same shape, wearing different clothes

Counters are the teaching example because they are easy to draw. The pattern is much broader, and it covers anything that checks a condition and then acts on what it saw.

FIG 5Four everyday operations with the same gap in them
how often it appears, one to fourhow bad the failure is, one to fourhow hard it is to notice, one to fourhow easily the database can fix it, one to four
add one to a counter4234
create the record if it is absent3443
spend from a balance if there is enough3324
claim the next job from a queue2442
The marked cell is the one that funds the incident reviews. Two workers both check that no account exists for an address, both find none, and both create one, after which every later operation has to decide which of the two duplicates is the real account.

In each of these, two workers look at the world, both conclude that an action is safe, and both act. The check was true when it was made. It stopped being true before the action, and nothing told anybody.

The machine has its own ideas

One more complication, because it surprises people who have understood everything above. Even without any interleaving of your steps, a worker can read a value that another worker has already changed.

Each processor keeps its own copy of recently used memory. A write made on one processor becomes visible to another only when the machine arranges for it to be, and both the compiler and the processor are permitted to reorder operations that appear independent. Two assignments made in one order can become visible to another worker in the opposite order.

This is why every serious language now publishes a memory model, which is a precise statement of when one worker's write is guaranteed to be seen by another. The practical consequence is simple enough to remember: unless you have used something the language promises will synchronise, you have no promise at all about what another worker can see.

What to hold on to

An operation that reads and then writes has a gap in it, and a second worker inside that gap makes the result wrong without breaking anything. The failure is rare in proportion to how narrow the gap is, which means it survives testing and arrives with traffic. The same shape appears wherever a check is followed by an action. And separately from all of that, a worker is not guaranteed to see another worker's writes at all unless something was done to make it so.

Recap

  • Almost every operation that looks atomic is really a read, then a change, then a write, and the gap between the read and the write is the window in which another worker can ruin the result.
  • These failures are rare by construction, because the window is microseconds wide, which means they survive testing, appear under load, and cannot be reproduced on demand.
  • The pattern is not limited to counters. Anything that checks a condition and then acts on it has the same gap, which is where duplicate records and negative balances come from.

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

NextThe Part Only One May Enter →

The rest of this course

  1. 01The Update That Vanishedyou are here
  2. 02A Door That Admits Oneopening only
  3. 03The Bill Arrives as Latencyopening only
  4. 04Everybody Holding, Nobody Movingopening only
  5. 05Try, Check, Try Againopening only
  6. 06The Promise Is Weaker Than You Thinkopening only
  7. 07The Ceiling You Cannot Buyopening only
  8. 08The Problem You Decide Not to Haveopening only

Read alongside