Two users request the last seat at once. How do you prevent overselling?
After this you can answer: Two users request the last seat at once. How do you prevent overselling?
Say it in 30 seconds
Section titled “Say it in 30 seconds”The deeper answer
Section titled “The deeper answer”The lock covers one event row until commit or rollback. A second booking for the same event waits; after the first commits, it checks the updated count. Bookings for different events need not share that row lock. Plain event-list reads can see an earlier committed version without waiting for this booking.
The transaction also inserts the booking’s outbox event. An error rolls back all its writes, including any seat decrement. Replaying an idempotency key is checked before rejecting a newly sold-out event, so a successful earlier buyer can still recover their original result.
A conditional decrement with a seats-remaining predicate could also be safe inside a transaction. The explicit lock makes the distinct not-found, started and sold-out checks easier to explain. Neither choice removes a hot-event bottleneck; admission control and a clear timeout policy address that separately.
Counter-questions
Section titled “Counter-questions”Can Redis own the last-seat guarantee?
What if inserting the booking fails?
Does more API capacity solve the hot row?
Where in MiniTickets
Section titled “Where in MiniTickets”Read Bookings row locks, then inspect the rds-postgres piece →.
apps/api/src/queries/bookings.ts → createBookingTxRead this book online: https://book.theversehub.in/