Vibe Engines
YouTube
System Design

Design a Stock Exchange

Step 1 / 9

Learn system design by building a stock exchange / matching engine step by step.

The numbers to beatpricethen timeFIFOper leveldeterministicfills

The whole design, in writing

Learn system design by building a stock exchange / matching engine step by step. An interactive guide covering the order book and price-time priority, the matching engine, pre-trade risk checks, deterministic sequencing for low latency, market data feeds, journaling and replay, and clearing.

Every step of the build above, written out: the problem each piece solves, the option that was taken and the ones that were not, the numbers, and how it fails in production.

The big idea

What is a stock exchange?

Buyers and sellers submit orders; the exchange matches them into trades — fairly, deterministically, and in microseconds. Money is on the line, so it must never lose an order, never mismatch, and treat every participant equally. Correctness and latency, both absolute.

Traderplace order
New in this step: Trader.

At the center is a matching engine running a single, ordered stream of events against an in-memory order book. Wrap it in admission, risk checks, deterministic sequencing, market-data broadcast, and a durable journal — keeping the core path tiny and fast.

What the new pieces do

Traderclient
Sends buy/sell orders and expects acknowledgement and fills in microseconds. Latency and fairness are the product — a slow exchange loses everyone.

Step 1 · The core

Match orders into trades

A trader sends “buy 100 at $50”. The exchange must instantly find a matching sell and execute, or rest the order until one appears. What structure makes that match fast and correct?

Order GatewayMatching EngineOrder Book
New in this step: Order Gateway, Matching Engine, Order Book. · swipe to pan the diagram

"Buy 100 at $50" must instantly find a match or wait for one. What structure makes that fast and correct?

  1. Scanning every open order per incoming order is O(N) on the hottest path in the system — far too slow for microsecond matching. You need a structure sorted for instant best-price lookup.

  2. A round-trip to disk-backed storage per order is orders of magnitude too slow, and SQL can’t express price-time matching cheaply. The book must live in memory, purpose-built.

  3. Best bid and best ask sit at the top; matching crosses the spread — highest bid against lowest ask — until they no longer overlap. A market order takes best price; a limit order rests if it can’t match.

An Order Gateway admits the order to a Matching Engine that runs against an Order Book — resting bids and asks per symbol. A market order takes the best price; a limit order matches if possible, otherwise rests in the book.

What the new pieces do

Order Gatewaybackend
Authenticates the trader, validates the order, and admits it into the system. The boundary between the messy outside world and the pristine matching core.
Matching Engineservice
The heart: matches incoming orders against resting ones by price, then time. Kept single-threaded and in-memory so it’s deterministic and blazingly fast.
Order Bookstore
The live in-memory book of resting buy (bid) and sell (ask) orders per symbol, sorted for instant best-price lookup and matching.

Step 2 · The rules of matching

Price-time priority

When many orders want the same price, who trades first? Get this wrong and the market is unfair — and traders will (rightly) leave an exchange that doesn’t treat equal orders equally.

TraderOrder GatewayMatching EngineOrder Book
The system as it stands at this step. · swipe to pan the diagram

Many orders want the same price. Who trades first, fairly?

  1. Favoring size lets big players jump the queue and punishes small orders — manifestly unfair, and traders will leave. Fairness can’t depend on order size.

  2. Best price wins; among equal prices the earliest order fills first. Organizing the book as price levels, each a FIFO queue, builds the rule into the data structure — deterministic and auditable.

  3. Randomness makes fills unpredictable and unauditable — a trader can’t reason about where they stand in line. Matching must be deterministic, so equal-priced orders need a stable order: time.

Match by price-time priority: best price wins, and among equal prices the earliest order fills first. The book is organized into price levels, each a FIFO queue, so the rule is built into the data structure.

  • pricethen time
  • FIFOper level
  • deterministicfills

Back of the envelope

sort by price, then time (FIFO)
the matching rule is the data structure
price levels = FIFO queues
best bid / ask sit at the top
deterministic, auditable fills
every trader knows their place in line

Step 3 · Guard the book

Pre-trade risk checks

A trader could submit an order they can’t afford, or one that blows past position limits — a “fat finger” or a rogue algo. Letting that reach the book risks trades that can’t settle.

Order Gatewayvalidate + admitMatching Engineprice-timeRisk Checklimits · funds
New in this step: Risk Check.

