Part 7 of 8 · 2 min read
Trade-offs
What to say out loud.
Market data, fairness and what to say
Market data publisher. It rebuilds books and candlesticks from fills and publishes them to subscribers, with different depth by customer (for example 5 levels of L2 by default and 10 for a fee). Memory has an upper limit, so the number of candlesticks is capped, with ring buffers.
Fairness. For a regulated exchange, every receiver of market data must get it at the same time. If the publisher sends to subscribers in connection order, the first one always gains an advantage. Remedies: send to all subscribers in parallel from a shared stream, and use multicast (one send received by many) so no one is first. Equal-length cables in a data centre also remove physical advantages.
Other topics. Security: identity checks, DDoS protection for public pages, and a separate network for trading. Reporting must be accurate and complete. Fees and settlement (moving money and shares after the trade) are outside this design.
| Choice | Gain | Cost |
|---|---|---|
| All on one server | Microsecond latency | Hard to scale, code is complex |
| mmap event store | No network or disk on the path | State lives in files, needs replication |
| Single-threaded loops | No locks or switches | Every task must be short |
| Single sequencer | Total order | One more part to protect |
| Event sourcing | Replay, audit, recovery | Storage and discipline |
| Hot-warm plus Raft | Fast failover, no loss | More servers |
| FIFO matching | Simple, fair | No priority for market makers |
Interview angle
Answer in your head first, then open it.
What makes an exchange different?
Strict, stable latency and perfect order, with almost no data loss.
How is a match found fast?
A book with price levels, doubly linked lists and an order map, all constant time.
How is the order of events guaranteed?
One sequencer stamping all events.
How do you reach tens of microseconds?
A single server, mmap for communication, pinned single-thread loops, and no disk on the critical path.
How do you survive failures?
Hot-warm pairs fed by the same events, replicated by Raft across servers.
One follow-up to try. "Add 10,000 symbols." A good reply: matching for each symbol is independent, so partition symbols across servers, with each server running its own sequencer, event store and Raft group. A routing layer sends each order to the server of its symbol. Orders that span symbols, such as an order that trades two stocks together, need a separate design.
Check yourself: 1. How can a market data feed be unfair? 2. How does the design scale to more symbols? 3. Which choice gives up scale for speed?
Words you will meet
| Word | Meaning |
|---|---|
| Limit order | An order with a fixed price |
| Order book | Open buy and sell orders by price |
| Spread | The gap between best bid and best ask |
| Fill | One side of a match |
| Sequencer | The part that stamps events with a rising number |
| mmap | Mapping a file into memory so processes share it |
| Hot-warm | A working primary and a ready copy |
| RTO and RPO | Allowed stop time, and allowed data loss |