DEV Community

Cover image for Two fans click the same seat at the same moment. Who wins?
System Design Simple
System Design Simple

Posted on Originally published at systemdesignsimple.com

Two fans click the same seat at the same moment. Who wins?

Two fans both see the same seat as free, both click buy, and the seat is sold twice unless the claim is one atomic step. The fix is a single conditional UPDATE, and you branch on the row count it returns.

The bug: check-then-act

The naive claim is two statements: SELECT status, then, if the app saw free, UPDATE. The time between the SELECT snapshot and the UPDATE taking its row lock is the race window. Any transaction that reads inside that window makes the same decision on the same stale fact.

In our example both sessions read free, both UPDATE, both COMMIT, and both users are told "yours". Nothing failed. The second UPDATE simply overwrote the first: a lost update. Wrapping both statements in BEGIN ... COMMIT at the default Read Committed level changes nothing, because a plain SELECT takes no lock and the UPDATE never compares against what was read.

The fix: one conditional UPDATE

UPDATE seats SET holder = 'you'
WHERE seat = 'A12' AND status = 'free'
RETURNING seat;
Enter fullscreen mode Exit fullscreen mode

One row wins, zero rows means someone else got it first. Under Read Committed, the second UPDATE blocks on the row lock, and when the first transaction commits, PostgreSQL re-evaluates the WHERE clause against the updated row version, finds the status is no longer free, and skips the row. It reports UPDATE 0. That re-check is what makes the single statement safe.

When one statement is not enough

  • The rule needs app logic: lock the row first with SELECT ... FOR UPDATE, decide in the app, then write. The lock serializes the deciders.
  • Any of many rows will do, like the best available seat or a job queue: use FOR UPDATE SKIP LOCKED so each worker locks rows nobody else has locked and skips the rest instead of queueing behind each other.
  • Repeatable Read: a concurrent update of the same row fails with SQLSTATE 40001, so you retry the whole transaction. Write skew across different rows is still possible.
  • Serializable: snapshot isolation with predicate locks catches write skew too, but you still retry on 40001.

Safety nets around the write

A unique constraint on the seat is the net under everything: even a bug in the app cannot insert a second owner. Holds should expire on read, comparing hold_expires_at with the database's own now(), never an app server clock. And put an idempotency key on the payment, so a retried request charges once even if the claim is retried.

One last split matters: seat ownership must be linearizable on the primary, because two owners is a wrong answer. The seat map display can be eventually consistent, because a stale map is only a stale picture.

Read the full article, with the worked timeline, the isolation level table, deadlocks, and the interview version, on System Design Simple: https://systemdesignsimple.com/two-fans-one-seat/

Top comments (1)

Collapse
 
sunnatulla_dev profile image
Sunnatulla •

Careful with the snippet as written: it sets holder but never changes status, so the Read Committed re-check still sees 'free'. I raced two sessions on PG17: both got 1 row back, final holder bob, status still free. With SET holder = $1, status = 'held' bob gets 0 rows as you describe.