A trader could submit an order they can’t afford or that blows past limits (fat-finger, rogue algo). Where do you stop it?

  1. Unwinding executed trades is disruptive, may be impossible once others traded against them, and corrupts the market. Bad orders must be stopped before they execute, not after.

  2. Putting credit/position checks in the single-threaded hot loop adds latency and non-determinism to the core. Keep the matcher tiny and fast; validate before it.

  3. Verify buying power, position/credit limits and price/size sanity at the gateway, rejecting violations immediately — so only valid orders ever touch the engine, and the hot loop stays fast and deterministic.

Insert a Risk Check before matching: verify buying power, position and credit limits, and sanity-check price/size. Reject violations immediately, so only valid orders ever touch the matching engine.

What the new pieces do

Risk Checkguard
Pre-trade gate: does the trader have the buying power and stay within position/credit limits? Rejects bad orders before they reach the book.

Step 4 · Make it deterministic

A single sequencer

Orders arrive concurrently from everywhere. For fairness, auditability, and the ability to run hot replicas, every component must agree on the exact order events were processed — and racing threads can’t guarantee that.

Order Gatewayvalidate + admitSequencertotal orderMatching Enginesingle-threaded
New in this step: Sequencer.

Orders arrive concurrently from everywhere. How do you make matching fair, auditable, and replica-safe?

  1. Racing threads can’t guarantee the order events were processed, breaking fairness and making the engine non-reproducible — so replicas diverge. The core must process one agreed order.

  2. A sequencer stamps every admitted order with a global sequence number, then a single thread processes that one stream — same input order, same output, on primary and every replica. Astonishingly fast in-memory, and perfectly reproducible.

  3. Wall clocks differ across machines and tie at microsecond scale, so timestamps don’t give a single agreed order. You need one authoritative sequencer assigning a total order, not independent clocks.

Funnel all admitted orders through one Sequencer that assigns a global sequence number, then feed that single stream to a single-threaded matching engine. Same input order, same output, every time — on the primary and every replica.

  • 1sequencer
  • single-threadmatcher
  • µsper match

What the new pieces do

Sequencerservice
Stamps every order with a single, global sequence number so all replicas process events in the exact same order — the basis of determinism and fairness.

Back of the envelope

1 sequencer ⇒ total order
every replica sees the same input stream
single-threaded, in-memory
no shared-state races, µs per match
same input ⇒ same output
perfectly reproducible for failover

Step 5 · Tell the world

Market data feed

Every participant needs to see book updates and trades to make decisions — and they must all see them at the same time. Any latency advantage is an unfair edge worth millions.

SequencerMatching EngineOrder BookMarket DataMarket Feed
New in this step: Market Data, Market Feed. · swipe to pan the diagram

Every participant needs book updates and trades — and a latency edge is worth millions. How do you distribute fairly?

  1. Polling gives whoever polls fastest (or is closest) a systematic edge, and hammers the engine. Market data must be pushed equally, not pulled at each participant’s own rate.

  2. Selling a latency advantage is exactly the unfairness a fair exchange must prevent — it lets some systematically see the market first. Delivery should be equal-latency for all.

  3. The engine emits each change to a market feed (often multicast) so the same picture reaches everyone simultaneously. Distributing data fairly matters as much as matching fairly.

The engine emits every change to Market Data, broadcast over a Market Feed to all participants with equal-latency delivery (often multicast). The same picture reaches everyone simultaneously.

What the new pieces do

Market Datastore
The stream of book changes and executed trades. Every participant needs the same view of the market at the same time.
Market Feedbus
Publishes market data to all participants simultaneously, with equal-latency delivery so no one gets an unfair early peek.

Step 6 · Never lose a trade

Journal & replay

The order book lives in memory for speed — but memory is volatile. A crash without a record would lose orders and trades, which for an exchange is catastrophic and possibly illegal.

Matching Enginesingle-threadedOrder Bookprice levelsJournalappend-only log
New in this step: Journal.

The order book lives in volatile memory for speed. How do you survive a crash without losing trades?

  1. A snapshot every few seconds loses every order and trade since the last one — catastrophic and possibly illegal for an exchange. Every event must be durable before it takes effect.

  2. Log first, apply second: on failure a replica replays the journal to reconstruct the exact book, and because the engine is deterministic the replay reproduces an identical result. The sequential write is cheap.

  3. Synchronously copying full book state on every change is slow and still loses the in-flight event on a crash. Journaling the ordered event stream is cheaper and, with determinism, rebuilds state perfectly anywhere.

