There is one seat left. Two requests read “available” at almost the same moment, both decide to reserve it, and both return success. The bug is easy to miss in a single-user test because each request was locally reasonable. The mistake was treating a read followed by a write as one indivisible decision.
Write down the invariant first
State the business rule in a form the database can enforce: a seat can have at most one active reservation. If reservations are a row per seat, a unique constraint on the relevant seat and event identifiers may be enough. If the rule depends on reservation status, a partial unique index can enforce uniqueness only for active rows. An application-side “is it free?” check is still useful for a friendly message, but it is not the final authority.
Choose an atomic decision
One approach is a single conditional update: decrement remaining capacity only where it is greater than zero, then check whether a row was updated. Another is to lock the seat row with SELECT FOR UPDATE inside a transaction and decide while holding the lock. Both work only if all writers follow the same rule. PostgreSQL documents row locking in Explicit Locking. Keep the transaction short; a network call to a payment provider while holding a row lock invites queueing and timeouts.
Isolation levels do not erase every mistake
PostgreSQL’s default Read Committed level gives each statement a fresh view of committed data. Two separate SELECT statements inside one transaction can therefore observe different states. Serializable isolation can detect some impossible concurrent outcomes and roll a transaction back, but the application must retry serialization failures safely. The transaction isolation guide explains these differences. Raising isolation globally without understanding contention or retries can simply turn duplicate bookings into unexplained customer errors.
Reservations also expire
A booking in progress may hold the last seat while a customer completes payment. Give that hold an expiry time and define whether payment after expiry can still confirm it. The expiry worker and checkout handler must use the same locking or constraint strategy; otherwise they can race each other. Record an immutable attempt ID so retries cannot create a second hold. If a reservation has already expired, return a precise status rather than silently creating another one.
Test with synchronized clients
Start two transactions at a barrier so both try for the final seat together. Assert that exactly one succeeds and capacity never goes negative. Repeat the test against the actual database engine rather than an in-memory mock. Include cancellation, expiry, and a delayed payment callback. Log contention and rejected attempts so a sudden increase in conflicts is visible during a popular sale.
What a constraint can and cannot protect
A unique constraint stops two active reservations from committing for the same seat, even if an overlooked code path skips the friendly availability check. It will not automatically restore a payment that was captured before the losing transaction failed. Reserve first, commit the database change, and only then move into a payment step with an explicit pending state. If payment fails, release the hold in a separate, idempotent transition. When capacity is stored as an aggregate counter, use a conditional update and check the affected row count; a plain read-then-decrement in application code reintroduces the race.
A test that makes the race reproducible
Do not rely on a hundred requests fired at random and hope they overlap. Put both workers behind a barrier immediately before the contested database operation, release them together, and record the outcome. Run this against PostgreSQL with separate connections: many test frameworks wrap each test in a transaction or share one connection, hiding the real behavior. Assert the final database invariant, not merely the HTTP status codes. Repeat with the expiry worker acting as a third contender, because a hold that expires at the same instant as payment confirmation is where optimistic assumptions tend to fail.
Sources
PostgreSQL: Transaction Isolation; PostgreSQL: Explicit Locking; PostgreSQL: Unique Indexes.



