LOST UPDATE

Two clients, one row, last write wins

L215 MINRACE CONDITIONS
PREDICT

Balance is 100. Client 1 reads it and will write 150. Client 2 reads it and will write 70. Each SELECT and UPDATE autocommits on its own. Which final balances are possible?

Two clients read the same row. Each computes a new value from what it saw. Each writes that value back with a plain UPDATE. The second write replaces the first, and the first client’s change is gone. The database cannot object: it was handed a number, not a delta, and it stored the number.

This is the data race from the previous lesson, moved into a database. The load is a SELECT, the add happens in your application, the store is an UPDATE. The fixes are the same two families: make the read-modify-write one statement, or make the second client wait or fail.

▶ SIMULATION
CLIENT 1 (+50)
saw: —
idle
DATABASE · accounts id=7
balance = 100 v1
committed: none
row lock: free
CLIENT 2 (−30)
saw: —
idle
✓ balance reflects every committed write
SCHEDULE
01[C1]C1: SELECT balance FROM accounts WHERE id = 7
02[C2]C2: SELECT balance FROM accounts WHERE id = 7
03[C1]C1: UPDATE accounts SET balance = saw +50; COMMIT
04[C2]C2: UPDATE accounts SET balance = saw −30; COMMIT
Start of lost update scenario. 4 steps.
-- Client 1 (+50) and Client 2 (-30) both run, autocommit:
SELECT balance FROM accounts WHERE id = 7;   -- both see 100
-- ...app computes new balance in memory...
UPDATE accounts SET balance = 150 WHERE id = 7;  -- C1
UPDATE accounts SET balance = 70  WHERE id = 7;  -- C2: +50 is gone

The invariant strip checks balance == 100 + every committed write. In LOST UPDATE it goes red on the last step. In the three fixes it stays green under the same overlap: with the atomic update there is no separate read at all, with FOR UPDATE the second client’s read waits for the first to commit, and with the version check both read the same value but the second write matches 0 rows and retries.

BREAK IT NOT YET

Each client runs a SELECT then an UPDATE. Order the four statements so that one committed write disappears from the balance.

GOAL: both clients commit and balance != 120

FIX IT NOT YET

Rewrite applyDelta so two clients calling it concurrently (one with +50, one with −30) always leave the balance at start + 20. The grader tries every interleaving of the two clients’ statements.

Mental model:

a row is shared memory with a network in the middle. Any time your code reads a value, does arithmetic, and writes the result back, you have a read-modify-write, and it is only safe if the database can see the whole thing as one operation. Send the delta, hold the lock, or check the version. A value computed in your process is stale the moment it arrives.

IN PRODUCTION

Account balances, stock counts, like counters, quota and rate-limit buckets, any ORM save() that writes back a whole object it loaded earlier. The symptom is a total that does not add up, discovered by reconciliation weeks later.

IN INTERVIEWS

“Two users edit the same record. What happens?” Say that the last write wins and the first edit vanishes. Give the three fixes, say which isolation level stops it (Postgres REPEATABLE READ aborts the second writer; READ COMMITTED does not, and neither does MySQL InnoDB REPEATABLE READ, whose UPDATE reads the latest row and overwrites it), and mention that an ETag on a REST PUT is the optimistic version check in HTTP clothing.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: CHECK-THEN-ACT →