KatInterview

Design a Resilient Order Refund Workflow Engine

System DesignModel answer
MediumModel answerSystem Designsystem-designdistributed-systemsidempotency

Last updated

Design an idempotent, durable DAG workflow execution system for processing order refunds, handling step dependencies, crash recovery, manual reviews, and financial consistency.

Processing refunds in an e-commerce or food delivery platform involves multiple interdependent steps across different services: verifying balances, executing payment gateway refunds, updating account credits, clawing back partner payouts, re-evaluating driver/worker compensation, updating fraud risk models, and notifying users.

Some tasks can execute in parallel (such as sending customer emails and notifying internal operations teams), while others must run strictly in sequence (such as checking available balances before issuing a gateway refund). Furthermore, step failures may require human manual review or compensation steps.

Design a durable workflow engine that executes refund requests modeled as a Directed Acyclic Graph (DAG).

Requirements

Functional Requirements

  • DAG Execution & Dependency Tracking: Execute nodes according to explicit dependencies. Independent steps run concurrently; dependent steps wait for all parent steps to succeed.
  • Financial Balance & Idempotency: Support partial line-item refunds while ensuring the total refunded amount never exceeds the original charge. Prevent double-refunding across concurrent API requests.
  • Fault Tolerance & Crash Recovery: If worker nodes fail mid-task or orchestrators crash, the system must resume from the last known state without executing duplicate financial operations.
  • Human-in-the-Loop: Support pausing workflows when triggered by risk rules (e.g., high fraud scores) and resuming them asynchronously via external events or admin actions.
  • Observability: Expose status querying for ongoing, paused, and completed refund requests.

Non-Functional Requirements & System Scale

  • Throughput: 10 million completed orders per day with a 3% refund rate (~300,000 refund workflows per day, averaging ~3.5 workflows/second and peaking at ~30 workflows/second).
  • Correctness: Zero tolerance for double-payouts, lost state transitions, or invalid financial balances.
  • Latency: Fully automated paths should complete execution within 3 seconds (excluding external payment processor latencies).

System Design Deliverables

  1. Data Architecture & Schema: Define models for workflow definitions, runtime execution states, task dependencies, and financial idempotency tracking.
  2. System Architecture: Detail the interactions between the orchestrator service, database, task queues, and execution workers.
  3. Idempotency & Resilience: Explain how the system handles transient worker failures, network timeouts, duplicate messaging, and worker crash recovery.
  4. Concurrency Control: Explain how race conditions are prevented when multiple refund requests target the same order simultaneously.

Hints

Hint 1

Calculate scale upfront: 30 requests/second at peak means the core challenge is consistency and state resilience, not raw volume.

Hint 2

Consider how step state transitions and event notifications can be made transactional using patterns like the Transactional Outbox.

Hint 3

Distinguish between external API retry keys and logical execution step keys to avoid duplicate chargebacks during network timeouts.