LOST UPDATE
Two clients, one row, last write wins
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.
-- 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-- 1. Push the arithmetic into the database (atomic):
UPDATE accounts SET balance = balance + 50 WHERE id = 7;
-- 2. Lock the row for the read-modify-write (pessimistic):
BEGIN;
SELECT balance FROM accounts WHERE id = 7 FOR UPDATE;
UPDATE accounts SET balance = 150 WHERE id = 7;
COMMIT;
-- 3. Version check, retry on 0 rows (optimistic):
UPDATE accounts SET balance = 150, version = 2
WHERE id = 7 AND version = 1; 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.
- Exploratory testing and chartersQA and Quality Engineering
- Integration tests with a real databaseSoftware Testing and Quality