LESSON 05 · Write skew

Catch conflicts across different rows

Two transactions can each check a rule, write different rows, and together break it, with no conflict for the database to notice.

SYSTEM VIEW

The interleaving

Diagram view
Bookings—
Conflicts caught—

Watch two buyers take the last of 100 places at once, then replay the race with a row that gives the conflict something to lock.

  1. Section 5 has 100 places, and each sale inserts a booking row. With 99 sold, Buyer A and Buyer B check out at once. Each transaction runs SELECT count(*), sees 99, and finds one place left.

    Rule
    at most 100

    What changed

    • A counts 99
    • B counts 99
  2. Each buyer inserts its own new booking row. Snapshot isolation stops two transactions writing the same row, but these are different new rows, so there is no write-write conflict to detect.

    What changed

    • A’s booking row
    • B’s booking row
    • No conflict found
  3. Both commit, and section 5 holds 101 bookings for 100 places: write skew, though each respected the rule alone. Each new row is a phantom, a row the other’s count would now match, so locking the 99 visible rows wouldn’t stop it.

    Places
    100
    Booked
    101

    What changed

    • A and B commit
    • Bookings 99 → 101
  4. Replay with the conflict materialized: a Section 5 row keeps a sold count of 99. Both buyers run UPDATE … SET sold = sold + 1 WHERE id = 5 AND sold < 100. A takes the row lock and makes 100; B needs the same row, so it waits.

    Update
    sold = sold + 1
    Guard
    AND sold < 100

    What changed

    • Section 5 row, sold 99
    • A holds the row lock
    • B waits
    • Sold 99 → 100
  5. A inserts its booking and commits, which releases the row lock. Only then can B’s update run. That wait is the cost: every booking in section 5 queues on this one row, in a table kept only for concurrency.

    Bookings queue on
    one row

    What changed

    • A’s booking row
    • A commits
    • A’s row lock
  6. At read committed, B’s update now runs against 100, so the guard sold < 100 fails, the update matches no rows, and B reports the section full. At snapshot isolation Postgres aborts B instead, and the retry reaches the same answer.

    Read committed
    0 rows: full
    Snapshot
    abort, retry: full

    What changed

    • B’s update: 0 rows
    • Section full
  7. When the rule is about one key, a constraint is simpler. Replay with reserved seats: both buyers insert seat 14B at once, and a unique index on show and seat lets A’s in and fails B’s. With many rules across rows, use serializable isolation.

    Unique index
    show, seat
    Many rules
    serializable

    What changed

    • Section 5 row
    • A books seat 14B
    • B’s duplicate insert

Space plays · ← → step · Home resets

THE IDEA

How it works

A general-admission section has 100 places, and each sale inserts a booking row. With 99 sold, two buyers check out at once. Each transaction runs SELECT count(*), sees 99, and inserts one row. Both commit, and the section has 101 bookings. Snapshot isolation stopped neither, because they wrote different new rows, so no write-write conflict existed to detect.

This is write skew: transactions read overlapping data, write different rows, and jointly break a rule each respected alone. The same shape appears when two on-call engineers each see a colleague on the schedule and both sign off. The booking case adds a phantom, where a write changes which rows another transaction’s search would match. Locking the 99 visible rows does not stop a new row from appearing.

One fix is to materialize the conflict: create a row that stands for the contended thing, giving locks something to attach to. Keep a sold counter on a section row, and run UPDATE sections SET sold = sold + 1 WHERE id = 5 AND sold < 100 in the booking transaction. The second buyer waits on that row, then at read committed matches no rows and reports the section full. At snapshot isolation Postgres aborts it, and the retry reaches the same answer.

When the rule concerns one key, a constraint is simpler: a unique index on show and seat makes a second insert for seat 14B fail. Materializing adds a table purely for concurrency, and a section’s bookings all queue on one row. With many such rules, use serializable isolation.

DECISION POINT

A meeting-room app stores bookings as rows with a room, a start time, and an end time. Two people book overlapping slots in the same room at the same moment, and both succeed. The database is Postgres at repeatable read, and you have one afternoon to fix it. What do you do?

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

Keep in mind

  • Write skew breaks a rule across rows that no single write conflicts on.
  • A phantom is a new row that would have changed another transaction’s query.
  • Materializing a conflict gives locks a row to attach to, at the cost of a contended row.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
DiagnoseQUESTION 01

Two on-call engineers each check that a colleague remains on the schedule, then sign off. Both commit and nobody is on call. The database ran snapshot isolation and reported no conflict. Why?

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.