Atomicity
When two users buy the last item at once, there cannot be two successful orders; checking and deducting stock must act as one whole operation.
When two users buy the last item at once, there cannot be two successful orders; checking and deducting stock must act as one whole operation.
Separated read and write expose an intermediate state.
Problem and key mechanism: When two requests read the last item separately from deducting it, both may believe they can succeed; make the check and deduction one indivisible action so only the request that still meets the condition decrements stock and the other fails as a whole.
How to observe the improvement: The other request should fail completely with an out-of-stock response, without producing a second successful order. Then verify that remaining inventory, the successful order count, and the failure feedback all agree.
Claiming a support ticket also needs atomicity: When two agents claim the same ticket, checking that it is unassigned and assigning the owner must be one atomic operation. Only one agent succeeds; the other is told it was already claimed.
Inspect the purchase flow: when inventory is 1, simulate two concurrent requests and make the inventory check and deduction one indivisible database operation. Verify that exactly one order succeeds, inventory becomes 0, and the other request receives out-of-stock; do not treat a disabled frontend button as concurrency protection.