KatInterview

Design a Multi-Step Order Processing Pipeline

System DesignModel answer
MediumModel answerSystem Designdistributed-systemsevent-drivenidempotency

Last updated

Design the backend system that moves a customer order through reservation, payment, fulfillment, and shipping while handling failures and partial states gracefully.

Background

You are designing the backend for an e-commerce platform. When a customer places an order, several independent services need to coordinate: inventory must be reserved, payment must be collected, a warehouse must pick and pack the items, and a carrier must be notified. Each of these is owned by a separate team and runs on a separate data store.

Task

Walk through the complete lifecycle of an order — from the moment it is placed to the moment it is delivered or cancelled — covering:

  • State machine — what states an order moves through, what triggers each transition, and what invariants must hold at each step
  • Service decomposition — how you split responsibility across services and what each service owns
  • Data flow — how services communicate (synchronous calls, async events, or both) and where the authoritative record lives
  • Failure recovery — what happens when inventory is reserved but payment fails, or payment is captured but the warehouse never acknowledges the job
  • Duplicate and out-of-order messages — how you ensure that replaying an event does not double-charge a customer or double-reserve stock
  • Customer-facing status — how a customer sees a single coherent order status even when internal state is scattered across services

Constraints and Assumptions

  • Inventory and payment run on separate databases; no distributed transaction spans both.
  • The message bus may deliver events more than once, out of order, or with arbitrary delay.
  • The system must support at least: reservation expiry, payment failure, partial fulfilment (only some items are in stock), cancellation before shipment, and post-shipment return initiation.
  • Peak load is on the order of tens of thousands of new orders per minute during sale events.

What Is Expected

Describe your architecture in prose and/or diagrams (ASCII is fine). Justify the key design choices and call out the trade-offs you are consciously accepting. You do not need to write production code, but pseudocode or API sketches are welcome where they sharpen the explanation.

Cover the clarifying questions you would ask before committing to a design, then proceed with reasonable assumptions.

Hints

Hint 1

Start with the state machine before talking about services — every architectural decision should be traceable back to a valid state transition. Draw it out: PENDING → RESERVED → PAYMENT_CAPTURED → IN_FULFILLMENT → SHIPPED → DELIVERED, plus the cancellation and failure branches.

Hint 2

Because inventory and payment can't share a transaction, you need the Saga pattern. Think through whether you want choreography (services react to each other's events) or orchestration (a central workflow service drives the steps). Both work — but pick one and defend it, don't try to mix them without a reason.

Hint 3

Idempotency is the main reliability tool here. Every state-changing operation should accept a stable idempotency key (typically the order ID plus a step name) so that retrying a failed step produces the same outcome rather than a double charge or double reservation.