Menu

1 / 2

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

DEV Community: postgres·System Design Simple·about 8 hours ago
#hiOda1GM
#devtopostgresql#devto#dev#seat#lock
Long article (485 words) — no AI summary yet.
Reading 0:00
15s threshold

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 ...…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.