KatInterview

Designing an Order-Routing Platform Over Async Exchange APIs

System DesignModel answer
HardModel answerSystem Designsystem-designdistributed-systemsidempotency

Last updated

Design a crypto order-routing service that fronts unreliable third-party exchange APIs with async fills, exactly-once effects, spiky load, and a durable ledger.

You are designing a trading platform that lets clients submit orders (market, limit, IOC/GTC), cancels, and queries, but never holds the matching engine itself: every order is forwarded to one or more external venues that expose a REST API which looks synchronous. In reality the venue's HTTP 200 only confirms receipt — fills, partial fills, and cancellations land later through polling, webhooks, or websocket streams, and a request can time out after the venue has already executed it.

Cover the following in your design discussion:

  1. Architecture — the services, datastore(s), event bus, and per-venue connectors; why the client gets a fast synchronous acknowledgement while execution proceeds asynchronously; how submission is decoupled from fill/cancel processing.
  2. Order state machine — the full set of states (including a distinct "outcome unknown" state), legal transitions, how state is persisted and recovered after a crash, and how transitions are made idempotent.
  3. Timeouts and partial failures — retries, idempotency keys / client-assigned order IDs, deduplication, circuit breakers, dead-letter handling, and periodic reconciliation with the venue; how to avoid double execution and get effectively-once external effects.
  4. Bursty traffic — rate limiting, queueing, backpressure, autoscaling, batching, and protection of downstream venue rate limits.
  5. Consistency and durability — guarantees for orders, fills, and user balances (e.g., double-entry ledger with reserve → settle/release), and how strong internal consistency coexists with eventual consistency against venue truth.
  6. Multi-venue routing (optional) — smart order router, parent/child order modeling, aggregation of child fills into the parent.
  7. Client APIs, SLOs, observability — the external API surface, latency/availability targets, and the metrics, tracing, logging, and alerting needed to run it.

State your assumptions about scale and be explicit about trade-offs and failure modes.

Hints

Hint 1

Separate the 'accept' path (fast, local, transactional) from the 'execute' path (slow, remote, retried) — the venue's success response is not the source of truth.

Hint 2

Model 'we sent a request and don't know what happened' as a first-class state resolved only by querying the venue, never by guessing or blindly resubmitting.

Hint 3

Aim for exactly-once *effects*, not exactly-once delivery: at-least-once messaging plus idempotent handlers keyed on client order IDs and venue trade IDs.

Hint 4

Dedupe fills using the venue's cumulative filled quantity, not per-message deltas, so replays are naturally no-ops.