Project case study Personal project

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

01

Price-time priority (FIFO) matching

02

Hybrid async Tokio & dedicated OS thread

03

Exact financial decimal precision

04

Sub-10 microsecond matching latency

Architecture flow

From intent to verified state.

  1. 01

    Client order requests arrive asynchronously via the Tokio-based EngineProcessor interface.

  2. 02

    Orders are forwarded across a bounded, lock-free MPSC channel to the dedicated matching engine thread.

  3. 03

    The engine traverses an ordered BTreeMap to find the best matching price level in O(log n) time.

  4. 04

    Orders at identical price levels are resolved from a VecDeque in strict FIFO time priority.

  5. 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.

01.1

Decoupled I/O and matching

Isolates non-blocking async network handling (Tokio) from the CPU-bound matching critical path, eliminating async runtime scheduling jitter.

01.2

Dedicated matching OS thread

Runs the matching engine in an isolated OS thread, ensuring deterministic cache locality and predictable execution timing.

01.3

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.

02.1

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.

02.2

O(1) VecDeque time priority

Implements first-in, first-out (FIFO) matching queues at each price level to enforce strict time priority.

02.3

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.

03.1

Price-time priority matching

Matches resting limit orders against incoming aggressive orders at the limit price or better.

03.2

Partial fills & remaining balances

Accurately handles partial executions, decrementing order quantities and leaving unfulfilled balances in the book.

03.3

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.

04.1

Sub-10 microsecond latency profile

Demonstrates p99 matching latencies under 10 microseconds with throughput exceeding 100,000 orders per second on single-thread benchmarks.

04.2

Clean price-level deallocation

Automatically trims empty price levels from the BTreeMap to maintain compact memory footprints and fast iterations.

04.3

Strict type safety

Leverages Rust's exhaustive enums and Result types for error handling, denying unhandled panics across the trading engine.

Continue exploring

More systems, protocols, and practical tools.