Payments & ledgers
Matching engine - price-time priority, and why the clock cannot decide who was first
Teaches why an exchange assigns order with a sequence number instead of measuring it with a clock, and what a single-threaded book buys in return.
What this board gets wrong on purpose
The tension
Every good property on this board - deterministic matching, a standby that recovers by replay, a surveillance answer that is a fact rather than an opinion - is bought by funnelling the entire market through one sequencer and one matching thread. That is one machine’s worth of throughput and one machine’s worth of failure. The failover is drawn as a hot standby replaying the journal, which is honest and incomplete: promotion takes 200-400 ms, during which the market is halted, and the new primary must prove it has consumed every sequence number the old one published or it will re-use numbers that are already on the wire and in a thousand subscribers’ books. Nothing on this board arbitrates that race - there is no external quorum, only a heartbeat, so a network partition that leaves both engines believing they are primary produces two divergent books and two conflicting trade streams, and the only recovery is a busted-trade process run by humans the next morning. The sharding escape hatch (one engine per instrument group) removes the throughput ceiling and does not remove the problem: it multiplies the number of single points of failure, and any order type spanning two instruments loses determinism entirely.
1 · Requirements
An exchange is a fairness machine. Its product is not throughput and not latency - it is the credible claim that the same rules were applied to everybody in the same order, and that the order can be proved afterwards.
Ask these before designing
- Continuous trading, or auctions? Continuous matching is the interesting engine. Opening and closing auctions are a different algorithm - collect, then cross at one price - and most real venues need both, which means two matching modes over one book.
- Which order types? Limit and market is a weekend project. Add stop, iceberg, pegged, and self-trade prevention and the state machine triples. Each new type must preserve determinism, which is why order types are added at roughly one per year and not one per sprint.
- Is there a maker-taker fee? Fees change behaviour, and behaviour changes load. A rebate for resting orders produces a market where 70-80% of messages are cancels, which is a capacity requirement disguised as a pricing decision.
- Who may be first? Strict price-time is one answer. Pro-rata allocation is another, and size-priority is a third. This is a market-structure decision that determines the core data structure, so it cannot be deferred.
- What happens on a halt? Not an edge case: circuit breakers fire on ordinary days. Does the book persist across the halt, or is it purged? Both are defensible, and only one can be true.
Functional
- Accept, amend and cancel orders; every message acknowledged with its sequence number.
- Match strictly by price, then by sequence, and never by timestamp.
- Publish every book change to market data, in sequence, to everybody at once.
- Publish executions privately to the two participants involved.
- Replay any session from the journal and reproduce the same trades, exactly.
- Halt and resume an instrument without losing the book.
Non-functional
- Determinism above everything. Same input sequence, same output, every time, on any machine. Without this there is no standby, no replay, no surveillance and no defence in a dispute.
- p99 tick-to-trade under 20 microseconds inside the engine; p99 gateway-to-ack under 250 microseconds.
- Fairness is measured, not asserted. The spread between the fastest and slowest gateway is published; if one path is reliably quicker, participants pay for it and the venue has an integrity problem.
- No message loss, ever. The journal is written before the engine sees the message, and an unjournalled message does not exist.
- Seven years of replayable history, because that is what a regulator asks for and the unit of the request is "the whole session".
Explicitly out of scope
Clearing and settlement (drawn, not designed), market surveillance algorithms, colocation and the physical network, listing and corporate actions, fee schedules. This board is about the moment two orders meet.
The rest of this board
Available on Tier Slate
This page publishes the question. The answer — 5 more written pages, an 8-step narrated walkthrough and a 5-table schema — is the board itself, and it opens in Tierslate.
- 5written pages
- 8walkthrough steps
- 5tables30 columns
5 pages behind this one
- Capacity estimation
- Storage estimation
- Availability
- How it works
- Deployment plan
More boards
Systems where losing a write is losing money.