coding challenges · advanced
Stock Exchange — Critical Trading Path (HFT)
CoinDCX stock-exchange critical trading path (Client Gateway → Order Manager → Sequencer → Matching Engine; 100 symbols, ~215k QPS, ms P99, 99.99% uptime, near-zero RPO). First convert NFRs to a budget (~µs per hop, durable-before-ack, seconds failover). Latency: prefer a single-server hot path over microservices — co-locate stages in one process, pass messages via lock-free mmap/shared-memory ring buffers (no kernel, no copies), pin threads to dedicated cores, and run single-threaded busy-spin loops (no locks, no context switches, no malloc/syscalls). Order book: per symbol a sorted std::map of price levels (best bid/ask = begin(), O(1) top), a FIFO list per level for price-time priority, and an unordered_map<OrderId, list-iterator> for O(1) cancel; matching fills against the opposite top level. HA/RPO: the Sequencer assigns a global sequence number and writes a replicated append-only log durable before acking (≈0 RPO); because the engine is a deterministic state machine, recovery is log replay (event sourcing) and hot-warm replicas / Raft enable seconds-level failover for 99.99%; snapshot to bound RTO. Keep the hot path lean — O(1) in-memory risk checks on-path, market-data fan-out, persistence and analytics async on separate cores fed by ring buffers, with backpressure at the gateway. Interviewers grade quantified trade-off reasoning and failure-mode awareness, not a single right answer.
Exchange critical path: single-server hot path (mmap ring buffers + CPU pinning + single-threaded busy-spin) for µs/ms P99; order book = sorted map of price levels + per-level FIFO + id→iterator index → O(1) add/cancel/match; Sequencer writes a replicated append-only log durable-before-ack (≈0 RPO); the deterministic engine recovers by replay (event sourcing) with hot-warm/Raft failover (99.99%); risk is O(1) on-path, everything else async with edge backpressure.
The code
// Critical trading path of a stock exchange.// Client Gateway -> Order Manager -> Sequencer -> Matching Engine// Scale: 100 symbols, ~215k peak QPS, 1B orders/day.// Targets: ms round-trip, low P99, 99.99% uptime, near-zero RPO.
// Order book per symbol — O(1) add/cancel/match at top of book.struct PriceLevel { Price price; std::list<Order> fifo; // time priority within a price};struct OrderBook { std::map<Price, PriceLevel, std::greater<>> bids; // best bid = begin() std::map<Price, PriceLevel> asks; // best ask = begin() std::unordered_map<OrderId, std::list<Order>::iterator> index; // O(1) cancel};
// Determinism: every input is sequenced, then the engine is a pure// state machine. Replay the log => identical state (event sourcing).What this lesson walks through
- 01The path — and turn the NFRs into a budget
- 02Latency: one process, not microservices
- 03The order book — O(1) add / cancel / match
- 04HA + ~0 RPO: sequence → replicate → replay
- 05Keep the path lean — fan everything else out async
- 06The whole design in one frame
Gateway → Order Manager → Sequencer → Matching Engine. First convert the requirements into numbers: 215k QPS ≈ one order every ~4.6µs, ms P99 ⇒ a few hundred µs per hop, ~0 RPO ⇒ durable before ack, 99.99% ⇒ seconds-level failover.
See it animated — step by step, at your own pace
Unlock the full interactive walkthrough of Stock Exchange — Critical Trading Path (HFT) and 100+ animated C++ interview lessons.