hard general · part of Practice Questions · Senior SWE Roadmap

Requirements to clarify

  • Functional: accept buy/sell orders, match them against each other, execute trades, broadcast market data.
  • Non-functional: extremely low, predictable latency (microseconds matter), strict ordering/fairness (orders must be processed in the order received), zero tolerance for lost or duplicated orders.

Core components

  • Matching engine: maintains an order book (buy orders and sell orders, typically as sorted structures by price) and matches compatible orders (a buy at or above the lowest sell) — usually implemented as a single-threaded, in-memory process per instrument to guarantee strict ordering without lock contention.
  • Order book data structure: price levels held in a structure allowing fast best-price lookup and insertion (e.g., balanced trees or arrays of price levels with queues of orders at each level).
  • Sequencing: every incoming order is assigned a strict sequence number before matching — this total order is what makes the system fair and deterministic (and replayable for audit/recovery).
  • Market data distribution: trade executions and order book changes are broadcast to subscribers (traders, tickers) — a separate, high-fanout read path from the matching engine’s write path.

Key tradeoffs

  • Single-threaded-per-instrument matching sacrifices some parallelism but is what makes strict, fair ordering achievable without complex distributed locking — a good example of intentionally not scaling horizontally where correctness demands it.

Approach / Notes