TL;DR: The window between "the processor charged" and "we recorded it" cannot be removed, so recovery must work across it. Before calling, record an intent with a key derived from the business action (the order and its purpose) and send that key to the processor as its idempotency key and reference. On recovery, the durable engine re-runs the activity; the activity first looks up the key at the processor and records the existing payment if there is one, and otherwise calls with the same key, which the processor deduplicates. Reconciliation catches anything that slips through.
How to approach it
Name the window and why retries alone are dangerous there, then design the three layers: identify the payment before making it, make the retry safe, and reconcile afterwards. Say what the durable engine does and what it cannot do.
A strong answer
The window. The worker sends the charge, the processor charges the card, and the worker dies before writing "charged" anywhere. On restart, the engine sees the activity as not completed and runs it again. Without protection, the customer pays twice.
Layer 1: the key exists before the call. The intent and its key are recorded in a step before the charge:
key = "order:ord_004471:charge:v1" from the business action, not random
intent = {key, amount: 1499, INR, status: pending} committed before calling
request = charge(amount, card_token, idempotency_key=key, metadata.reference=key)
A key derived from the order and purpose is the same however many times the activity runs, on whichever worker.
Layer 2: the retry asks before it acts.
PROCESSOR = {} # key -> charge: the processor's records (fake)
def processor_charge(key, amount):
if key in PROCESSOR: # the processor's own idempotency
return PROCESSOR[key]
PROCESSOR[key] = {"id": f"ch_{len(PROCESSOR) + 1}", "amount": amount}
return PROCESSOR[key]
def charge_activity(intent, crash_after_call=False):
existing = PROCESSOR.get(intent["key"]) # look up first
if existing:
return {"charge": existing["id"], "recovered": True}
charge = processor_charge(intent["key"], intent["amount"])
if crash_after_call:
raise SystemExit("worker died before saving")
return {"charge": charge["id"], "recovered": False}
intent = {"key": "order:ord_004471:charge:v1", "amount": 1499}
try:
charge_activity(intent, crash_after_call=True)
except SystemExit:
pass
result = charge_activity(intent)
print(result, len(PROCESSOR)) # => {'charge': 'ch_1', 'recovered': True} 1
The lookup finds the charge the dead worker made, and the activity returns it as its result, which the engine records. Even without the lookup, the processor's idempotency key would return the same charge rather than creating another.
Layer 3: reconcile. A scheduled job compares intents with the processor's records: charges with no completed intent (recorded now), intents marked done with no charge (investigated), intents stuck pending past a deadline (looked up, then escalated).
What the durable engine does and does not do. It guarantees the workflow resumes at this activity with the right state, and that the activity is retried. It does not know whether the side effect happened; only the key and the lookup can answer that.
What interviewers probe next
"The processor keeps idempotency keys for only 24 hours." Then the lookup by reference carries the guarantee after that, and runs that may resume later (after an approval wait) must look up before acting.
"Why not record the intent and call the processor in one transaction?" They are different systems; no transaction spans both. The outbox pattern gets close: commit the intent, then a dispatcher performs it and resolves unknowns by lookup.
"What does the customer see during recovery?" A pending state, then confirmation once the activity's result is recorded, typically within seconds of the restart.
What if?
What if · interview follow-ups
Hold your answer as the constraints change
Charges carry a key derived from the order, the activity looks up before charging, and reconciliation runs hourly.
Start here
What if the customer legitimately buys the same item again tomorrow?
What does the key identify?
A new order gets a new order id and therefore a new key. The key identifies one business intent, not an item or an amount, so a real second purchase is never deduplicated away.
Carry it forwardKeys identify the intent, not the content.
Weak answerKeys built from customer and amount, which block a genuine repeat.
Constraint changes
What if the lookup itself times out?
Is the outcome known now?
Still unknown, so do not charge. Retry the lookup with backoff; if it stays unavailable past a deadline, mark the intent unknown and escalate rather than charge blind.
Carry it forwardUnknown stays unknown until a lookup answers it.
Weak answerCharging because the lookup failed.
Constraint changes
What if the amount must change after a crash (a coupon applied)?
Is it the same intent?
No. Changing the amount is a new intent with a new key version (v2), after confirming v1 did not charge or refunding it. Reusing the key with a different amount should be rejected by the processor as a conflict.
Carry it forwardDifferent arguments mean a different intent.
Weak answerReusing the key with the new amount.
Now defend it
One-line design?
Record an intent with a business-derived key before charging, send the key as idempotency key and reference, look it up before any retry, and reconcile intents against the processor.
How it's scored
Weak: "Retry the payment on failure" or "use a database transaction".
Hire: Idempotency keys on the payment call and a durable engine to resume the workflow.
Strong Hire: Names the unavoidable window, derives keys from the business action and commits the intent first, looks up before acting on retry, relies on the processor's idempotency as a second layer, reconciles intents against the processor, and handles key expiry, lookup failures and changed amounts.
Common mistakes
- Random keys generated per attempt.
- Assuming the durable engine prevents duplicate side effects on its own.
- Treating a timeout or failed lookup as "not charged".
- No reconciliation, so rare mismatches are found by customers.