BayLearnBayAreaLa8s

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.

Two workers, one waiting reportReport #4821WaitingCreate daily sales reportServer AWorkerServer BWorker
Two workers find the same report job. Both start building it. How would you let only one claim it?
Both workers see “Create report #4821.” Each thought the job was still available.

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
  1. Each service has already saved its own work.
  2. Returning money is another operation that must be tracked.
  3. Keep the unfinished work so it can continue after a restart.
0/4000
0/4000
0/4000

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.

How to walk the interviewer through the answer1The charge stuck after stock failed2Deleting the order leaves the charge3Refund the full charge when stock is gone4Save the step before you continue5Look up the refund before you create one6Compensation is not one atomic purchase
Walk these steps in order. Each box is one thing to say before you move on.
  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

Interviewers listen for the boundary between the tempting shortcut and the rule you actually enforce.

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.