A transaction makes a group of changes all-or-nothing, and locks make concurrent transactions take turns; both are only as good as the boundaries and the order you choose.
One seat is left (stock = 1). Priya and Rahul press Buy at the same moment, so two requests run the same code: read the stock, and if it is above zero, write stock − 1 and create an order. Timings are an illustrative model.
Request A
Priya buys
Database
tickets: stock = 1
Request B
Rahul buys
Swipe the diagram sideways to see every column →
Where the time goes
4 ms total
One seat is left (stock = 1). Priya and Rahul press Buy at the same moment, so two requests run the same code: read the stock, and if it is above zero, write stock − 1 and create an order. Timings are an illustrative model.
Illustrative timings. Real numbers vary with distance, network quality and server load.