Cancel and Ready tapped at the same moment. Who wins?
transcript
You tap cancel. The kitchen taps ready. Same moment. With a plain status field, both can win.
A plain status field: last write wins. Ready, with the money refunded. A partner collects unpaid food. Or cancelled, with no refund. The policy saw a stale status. The fix: every change is a transition, checked against one table. State and refund change together, under one lock. One call wins.
Placed, accepted, ready, picked up, delivered. Cancel while placed: full refund. While cooking: half. Once packed: nothing. Picked up? There's no cancel at all. The table says no.
But here's the catch. Fifty cancels hit one order at once. A double tap. A support agent. An auto-cancel timer.
Under the lock, the first sees accepted. Half of 220: 110 rupees. Same step, the order moves to cancelled. The other 49 are refused. The refund call goes out after the lock. Exactly once.
Decide the refund with the state change. Send it after. Every rupee charged ends in a meal, or a refund.
Full breakdown in the lecture.