One tin of ghee left. Two customers paid for it.
transcript
One tin of ghee left. Two customers just paid for it. No crash. No payment bug. Just two checkouts at the same moment.
Checkout A reads the stock. It's 1. Checkout B reads it too, a split second later. Also 1. Each checks in code. Enough stock. Each writes 0. Both commit. Two orders, one tin. That's a lost update.
So raise the isolation level? Here's the catch. It depends on the database. PostgreSQL's Repeatable Read throws an error at the second writer. Now you need retries. MySQL's Repeatable Read? The update silently overwrites. Both checkouts still succeed.
The real fix is one line. Stop reading, then writing. Subtract one, but only where stock is at least one.
Checkout A changes one row, and commits. Checkout B waits for it. Then its check runs again. Stock is 0 now. B updates zero rows. Zero rows means sold out. Safe, even at the default level.
Don't decide in your code. Let one statement decide.
Full breakdown in the lecture.