An order finds its turn
Illustrative exampleNumbers here are illustrative: round figures to reason with, not measurements.
A broker’s order reaches a gateway, which checks it is well formed and that the session may trade. The sequencer for that stock’s group gives it the next number and the exchange’s time; that number, not any gateway’s or broker’s clock, decides its turn. The order is written down on machines in separate racks before it is matched.
One matcher thread owns each group’s books in memory and applies their orders one at a time, in number order; the answer and fills go back through the gateway. Stocks are spread over matchers, so the market runs in parallel while each stock stays in one line.
- The broker sends the order to its gateway.
- The sequencer numbers it and writes it down.
- The stock’s one matcher applies it in number order.
- The broker hears “accepted” and each fill.
Diagram as text
Components: Broker A (Client); Gateway (API gateway); Sequencer (Service); Matcher (XYZ’s group) (Service); Journal (separate racks) (NoSQL DB); XYZ’s book (Cache). Connections: Broker A → Gateway (buy XYZ); Gateway → Sequencer (well formed); Sequencer → Matcher (XYZ’s group) (next number); Sequencer → Journal (separate racks) (before matching); Matcher (XYZ’s group) → XYZ’s book (one at a time).
Read:Learn: Consistency trade-offsLearn: Producer-consumer / work queue
Check yourself
Two buys for XYZ arrive a microsecond apart through different gateways. What decides which trades first?
Show answerHide answer
The sequencer’s numbering: whichever reaches it first gets the lower number, and the one matcher applies them in that order. The gateways’ clocks play no part.