What the puzzle models
Each step is one statement, and you are the operating system deciding which connection gets the CPU next. The database behaves like Postgres at its default isolation level, READ COMMITTED. A read sees whatever was committed when the statement ran. Writes stay private until COMMIT. An UPDATE takes a row lock that is held until the transaction ends, and anyone else who wants that row waits.
None of the code in the levels is wrong line by line, and every version passes a test that runs one request at a time. The bugs only exist in the gaps between statements.
Lost updates and check-then-act
The counter and the withdrawal have the same shape: read a value into the application, decide something, write back a value computed from what was read. If another transaction commits in between, the write is based on a value that is no longer current. The row lock doesn't help. The second UPDATE does wait for the first to commit, but then it writes the stale number it already computed.
A second check just before the write (the first wrong fix in level 5) makes the window smaller, so it fails less often in testing. It doesn't close the window. Wrapping the code in BEGIN doesn't either. Atomicity means all or nothing. It doesn't mean no other transaction runs at the same time.
Three fixes that hold
- Atomic conditional update.
UPDATE … SET balance = balance - 80 WHERE balance >= 80moves the check into the statement that writes. When the second transaction gets the lock, Postgres re-reads the committed row and re-checks the condition. Use this when it fits in one statement. It is the cheapest fix and needs no retry logic. - Pessimistic locking.
SELECT … FOR UPDATEtakes the lock at read time, so the read, the check and the write all happen under one lock. Simple to reason about, at the cost of other transactions queueing behind you. - Optimistic concurrency. Read a version number with the row and write only
WHERE version = :v. Nobody waits while reading. A conflict shows up as zero rows updated, and the application retries. This works well when conflicts are rare. It is also how you protect a read-modify-write that spans an HTTP round trip, where you can't hold a lock.
The checker in level 5 doesn't sample. It enumerates every order the scheduler could choose, including attempts that block, and runs each one. For two short transactions that is at most a little over a thousand schedules, so "0 of N" is a proof for this model.
Write skew
The doctors level is the odd one out. The two transactions write different rows, so no lock ever conflicts and no update is lost. Each transaction checks a condition over a set of rows and then changes the set. Snapshot isolation (Postgres's REPEATABLE READ) doesn't catch it either, because neither transaction writes anything the other one wrote. The fixes are SERIALIZABLE, which tracks read and write dependencies and aborts one transaction, or explicitly locking the rows the check depends on.
Deadlocks and lock ordering
Two transfers that lock their accounts in opposite orders can each end up holding the lock the other one needs. Postgres checks for the cycle after deadlock_timeout and aborts one transaction with an error, and the client has to retry. The fix doesn't involve the database: always take locks in the same order, for example the lower account id first, and the cycle can't form. Retrying on serialization and deadlock errors is still worth having, because some conflicts can't be designed away.