KatInterview

Crash-Safe Refund Pipeline as a Resumable Task Graph

System DesignModel answer
MediumModel answerSystem Designworkflow-engineidempotencyoutbox-pattern

Last updated

Design a fault-tolerant orchestration service that runs a refund as a dependency graph of steps, survives worker crashes, avoids double refunds, and handles failure and versioning.

Crash-Safe Refund Pipeline as a Resumable Task Graph

Your team is building the backend that processes customer refunds. A single refund is made of five steps: ValidateOrder, CheckInventory, ProcessPayment, UpdateDatabase and NotifyCustomer. The steps depend on one another and form a directed acyclic graph (DAG). You are asked to design the orchestration engine that runs these graphs reliably.

What the engine must guarantee

  1. Persistent progress. Execution state is stored durably. If a worker dies mid-run, the refund continues from the last step that finished instead of starting over.
  2. Dependency ordering. A step starts only after every one of its upstream steps has completed successfully.
  3. Safe retries. Each step can be run more than once without repeating its side effects. A customer must never be refunded twice because a step was retried after a partial failure.
  4. Terminal failure policy. When a step fails for good, the system either runs compensating actions (rollback) or parks the refund in a Manual Review state.
  5. Concrete data model. Define the structures for a WorkflowDefinition (nodes and edges) and a WorkflowExecution (current state and event log).

Questions to address in your answer

  • How do you get effectively-once results for side effects that are not naturally idempotent?
  • How do you detect that a long-running step has stalled, using heartbeats or timeouts?
  • What happens to refunds already in flight when a new version of the workflow definition is deployed?

What is expected

Walk through requirements and rough scale, the data model, the architecture, the key state transitions, the failure and compensation strategy, and the trade-offs you considered. Code or pseudo-code for the core structures and transitions is welcome.

Hints

Hint 1

Start by naming what matters most here: correctness over throughput. Estimate load (for example, about 1M refunds a day) to show that one relational database is enough, then spend your time on state transitions.

Hint 2

Make 'mark task done', 'decrement children's pending-parent counts' and 'record dispatch intent' a single database transaction (outbox pattern). Add a fencing check (status plus attempt number) so stale workers cannot overwrite newer results.

Hint 3

Derive the idempotency key from stable identifiers (refund and node), never from the attempt number. Then choose a technique per downstream: pass the key through, dedupe in the same transaction, or query-before-act for non-idempotent systems, and route ambiguous cases to manual review.

Hint 4

Pin each execution to the definition version it started with, and make published definitions immutable. Only new executions pick up the new version.