Retrying operations that aren't idempotent
Spot retry loops the agent wrapped around payments, POSTs, emails or queue publishes. Explain how they cause duplicates when the first attempt actually succeeded but its response was lost, and how an idempotency key or deduplication makes the retry safe.
Checkout now 'survives' payment timeouts
You asked the agent to stop checkout failing when the payment provider times out. The diff wraps `pay.charge()` in a three-attempt retry loop with backoff. A new test makes the first call time out and the second succeed. Everything is green, and the summary says checkout is now "resilient to transient errors". Do you approve?
A timeout doesn't mean nothing happened
A timeout means *no answer arrived*, not *nothing happened*. The provider may have charged the card, and only the reply got lost on the way back. A retry then charges the card a second time. The agent works toward what it can see: the error stops. Its test simulates a world where a timed-out call did nothing, so the proxy signal goes green. **Later cost:** double charges, duplicate emails or orders. They appear only under real network trouble.
Is this retry safe? Three checks
**Tell:** a retry (a loop, `@retry`, a `retries:` option, a retrying client) around a call that changes another system: a payment, a POST, an email or SMS send, a queue publish, or an insert without a unique key.
1. Does doing it twice change the outcome? Set-to-value and delete-by-id: usually no. Create, charge, send, add: yes.
2. Is the failure ambiguous (timeout, connection reset, 5xx)? Then assume it may have succeeded.
3. If the answer to both is yes, require an idempotency key or dedup.
BAD: a retry around a charge
Read the loop as the provider sees it. Attempt 1 times out after the charge went through. Attempt 2 is the same request again, with nothing that ties it to attempt 1. To the provider it is a brand-new payment. The final `raise` looks careful, but it is unsafe too. Three timeouts can mean three charges, and the customer is then told the payment failed.
// Python
--- a/billing/checkout.py
+++ b/billing/checkout.py
- return pay.charge(order.total, card)
+ for attempt in range(3):
+ try:
+ return pay.charge(order.total, card)
+ except Timeout:
+ time.sleep(2 ** attempt)
+ raise PaymentError("charge timed out")
BETTER: one key per payment, made once
**BETTER:** build one key per logical payment *before* the loop and pass it on every attempt. The provider stores key → result and returns the first result instead of charging again. A subtle tell: `uuid4()` created *inside* the loop, which makes each attempt a new payment. **Later cost:** the same double charge the key was meant to prevent. If the provider has no keys, look up the charge by order reference before retrying.
// Python
key = f"order-{order.id}-charge"
for attempt in range(3):
try:
return pay.charge(order.total, card, idempotency_key=key)
except Timeout:
time.sleep(2 ** attempt)
raise PaymentError("charge status unknown")
Twice must equal once
A retry is safe only if doing the thing twice has the same result as doing it once. For charges, POSTs, emails and publishes, a lost reply can hide a success. Insist on one stable idempotency key per logical operation, created before the loop. Otherwise, dedup on the receiving side using a stable id and a unique constraint.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.