
Software Engineering Design Patterns for Trading Systems
Event sourcing, CQRS, and architecture patterns used by exchanges
Why Patterns Matter in Trading Systems
Financial software fails in expensive ways: duplicate orders, inconsistent balances, silent partial failures. Design patterns are not trophies — they are responses to recurring failure modes: event sourcing for auditability, CQRS to scale reads differently from writes, circuit breakers to stop cascading outages, and idempotency so retries do not double-trade.
Pub/Sub and Loose Coupling
Matching engines, risk checks, and market-data publishers should not call each other directly in a giant ball of mud. A publish/subscribe bus lets the matcher emit fills while downstream services subscribe without knowing the implementation details of upstreams — but you must still choose delivery guarantees (at-least-once vs exactly-once) and retention for replay.
Circuit Breakers and Bulkheads
A circuit breaker stops calling a sick dependency after repeated failures, giving it time to recover and protecting your thread pools. Bulkheads isolate resource pools so a runaway analytics job cannot starve order submission.
State Machines and Idempotency
Orders and transfers have legal lifecycles. Encode allowed transitions in a finite state machine so invalid jumps are impossible. Pair that with idempotency keys on client requests so network retries cannot create duplicate live orders.
Matching Engines: Price-Time Priority
Centralized venues match incoming orders against resting liquidity with price-time priority. On-chain matchers (like many L1 DEX designs) must be deterministic: every validator re-executes the same sequence and arrives at identical state — GaiaEx inherits this model from Hyperliquid’s rules.
Pragmatic Adoption
Do not cargo-cult every pattern on day one. Start with clear boundaries, structured logs, and tests around money movement. Add an event log when reconciliation pain appears; add circuit breakers when external APIs flake. Patterns solve real scrapes, not imaginary ones.