Skip to content

Interview question · Long-Running & Durable Agents

A worker crashes after the payment call succeeds and before the result is saved. Design recovery.

No design closes that window; good designs make it harmless. The payment must be identifiable before it is made, the retry must ask before it acts, and the processor's own idempotency must do the rest.

hardEssentialNewUpdated Nov 2026

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.

  1. Start here

    What if the customer legitimately buys the same item again tomorrow?

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.
You just read a free answer. Every topic opens its first 5 answers to anyone. Sign in and the count per topic goes to 6, and the site remembers where you stopped in each one.Google sign-in · free · no payment details
HOW DID IT GO?
0
READING SIGNED OUT

Signing in raises your free answers from 5 to 6 per topic, adds an applied lesson in every topic and easy practice tests, and the site starts remembering you: mastery per topic, bookmarks, and a next-focus recommendation. Free, no card.

Sign in free
UP NEXT ON YOUR JOURNEY
DISCUSSION · 0

Nobody has commented. Post a correction or another way to answer.