Guided challenge · About 15 minutes · Optional Python lab · Distributed Systems
The Order Failed. But the Customer Was Charged.
Maya paid $80 for shoes. The warehouse had none left. How would you cancel the order and return her money?
Answers stay in this browser until you save them on this device.
What happened?
Maya buys a pair of shoes for $80.
The payment succeeds.
The warehouse then reports that the shoes are unavailable.
The order cannot be completed, but Maya has already been charged.
Payments and inventory are managed by separate services.
Your task
How would you recover when payment succeeds but a later step fails?
For this exercise
- Simulated payments and refunds only; no real money moves.
- Plain-English answers are enough for the main questions.
- Optional Python lab runs on your device with three local SQLite databases.
- A timeout from the warehouse is not the same as an out-of-stock rejection.
Optional hints
- Each service has already saved its own work.
- Returning money is another operation that must be tracked.
- Keep the unfinished work so it can continue after a restart.
Related path: System Design Interview Sprint
Interview answer
Explain it in an interview
Maya paid 80 dollars for shoes, and the warehouse had none left. How do you cancel the order and return her money?
Maya bought shoes as order 4821 for 80 dollars, which is 8000 cents. Payment succeeded. The warehouse then reported that it could not reserve the shoes. She has been charged, and the order cannot complete until that charge is refunded.
Step 1
The charge stuck after stock failed
This lab charges first and reserves inventory second, so payment recorded 8000 cents for order 4821 before the warehouse could hold the shoes. If I only deleted the order, the payment row would still say she paid. The charge and the stock check are not one commit.
Step 2
Deleting the order leaves the charge
Starting the order over from scratch can charge her again. Refunding without a stable refund key can record two refunds when the first reply is lost. Reserving stock before payment is a valid alternative, and this lab does not implement it. I should describe the compensation path that is actually here.
Step 3
Refund the full charge when stock is gone
The coordinator moves order 4821 through payment, reservation, and refund steps. When stock is gone I ask for a full refund of the same 8000 cents, not a partial amount. The payment key and the refund key identify those calls. The order stays in refund pending until that refund is known.
Step 4
Save the step before you continue
Order, payment, and inventory each have their own SQLite file, so there is no single commit across them. I persist the coordinator state before I rely on it after a crash. Recovery reads that state and continues the unfinished step. It does not create a second order for the same work.
Step 5
Look up the refund before you create one
If the refund call times out, I look up the refund by its key before I create another one. Attempts back off instead of hammering the payment service. A coordinator restart resumes refund-pending work. If I still cannot tell what happened, the order can wait for review instead of my guessing.
Step 6
Compensation is not one atomic purchase
The refund is a simulated full reversal of a simulated charge. There is no partial refund, no reservation expiry, and no production authentication. I would not say the purchase was atomic across services. Recovery is a sequence of local commits, not a button that erases the charge by itself.
Do not stop here
I would delete order 4821 when the warehouse has no shoes, and assume the 80 dollar charge disappears with it.
Say this boundary
I keep order 4821 in refund pending until one full 8000 cent refund is recorded under its refund key, and I look that key up before I refund again.
Leave these claims out
- The charge and the reservation commit together
- Deleting the order undoes the payment
- A timeout means the refund did not happen
I would walk order 4821 from the 80 dollar charge to the failed reservation and then to one refund. After a lost reply I look up the refund key before I ask for the money again.
Now say the same idea in your own words, using the story from this challenge.