4 min read
Architecture principles for trading systems
Modularity, scalability and maintainability mean something specific when the system sends orders: a protected hot path, single-writer deterministic state, explicit failure domains, capacity sized for bursts, and replay as the test harness.
Modularity, scalability and maintainability are the usual three principles, and stated that generally they are hard to disagree with and hard to act on. In a system that prices, quotes or routes orders, each one turns into something concrete enough to review a design against. These are the versions I use.
Modularity: a hot path, and everything else
The first boundary to draw is not between business domains. It is between the code that sits between a market event and the resulting order, and all the code that does not.
The hot path gets a latency budget per stage (decode, book update, signal, risk check, encode, send) and a short list of things it may not do: allocate unpredictably, take a contended lock, make a blocking call, or wait on a logger. Persistence, analytics, monitoring and UIs consume what the hot path publishes, asynchronously, and can be slow or down without changing the time to market of an order.
This boundary is what makes the rest of the system easy. Most of the code in a trading stack is on the cold side, where ordinary engineering trade-offs apply and developer time is worth more than microseconds. Without the boundary, every component inherits hot-path constraints it does not need, or the hot path inherits dependencies it cannot afford.
One writer per piece of state
Order state, positions and the book each have exactly one thread that mutates them. Other components send it messages and read what it publishes.
Single ownership removes locks from the hot path, but the larger benefit is determinism. If a component is a state machine whose output depends only on an ordered stream of inputs, then:
- replaying the recorded input reproduces any production behaviour exactly, including the bug from 14:31:07 last Tuesday;
- a hot standby consuming the same stream holds the same state and can take over without reconciliation;
- a new version can be run against yesterday's input and its orders diffed against what was actually sent.
Determinism has a price: time and randomness must be inputs. Logic that calls the system clock directly cannot be replayed. Timestamps arrive on the events, and timers are events too.
Scalability: size for the burst
Load in markets is not shaped like web traffic. Message rates at the open, on an economic release or during a sell-off are multiples of the daily average, they arrive on every instrument at once, and they coincide with the moments when being slow or wrong is most expensive.
So capacity is defined against the worst recorded burst with headroom, and latency is specified in percentiles under that load, not as an average on a quiet afternoon. The design question for every queue is what happens when it fills: market data can usually be conflated to the latest value per instrument, orders and executions can never be dropped, and a consumer that cannot keep up should be disconnected and made to resynchronise instead of slowing the producer. Scaling out is done by partitioning on instrument or on client so that order within a key is preserved.
Failure domains are part of the design
A trading system has an unusual failure asymmetry: stopping is cheap and bounded, continuing while wrong is not. That inverts some defaults.
- Fail closed. On missing market data, a stale reference price, a sequence gap or a failed risk service, the correct behaviour is to stop quoting and cancel, not to retry optimistically.
- Pre-trade controls sit in the order path and do not trust the strategy. Price collars, maximum order size, position and notional limits and message-rate throttles are enforced by a component that strategy code cannot bypass. These controls are also a regulatory requirement, under the SEC Market Access Rule (15c3-5) in the US and RTS 6 under MiFID II in the EU.
- A kill switch that is exercised. RTS 6 requires the ability to cancel all outstanding orders immediately. One that has never been triggered in production conditions is a hypothesis.
- Cancel on disconnect, wherever the venue offers it, so that a dead process does not leave live orders behind.
Each of these is a module boundary as much as a safety feature. The risk gateway is the clearest example: small, slow-changing, independently deployed, and owned separately from the strategies it constrains.
Maintainability: replay is the test harness
The property that makes a trading system safe to change is not documentation, it is the ability to answer "what would this build have done yesterday?" before it trades. A recorded day of market data and order flow, replayed deterministically through the new version, is a regression test that no hand-written suite approaches.
Two related habits pay for themselves:
- Observability with accurate time. Every event is timestamped at each hop from a synchronised clock, so latency can be attributed to a stage instead of argued about. Regulation sets the floor here as well: RTS 25 requires high-frequency participants to stay within 100 microseconds of UTC.
- Changes that are small and reversible. Behaviour behind configuration with a known default, staged rollout by instrument or by desk, and a rollback that is rehearsed. The riskiest release is a large one made urgent by the market.
The short version
Protect the hot path with a hard boundary. Give every piece of state one owner and make components deterministic functions of their input. Size for the burst and decide in advance what each queue does when full. Treat stopping as the safe state. Keep the recordings, because replay is both the debugger and the test suite.