Transaction
Creating an order and deducting stock must succeed together; if any step fails, it must not leave half an order behind.
Creating an order and deducting stock must succeed together; if any step fails, it must not leave half an order behind.
The order exists, but stock was not reduced.
Problem and key mechanism: When order creation and inventory deduction are handled separately, a stock failure can leave an incomplete order behind. Putting both in the same transaction ensures that if either step fails, all order and inventory changes are rolled back.
How to observe the improvement: Commit the order and inventory changes together only after both succeed. Return the order ID on success, and return a specific reason on failure instead of presenting partial work as success.
Clinic booking also needs a transaction: Creating an appointment record and reserving the doctor's time slot belong in the same transaction. If the slot is unavailable, both roll back so no appointment is left without a slot.
Inspect the checkout flow and put order creation and inventory deduction in the same database transaction. Simulate an inventory deduction failure and verify that neither an order record nor partial inventory changes remain. Commit and return the order ID only when both succeed. Do not assume external payments or emails will roll back automatically.