KatInterview

Designing a High-Throughput Order Management & Execution System

System DesignModel answer
MediumModel answerSystem Designsystem-designdistributed-systemsconcurrency

Last updated

Design a scalable retail brokerage order management system handling limit/market orders, cancellations, concurrency-safe account balances, and edge cases like lost/duplicate fill reports.

Design an Order Management and Execution System (OMS) for a retail stock brokerage. The system must process market orders and limit orders while allowing users to cancel unexecuted order remainders.

Key Requirements

  1. Order Processing Lifecycle:

    • Market Orders: Must execute immediately at the best available market price.
    • Limit Orders: Must execute only at or better than the specified price (buy at/below limit price, sell at/above limit price). Unfilled portions remain pending until executed, expired, or cancelled.
    • Cancellations: Users can request cancellations for active orders. Only the unfilled portion of the order is cancelled. Fills completed prior to or concurrently with the cancellation remain valid.
    • Partial Fills: Orders can fill incrementally over time across multiple partial executions.
  2. Account Balance & Inventory Management:

    • Prevent double-spending of cash or over-selling of positions under concurrent requests (e.g., submitting multiple orders simultaneously).
    • Ensure accurate updates to user balances (cash and shares) across fills, cancels, and rejections.
  3. Fault Tolerance & Reliability:

    • Guarantee idempotency for order submissions, exchange execution reports, and cancellations.
    • Gracefully handle out-of-order execution reports, duplicate notifications, and exchange connectivity drops.
  4. Scalability & Performance:

    • Scale to handle millions of daily orders with extreme spikes during market open/close (e.g., peak loads of 10,000+ orders per second).
    • Provide sub-second order acknowledgment latencies to end users.

Deliverables

Provide a architectural blueprint covering:

  1. Core Architecture & Functional Flow: High-level components and communication flows between clients, your OMS, and external execution venues/exchanges.
  2. Order Lifecycle & State Machine: Explicit state transitions, edge case handling (e.g., cancel/fill race conditions), and recovery from failures.
  3. Balance Management & Concurrency Control: Data schemas for cash/inventory, locking strategies, and reconciliation mechanics.
  4. Scalability & Data Storage Strategy: Partitioning scheme, database choices, caching strategy, and queueing/rate-limiting mechanisms for high volatility bursts.

Hints

Hint 1

Think about how to distinguish between total cash/shares and available cash/shares to prevent double-spending without needing global locks across all users.

Hint 2

Consider using a state machine where state transitions are idempotent. How do you handle a scenario where an Execution Report (FILL) arrives AFTER a CANCELLED confirmation, or vice versa?

Hint 3

What partition key will ensure that order updates and account balance updates for a given user land on the same database shard/event processing node?