Race conditions and lost updates 00:00:01 Two jobs disappear into one increment The thumbnail queue starts the morning with ten completed jobs. Two workers each finish a different photo. Both report success. The counter says eleven. Which job was lost? Neither thumbnail vanished. The counter update lost part of the history. Each worker reads the same old value, adds one locally, and prepares to write eleven. The mistake happened before the second write. A writes eleven. B writes its own eleven over it. Both writes are individually valid. Together they recorded only one increment. This is one kind of race condition, called a lost update. 00:00:42 Draw the window Imagine freezing worker A immediately after its read. It still believes the database contains ten. As long as another worker can change the value before A’s write commits, that belief can go stale. Give B enough time to finish, then resume A. The jobs and their inputs are identical. Their timing changed the answer. Ordinary tests that accidentally run the jobs one after another may never expose the defect. A test can place a barrier just after both reads. Hold each worker until the other has arrived, release them, then compare the stored count against the twelve that two finished jobs require. 00:01:23 Move the decision next to the value Instead of sending a value calculated earlier, A asks the database to increment the stored value. For example, a single row update can set the total to its current value plus one. The database coordinates the conflicting updates to that row. With the appropriate isolation and successful commits, the second increment applies to eleven. Its final value becomes twelve, regardless of which worker went first. A mutex inside one process protects threads that share that mutex. Two separate worker machines each have their own memory. Neither local lock can force the other machine to wait. 00:02:00 The last available slot The problem gets more expensive when the number represents free equipment. One slot remains in an eight-slot thumbnail pool. Two new handlers both check availability before either claims it. Both handlers can pass a separate check and start work. Now nine jobs compete for eight slots. Correctness requires the decision to reserve a slot and the change to the shared count to happen together. A conditional update can claim capacity only while a slot remains. One request wins. The other sees that nothing changed and can wait, retry with a limit, or tell its caller the pool is full. 00:02:37 Test the invariant, then the retry Suppose the counter is now atomic, but a worker times out after reporting completion and sends the same completion twice. Will atomic increments alone keep the number correct? Both increments could be perfectly serialized and still count one job twice. A stable job identifier and one atomic record of completion can make a repeated notification harmless. The missing increment and the repeated increment have different causes. Protect the shared transition against concurrent writers, then decide what a repeat of the very same operation must mean.