Journal every event to an append-only log before applying it. On failure, a replica replays the journal to reconstruct the exact book state and takes over. Because the engine is deterministic, replay reproduces the identical result.

What the new pieces do

Journalstore
A durable, ordered log of every event before it’s applied. Replaying it rebuilds the exact book state — the safety net behind an in-memory engine.

Back of the envelope

append before apply
the sequential journal write is cheap
deterministic ⇒ replay = rebuild
the log is a perfect recipe
replica replays ⇒ takes over
in-memory speed without losing data

Step 7 · After the match

Clearing, settlement & scale

A match is a promise, not finished money. Ownership and cash must actually change hands, and one engine can’t hold the order books for every symbol on earth.

Matching Enginesingle-threadedClearing & Settlementpost-trade
New in this step: Clearing & Settlement.

Hand executed trades to Clearing & Settlement (T+1/T+2) off the hot path, so post-trade work never slows matching. Scale by partitioning symbols across matching engines — each symbol’s book is independent, so AAPL and TSLA run on different engines in parallel.

What the new pieces do

Clearing & Settlementservice
After a match, transfers ownership and money between parties (T+1/T+2). Decoupled from matching so settlement never slows the hot path.

You did it

You just designed a stock exchange.

TraderOrder GatewaySequencerMatching EngineOrder BookRisk CheckMarket DataJournalClearing & SettlementMarket Feed
The finished design, end to end. · swipe to pan the diagram

Everything you assembled, in order

  • A matching engine crossing an in-memory order book turns orders into trades.
  • Price-time priority — best price, then FIFO — makes matching fair by construction.
  • Pre-trade risk checks at the edge keep invalid orders out of the core.
  • A single sequencer + single-threaded engine give deterministic, fair, fast matching.
  • A market-data feed broadcasts the same view to all participants with equal latency.
  • An append-only journal plus deterministic replay make the in-memory engine durable.
  • Clearing runs off the hot path; partitioning by symbol scales the exchange out.

Where an interviewer pokes next

Getting the boxes right is the easy half. These are the questions that separate a candidate who drew the diagram from one who has run the thing. Answer each one out loud before you open it.

  1. Why single-threaded — isn’t parallel matching faster?

    For a single symbol’s book, no: one thread on modern hardware matches millions of orders/sec entirely in L1/L2 cache, with zero locking, context switches, or cross-core coordination — which multi-threading pays dearly for. Parallelism comes from partitioning by symbol (different books on different engines), not from threading one book. Determinism (easy failover) is the bonus.

  2. How does failover work without losing the in-flight order?

    The sequenced stream and every event are journaled before being applied, and replicas consume the same stream. On primary failure, a replica that has replayed up to sequence N takes over from N+1 — the book is bit-identical because the engine is deterministic. The journal is the source of truth; the in-memory book is a derived, rebuildable view.

  3. What matching rules exist beyond price-time (e.g. pro-rata)?

    Price-time (FIFO) is most common, rewarding the earliest order at a price. Some markets (often futures/options) use pro-rata — allocating a fill across resting orders proportional to size — or hybrids. The choice shifts incentives (FIFO rewards speed, pro-rata rewards posting size) but must always be deterministic and encoded in the book.

  4. How do you keep clearing/settlement from slowing matching?

    Decouple it: the engine emits executed trades to clearing asynchronously and moves on. Settlement (T+1/T+2 transfer of ownership and cash via a clearing house that nets exposures) runs entirely off the hot path. A match is a binding promise recorded in the journal; turning it into settled money is a separate, slower pipeline.

  5. How do you handle a flash-crash or runaway algo?

    Layered safeguards around the matching rule: pre-trade risk limits at the gateway, exchange circuit breakers that halt a symbol when price moves beyond a band in a short window, price collars that reject orders far from the last trade, and per-participant kill switches. The matcher stays simple and fast; market-integrity controls live around it.

Check yourself — the answers, and why

Eight steps in, these are the calls you should be able to make cold. Pick one, then read why.

  1. Matching is fast because the order book is…

    • A database table
    • A sorted in-memory structure (best bid/ask on top)
    • A message queue

    Matching = crossing the spread; the book is sorted so the best prices are instantly available.

  2. Price-time priority means…

    • Largest order first
    • Best price, then earliest order (FIFO) at that price
    • Random selection

    Price levels as FIFO queues bake fairness into the data structure — deterministic and auditable.

  3. A single sequencer + single-threaded engine give…

    • More parallelism
    • Deterministic, reproducible matching (and easy failover)
    • Encrypted orders

    One ordered input stream, no races — same input always yields the same output, on every replica.

  4. Pre-trade risk checks live on the admission path so that…

    • Matching is fairer
    • The hot matching loop stays fast and deterministic
    • Orders settle faster

    Validate at the edge; keep credit/position checks out of the single-threaded core.

  5. The in-memory engine survives crashes via…

    • Snapshots every minute
    • An append-only journal written before apply, replayed on failover
    • A bigger cache

    Log first, apply second; determinism makes replay rebuild the exact book anywhere.

