CSE Roadmap

Specialist tier · about 5 hours · 8 parts

Part 3 of 8 · 3 min read

First design

Three flows.

Trading flow (the critical path).

  1. A client places an order in the broker's app.
  2. The broker sends it to the exchange.
  3. The client gateway receives it. It checks the input, rate limits, authenticates and normalises, and passes it on. It must stay light.
  4. The order manager applies the risk checks (rules from the risk manager).
  5. It checks the user's wallet for enough funds (page 9) and holds them.
  6. It sends the order to the sequencer, which stamps a sequence number on it.
  7. The matching engine takes orders in sequence order, keeps an order book per symbol, and looks for matches. A match creates two executions (fills): one for the buyer and one for the seller.
  8. The executions return through the sequencer, which stamps them too.
  9. The order manager updates order states and returns fills through the gateway to the broker, and on to the client.

Market data flow. The matching engine's stream of executions goes to the market data publisher. It builds order books and candlestick charts, and sends them to a data service. Brokers read from it and pass data to their clients.

Reporting flow. The reporter gathers fields from both orders and executions (client, price, quantity, filled quantity, remaining quantity) and writes them to a database for reports, tax and compliance. It is not on the critical path, but accuracy matters more than speed here.

The sequencer. It stamps each incoming order and each pair of executions with a number that rises by one each time, so missing numbers are easy to spot. It also acts as a message queue and an event store, like a pair of log streams around the matching engine. Sequence numbers give a single, agreed order for everything. Replaying the same orders gives the same fills. That is determinism, the base of recovery.

Order manager. It tracks the state of every order (new, partly filled, filled, cancelled). The many state changes make this the most complex part, and event sourcing suits it (section 4).

Client gateways differ. Retail clients use web or mobile and care about security. Large institutions need very low latency. At the extreme, they place their own servers in the exchange's data centre (colocation), so latency is limited by the speed of light along a cable.

A worked example (made up). A sell order for 500 shares at 100.10 rests in the book. A buy order for 200 at 100.10 arrives, gets sequence 1,001, and matches. The engine creates a fill of 200 for the buyer and a fill of 200 for the seller (sequences 1,002 and 1,003). The sell order now has 300 left in the book.

Matching engine

A matching engine keeps the order book of each symbol and pairs buy orders with sell orders at compatible prices, producing executions.

Example: a buy at 100.10 and a sell at 100.10 meet, and the engine records one fill for each side.

Check yourself: 1. Why must the gateway stay light? 2. What does the sequencer stamp? 3. Which flows are not on the critical path?