KatInterview

Orchestrating a Durable Multi-Step Refund Process

System DesignModel answer
MediumModel answerSystem Designdistributed-systemsidempotencystate-machine

Last updated

Design and implement a crash-resistant orchestrator that coordinates a refund across three HTTP services with idempotency, persistence, and retry logic.

You're building a refund orchestration system that coordinates three separate HTTP services. The orchestrator must ensure correctness even when services fail, time out, or the orchestrator itself crashes mid-execution.

Architecture

Three independent services communicate over HTTP:

Order Management (port 8001)

  • GET /orders/{order_id} returns {"order_id": string, "total_amount_cents": int, "refunded_amount_cents": int, "status": string}
  • POST /orders/{order_id}/holds with {"hold_id": string, "amount_cents": int} reserves a refund amount, returning {"hold_id": string, "status": "HELD"} or 409 if insufficient funds
  • POST /orders/{order_id}/commit-hold with {"hold_id": string} finalizes the reservation

Payment Processor (port 8002)

  • POST /payments/refund with {"idempotency_key": string, "order_id": string, "amount_cents": int} processes the actual money movement
  • Uses the idempotency key to deduplicate: identical keys return the same result without re-executing
  • May return 504 timeouts or drop connections

Refund Orchestrator (port 8003) — your implementation

  • POST /refunds with {"refund_id": string, "order_id": string, "amount_cents": int} starts a workflow, returning 202 with status
  • GET /refunds/{refund_id} returns workflow state and progress through steps
  • Must execute: validation → hold funds → process payment → commit hold

All amounts are non-negative integers representing cents.

Requirements

Correctness: Concurrent refund requests must not allow refunded_amount_cents to exceed total_amount_cents. Duplicate refund_id values should return existing workflow state.

Durability: If the orchestrator crashes during execution and restarts, it must resume incomplete workflows from where they stopped, not restart from the beginning.

Retry resilience: When the payment service times out or returns ambiguous errors, the orchestrator must retry using the same idempotency key until receiving a definitive success or failure.

Example Flow

POST /refunds
{"refund_id": "rfnd_456", "order_id": "order_789", "amount_cents": 2500}
→ 202 {"refund_id": "rfnd_456", "status": "PROCESSING"}

// Internally executes:
// 1. GET order_789 to check available balance
// 2. POST hold with hold_id="hold_rfnd_456"
// 3. POST payment with idempotency_key="rfnd_456:payment"
// 4. POST commit for hold_rfnd_456

GET /refunds/rfnd_456
→ 200 {
  "refund_id": "rfnd_456",
  "status": "SUCCESS",
  "steps": {
    "validate": "COMPLETED",
    "hold": "COMPLETED",
    "payment": "COMPLETED",
    "commit": "COMPLETED"
  }
}

Design the orchestrator's architecture, state management, and error handling. Explain how your design ensures correctness under concurrency, crashes, and transient failures.

Hints

Hint 1

Think about what state must be persisted before making each HTTP call, and what happens if the process dies right after sending a request but before recording the response.

Hint 2

The order service's hold operation is your concurrency control mechanism—it's the only place where the 'check and reserve' happens atomically.

Hint 3

Each external call needs a stable, deterministic identifier derived from refund_id so that retries (including after crashes) use the exact same key and produce idempotent effects.