Design a Stock Exchange — Visual Design Journey
Watch a single order gateway evolve, bottleneck by bottleneck, into a sequenced, durable, fenced-failover matching engine that fills orders by strict price-time priority.
15 scenes · 39 narrated steps · interactive, self-paced
Design a Stock Exchange
Design a Stock Exchange
A long-form engineering design session — watch order matching, sequencing and durability evolve, bottleneck by bottleneck.
Maximize to view synchronized narration and navigate between explanation steps.
What You Should Understand Now
- Price-time priority is deterministic: when two resting orders share a price (S1 and S2 both at $101.00), the one with the earlier sequence number is always filled first — fairness comes from sequencing, not from which request "arrives" fastest over the network.
- An accepted order is never the same thing as an executed trade — a limit buy below the best ask simply rests on the book, and a limit price is a ceiling on what you will pay, never a promise of what you will pay, as the multi-level sweep in Test B demonstrates.
- IOC and FOK orders answer the same question — "what if the book can't fill me completely?" — in opposite ways: IOC takes the partial fill and cancels the rest, while FOK requires the entire order to be fillable right now or it executes nothing at all.
- A cancel request racing a matching order is resolved by whichever the sequencer serialized first — a completed fill can never be retroactively undone by a cancel that is sequenced after it.
- A single-writer matching engine per instrument is what makes price-time priority provably correct without locks, but durability requires a journal committed before acknowledgment, and failover requires epoch fencing so two engines never believe they own the same book at once.
- Market data is a broadcast of the truth, not the truth itself: it has its own sequence numbers, a subscriber detects gaps and resynchronizes from a snapshot, and none of this is allowed to sit on the matching engine's hot path.
Concepts in This Visual
Price-Time PriorityOrder Book (Bid / Ask / Spread)Limit, Market, IOC & FOK OrdersCancel/Fill Race SerializationPre-Trade Risk GateDeterministic SequencerSingle-Writer Matching EngineDurable Journal + Snapshot ReplayEpoch Fencing & Standby PromotionMarket Data Snapshot + DeltaPartitioning by InstrumentTrading HaltsPost-Trade Clearing Handoff
Go Deeper
Complete Design
The full written walkthrough — problem, market fundamentals, requirements, capacity estimation, data model, APIs, the initial single-process exchange, and its evolution into the final production architecture.
Architecture Deep Dive
Goes layer by layer: the matching engine execution model, order-book data structures, the price-time priority algorithm, sequencing, the journal, replication and leader fencing, and market-data architecture.
Trade-offs & Decisions
Why single-writer vs multithreaded matching, price-time vs pro-rata, synchronous vs async persistence, and active-passive vs active-active replication are each genuine trade-offs rather than obvious choices.
Failure & Scale Scenarios
What actually breaks in production — engine crashes, cancel/fill races, split brain, lost execution reports, market-data gaps, hot-symbol overload, and exchange-wide volatility spikes.
Interview Perspective
How to open and structure the interview, the core questions with model reasoning, Staff-level follow-ups, whiteboard exercises, and a bank of likely questions grouped by topic.