12/17
Part 4 of 7 · ConsequencesLearn and do

Make the risky tool safe to call twice

The refund call times out. Did the money move? The harness can't tell, and retrying might pay twice. The fix is an idempotency key: a name for the operation, so a repeat with the same key does nothing new. The code below calls refund_once, the refund you write in this step's task.

# idempotency key -> amount paid
paid = {}
print(refund_once(paid, "A17", 189.0, "A17-return-1"))
# the retry after a timeout
print(refund_once(paid, "A17", 189.0, "A17-return-1"))
print("payments:", len(paid))
Predict

The refund call times out after the payment went through, and the agent retries with the same key. How many payments are there?

Pick one to see what it prints.
Pick an answer above to see the rest