LESSON 04 · Lost updates

Stop writers overwriting each other

When two transactions read a value and write back something computed from it, one change can silently vanish.

SYSTEM VIEW

The interleaving

Diagram view
Balance—
Debits kept—

Watch two payments debit one wallet at the same moment, then replay the race with fixes that keep both debits.

  1. A wallet holds $100. Payment A will debit $30 and Payment B $50. Each reads the balance first, and both see 100, because neither has written yet.

    What changed

    • A reads 100
    • B reads 100
  2. Each subtracts in its own code: A works out 70 and B works out 50. A writes its 70 and commits, so the balance is now 70.

    What changed

    • A computes 100 − 30
    • B computes 100 − 50
    • Balance 70
  3. B writes the 50 from its stale read and commits: the balance should be 20 but is 50, and A’s debit has vanished. Read committed allows this lost update; Postgres’s repeatable read would abort B, MySQL’s would apply it.

    Should be
    $20
    Is
    $50

    What changed

    • Balance 50
    • A’s $30 debit
  4. Replay the race with UPDATE … SET balance = balance − 30, so the database does the arithmetic. A’s update takes the row lock, makes 70, not yet committed, and reports 1 row. B’s update needs the same lock, so it waits.

    Logic in code
    SELECT … FOR UPDATE

    What changed

    • Both send an atomic UPDATE
    • A holds the row lock
    • B waits
    • A’s update: 1 row
  5. A commits, and the row lock passes to B. B’s update runs on the latest value, 70, leaves 20 and commits, so both debits are kept. A guard such as AND balance >= 50 turns too little money into 0 rows updated, never a negative balance.

    Guard
    AND balance >= 50
    Too little money
    0 rows updated

    What changed

    • Row lock passes to B
    • B’s update runs on 70
    • B commits 20
  6. A form left open for minutes shouldn’t hold a lock: writers would queue behind it, as they do behind SELECT … FOR UPDATE. Replay the race with compare-and-set: both payments read 100 at version 7 and hold no lock.

    Form open for minutes
    compare-and-set

    What changed

    • Each read returns a version
    • No lock held
  7. A writes 70 only if the version is still 7. It is, so the row moves to version 8 with the new balance. The check and the write are one statement, so nothing can slip in between.

    What changed

    • Version 8 holds 70
  8. B’s write also asks for version 7, but the row is now at version 8, so it matches zero rows and changes nothing. B re-reads 70 and recomputes 20 rather than resending, so its debit isn’t lost. Under heavy contention, retries pile up.

    B’s debit
    retried on 70
    Heavy contention
    retries pile up

    What changed

    • B’s write: 0 rows
    • B re-reads 70, then retries

Space plays · ← → step · Home resets

THE IDEA

How it works

A wallet holds $100. Payment A debits $30 and Payment B debits $50, and each reads the balance, subtracts in code, and writes the result. Both read 100. A writes 70 and commits. B writes 50 and commits. The balance should be 20, but it is 50, and A’s debit has vanished. This is a lost update, and read committed allows it, since nobody saw uncommitted data.

An atomic operation lets the database do the arithmetic: UPDATE wallets SET balance = balance - 30 WHERE id = 7 AND balance >= 30. The second writer waits for the row lock, then applies its change to the latest value, and zero affected rows means insufficient funds. Use this when the change fits in one statement.

A row lock covers logic that does not fit. SELECT ... FOR UPDATE holds the row until commit, so a second transaction asking for the same lock waits. Do not hold it across a slow network call, since other writers queue behind you. Compare-and-set checks that nothing changed since you read: UPDATE ... SET balance = 70, version = 8 WHERE id = 7 AND version = 7. Zero rows affected means someone got there first, so re-read and recompute. It holds no lock between read and write, which suits a form left open for minutes, but retries pile up under heavy contention.

At repeatable read, Postgres aborts the second writer with a serialization error for the application to retry. MySQL InnoDB’s repeatable read does not detect this, and the second update simply applies.

DECISION POINT

A venue tool lets managers rearrange the pricing tiers of a section. A manager may keep the screen open for ten minutes before saving the whole layout, and last week one manager’s changes silently disappeared when another saved. Which guard do you choose?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • A read-modify-write cycle in application code can lose updates at read committed.
  • Prefer an atomic update, then a short row lock, then compare-and-set when read and write are far apart.
  • A compare-and-set that affects zero rows means re-read and recompute, not resend.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

Two admins can edit the same customer record in a form they may hold open for several minutes. Which guard against lost updates fits?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.