Rust Spot Order Book.
Rust Spot Order Book is a low-latency spot matching engine implemented in Rust, designed for cryptocurrency and digital asset trading platforms. Achieving sub-10 microsecond matching latency and ~100,000 orders/second single-core throughput, it implements a hybrid async-sync architecture that separates non-blocking Tokio networking from a dedicated single-threaded matching core utilizing ordered trees, FIFO queues, and financial decimal arithmetic.
- Role
- Systems & Concurrency Engineer
- Domain
- High-frequency trading systems · Low-latency concurrency · Spot matching engine · Memory efficiency
System at a glance
What the system does
Price-time priority (FIFO) matching
Hybrid async Tokio & dedicated OS thread
Exact financial decimal precision
Sub-10 microsecond matching latency
Architecture flow
From intent to verified state.
- 01
Client order requests arrive asynchronously via the Tokio-based EngineProcessor interface.
- 02
Orders are forwarded across a bounded, lock-free MPSC channel to the dedicated matching engine thread.
- 03
The engine traverses an ordered BTreeMap to find the best matching price level in O(log n) time.
- 04
Orders at identical price levels are resolved from a VecDeque in strict FIFO time priority.
- 05
Trade execution events and order updates stream back asynchronously to consumers over dedicated event channels.
01 / Concurrency model
Hybrid async-sync architecture for zero async overhead
Separating I/O event polling from the performance-critical matching path.
Decoupled I/O and matching
Isolates non-blocking async network handling (Tokio) from the CPU-bound matching critical path, eliminating async runtime scheduling jitter.
Dedicated matching OS thread
Runs the matching engine in an isolated OS thread, ensuring deterministic cache locality and predictable execution timing.
Lock-free MPSC messaging
Connects the async boundary to the matching core via zero-copy message-passing channels, eliminating mutex contention.
02 / Data structures
Ordered price trees and FIFO queues
Fast price discovery combined with fair, deterministic execution ordering.
O(log n) BTreeMap price levels
Organizes bid and ask levels in memory-efficient cache-friendly B-trees, providing fast access to the best bid and ask.
O(1) VecDeque time priority
Implements first-in, first-out (FIFO) matching queues at each price level to enforce strict time priority.
O(1) HashMap order indexing
Maintains direct key-value lookup of active orders by ID, supporting rapid cancellations and modifications.
03 / Execution mechanics
Limit and market order matching with financial precision
Executing orders cleanly according to strict exchange rules.
Price-time priority matching
Matches resting limit orders against incoming aggressive orders at the limit price or better.
Partial fills & remaining balances
Accurately handles partial executions, decrementing order quantities and leaving unfulfilled balances in the book.
Exact decimal arithmetic
Uses rust_decimal for all monetary and quantity calculations, avoiding floating-point precision loss and rounding errors.
04 / Performance & safety
Memory optimization and zero-panic Rust discipline
Predictable memory footprints and type-safe invariants under heavy load.
Sub-10 microsecond latency profile
Demonstrates p99 matching latencies under 10 microseconds with throughput exceeding 100,000 orders per second on single-thread benchmarks.
Clean price-level deallocation
Automatically trims empty price levels from the BTreeMap to maintain compact memory footprints and fast iterations.
Strict type safety
Leverages Rust's exhaustive enums and Result types for error handling, denying unhandled panics across the trading engine.