How you’d open this design in an interview

Before any boxes: agree what it must do, pin the qualities that shape everything, then build — naming each trade-off as you make it. The walkthrough above is that exact order.

What it must do

Agree on these before drawing a single box.

  • Match: take buy/sell orders and cross them into trades against an in-memory order book.
  • Order fairly: price-time priority — best price, then earliest (FIFO) at that price.
  • Guard the book: reject orders a trader can’t afford or that blow past limits, before matching.
  • Broadcast the market: publish book changes and trades to every participant with equal latency.
  • Never lose a trade: journal every event before applying it; replay to rebuild the exact book.

The qualities that shape everything

Each one names the mechanism that buys it.

Match in microseconds
An in-memory Order Book of sorted resting bids/asks lets matching just cross the spread — best bid against best ask — with no disk round trip or scan.
Fair, auditable fills
Price-time priority organizes the book into price levels, each a FIFO queue, so the matching rule is built into the data structure — best price, then earliest.
Invalid orders never touch the core
A pre-trade Risk Check on the admission path verifies buying power and position/credit limits before matching, keeping the hot loop fast and deterministic.
Deterministic and replica-safe
One Sequencer assigns a global sequence number and a single-threaded engine consumes that one stream — same input order, same output, on primary and every replica.
No one sees the market first
The engine broadcasts every change to a Market Feed with equal-latency delivery (often multicast) so the same picture reaches everyone simultaneously.
Survive a crash without losing a trade
Journal every event to an append-only log before applying it; a replica replays it and — because the engine is deterministic — rebuilds a bit-identical book and takes over.

The trade-offs you say out loud

Senior signal isn’t the boxes — it’s naming what you gave up and why it was the right price.

An in-memory sorted order book over storing orders in a database and querying for matches

A disk round-trip per order is orders of magnitude too slow and SQL can’t express price-time matching cheaply. The book must live in memory, purpose-built, so matching is just crossing the spread.

Price-time priority (FIFO per level) over largest order first

Favoring size lets big players jump the queue and punishes small orders — manifestly unfair, and traders leave. Best price then earliest is deterministic and auditable; fairness can’t depend on order size.

Pre-trade risk on the admission path over checking risk inside the matching engine

Credit/position checks in the single-threaded hot loop add latency and non-determinism to the core. Validate at the edge so only valid orders reach the matcher and the hot loop stays fast.

One sequencer + single-threaded engine over concurrent multi-threaded matching

Racing threads can’t guarantee the order events were processed, breaking fairness and making replicas diverge. One agreed input stream through one thread is reproducible — and astonishingly fast in-memory.

Journal every event before applying it over periodic snapshots of the book to disk

A snapshot every few seconds loses every order and trade since the last one — catastrophic for an exchange. Log first, apply second: the sequential write is cheap and deterministic replay rebuilds the exact book.

What this teaches

Learn system design by building a stock exchange / matching engine step by step. An interactive guide covering the order book and price-time priority, the matching engine, pre-trade risk checks, deterministic sequencing for low latency, market data feeds, journaling and replay, and clearing.

Key takeaways

  • A matching engine crossing an in-memory order book turns orders into trades.
  • Price-time priority — best price, then FIFO — makes matching fair by construction.
  • Pre-trade risk checks at the edge keep invalid orders out of the core.
  • A single sequencer + single-threaded engine give deterministic, fair, fast matching.
  • A market-data feed broadcasts the same view to all participants with equal latency.
  • An append-only journal plus deterministic replay make the in-memory engine durable.
  • Clearing runs off the hot path; partitioning by symbol scales the exchange out.

Concepts covered

  • What is a stock exchange?
  • Match orders into trades
  • Price-time priority
  • Pre-trade risk checks
  • A single sequencer
  • Market data feed
  • Journal & replay
  • Clearing, settlement & scale
built to be matched, not memorized — make the calls, crash the engine, run the gauntlet.
Finished this one? 0 / 65 System Designs done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More System Designs