KatInterview

Workflow Engine with DAG Execution and Timeout-Based Refunds

System DesignModel answer
MediumModel answerSystem Designdistributed-systemsworkflow-orchestrationstate-management

Last updated

Build a workflow engine that executes order tasks as a directed acyclic graph, finalizes successful orders, and triggers refunds when timeouts occur.

You are building a workflow engine for an e-commerce platform. Orders flow through multiple stages—inventory check, payment capture, fulfillment preparation, and shipping—organized as a directed acyclic graph where each task may depend on one or more prior tasks completing successfully.

Your system must handle:

  • Submission: Accept a new order with a defined workflow graph and a global timeout
  • Execution: Run tasks only after their dependencies finish; each task may call an external service
  • Terminal states: If all tasks complete before the timeout, mark the order finalized. If the timeout elapses first, trigger a refund workflow instead
  • Status queries: Return the current state of an order and which tasks have completed
  • Recovery: After a crash, resume in-progress orders without duplicating side effects

Requirements

  1. DAG validation: Reject cycles and undefined dependencies at submission time
  2. Prerequisite scheduling: A task runs only when every upstream task has succeeded
  3. Atomic terminal decision: Completion and timeout are mutually exclusive; once one path is taken, the other cannot occur
  4. Idempotency: Restarting a task or refund operation that already ran must not double-charge or double-refund
  5. Durable state: Persist enough information to recover after a process restart
  6. Correct status: Queries must reflect the true state even during concurrent task execution

What to Cover

Describe your high-level architecture, data model, and coordination mechanism. Explain how you prevent race conditions between completion and timeout, how you ensure idempotency, and how recovery works after a failure. Provide a sketch of the core APIs and outline common failure scenarios with your mitigation strategy.

Hints

Hint 1

Think about where the timeout clock lives and who is responsible for checking it—centralized or per-order?

Hint 2

Use versioning or unique execution identifiers to make external calls idempotent, so retries are safe.

Hint 3

Consider separating task state persistence from the actual decision to finalize or refund, using a compare-and-swap or lock to enforce atomicity.