Concurrent Checkout Bug Lets 20 Buyers Purchase One Ticket, and Two SQL Fixes
A developer found that a check-then-act purchase function in Node.js and PostgreSQL oversold a single concert ticket up to 20 times, even though the stock count still ended at zero. Twenty simultaneous buyers all read the stock before any write occurred, so each passed the availability check. Wrapping the logic in a plain transaction did not solve the problem under PostgreSQL's default isolation level. The article presents two fixes: a single atomic UPDATE with a stock condition, and SELECT ... FOR UPDATE to lock the row while the transaction runs. Both approaches prevented overselling in the author's tests.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in