RepositoryRust
AsthaMishra

AsthaMishra/matching-engine

From-scratch limit order book + matching engine in Rust. ~100 ns book ops replaying 104M operations from real NASDAQ ITCH 5.0 data; 143-fill sweeps in 2 µs. Binary OUCH order entry, 10 µs order-to-ack.

50sao
7fork
1issue
2lượt xem

Rust Matching Engine

A from-scratch limit-order-book matching engine with a NASDAQ OUCH/ITCH-style binary gateway, in Rust. Built to learn low-latency systems design from first principles.

On the numbers: every figure below is labeled with exactly what it measures and how. The nanosecond figures are isolated, in-process microbenchmarks of the order book - one component, no network, no protocol. They are not order-to-ack system latency. The end-to-end number (order entry → ack, including the OUCH codec + TCP) is reported separately and is microseconds, not nanoseconds. See Measurement scope.


Layout

Four layered crates - dependencies point downward only (core ← runtime ← adapters ← binary):

CrateRoleREADME
matching-corePure engine: order book, matching, types. No threads, async, or I/O.↗
matching-engineRuntime: sharded worker threads, lock-free routing, response slot pool.↗
ouch-gatewayOUCH/ITCH binary protocol: TCP sessions, codec, order-to-ack latency harness.↗
rest-gatewayREST adapter (Axum) - convenience/queries only, not the low-latency path.↗
serverBinary entry point - wires engine + gateways together.↗
TCP (OUCH binary) ──► ouch-gateway ──► matching-engine (sharded workers) ──► matching-core (OrderBook)
                                            ▲ lock-free channel + slot pool ▲
HTTP (REST, queries) ─► rest-gateway ───────┘

Threading model: each symbol's book is single-threaded (one owner, no locks inside a book); symbols are sharded across a fixed worker pool (symbol_id % num_workers), e.g. 100 symbols over 8 workers ≈ 12–13 symbols/worker. It is not a thread-per-symbol design.

Quickstart

cargo build --release

# Terminal 1 - server (OUCH binary OE on 127.0.0.1:8080)
cargo run --release -p server

# Terminal 2 - load test (1M orders, order-to-ack latency over loopback)
cargo run --release -p ouch-gateway --bin load_client -- 1000000

cargo test            # unit + property tests
cargo bench           # Criterion microbenchmarks (matching-core)

Measurement scope

Three different things are measured three different ways. Don't compare them to each other.

MeasurementWhat it coversExcludesHow it's measuredResult
Order book opA single book operation in isolationNetwork, protocol, threading, syscallsCriterion (synthetic), warm cache, in-process, book allocated outside the timer51 ns top-of-book match · 58 ns cancel at depth-1000 · 0.90 ns BBO · ~30M warm inserts/s
ITCH replayBook reacting to a real trading daySame as above (book only)Instant around each op, 104.6M ops / top-100 symbolsp50 99 ns, p99 502 ns, ~5.9M ops/s (book management; 96% deletes, 0 trades)
Matching pathLarge marketable orders sweeping a deep bookSame as above (book only)Instant per op, 1M synthetic ops, 731k executionssweep of ~143 fills in 2.0 µs (~14 ns/fill)
Order-to-ackOUCH Enter → Accept, codec + TCP included-Client hdrhistogram, loopback, single session, 1M ordersp50 10.4 µs · p99 19.6 µs · pipelined 2.0M orders/s

Caveats, stated plainly:

  • The order-to-ack number is localhost loopback with software timestamps - not external/hardware wire-to-wire. A real NIC + switch path would add latency; this is the floor, not a production figure.
  • The load generator is closed-loop (one order in flight, blocking on each ack), so those are service-time percentiles and the tail is understated by construction. Not offered-load percentiles.
  • ITCH Add messages are passive resting quotes (0 trades), so the replay measures book management (insert/cancel/modify), not matching throughput. Matching is measured separately by synthetic_replay and the Criterion benches.
  • Order-book microbenchmarks allocate the book outside the measured window. Earlier revisions did not, which inflated every figure 4–15× and made runs vary by up to 2.4×; see benchmark methodology.
  • Instant resolution here is ~100 ns, so per-op percentile tables are quantised at that granularity. The BBO figure is the only genuinely sub-nanosecond measurement, and it comes from Criterion's sampling rather than wall-clock timing.
  • Everything runs on x86-64 WSL2, whose VM scheduler injects 100 µs–ms pauses; the p99.9/max tail is partly environmental, not code.

A matching engine lives exchange-side; the point of this project is the data-structure and systems work, not a claim of HFT-grade end-to-end latency.

Stack

Rust 2024 · Tokio · Axum · crossbeam-channel / -queue · Criterion · proptest · cargo-fuzz · hdrhistogram

Dự án tương tự

Khám phá các dự án và mô hình AI khác có sự tương đồng.

RepositoryC++
chronoxor/CppTrader

High performance components for building Trading Platform such as ultra fast matching engine, order book processor

1.1k
Nov 5, 2026
RepositoryTypeScript
fasenderos/nodejs-order-book

Ultra-fast Limit Order Book for Node.js written in TypeScript for high-frequency trading (HFT) :rocket::rocket:

211
Nov 3, 2026
RepositoryRust
raymondshe/matchengine-raft

Thousands of symbols based on K8S fleet. Raft-replicated order-book matching engine in Rust — Durable by consensus, Adapting to Equity/ForEx/Crypto/Derivatives with fee deduction, Deterministic from order intake to market data, Operational console, Lightening fast, Production quality, Nice WebUI, Comprehensive Documentation

64
Nov 25, 2026
RepositoryJava
exchange-core/exchange-core

Ultra-fast matching engine written in Java based on LMAX Disruptor, Eclipse Collections, Real Logic Agrona, OpenHFT, LZ4 Java, and Adaptive Radix Trees.

2.7k
Nov 29, 2026
RepositoryC++
brprojects/Limit-Order-Book

Low latency Limit Order Book and Matching Engine created in C++, able to handle over 1.4 million transactions per second.

225
Jun 12, 2024
RepositoryC#
ArjunVachhani/order-matcher

simple, fast and feature rich order matching engine supports limit, market, stop-loss, iceberg, IOC, FOK, GTD orders

174
Jun 11, 2026
RepositoryC++
Leotaby/MicroExchange

Exchange-grade CLOB matching engine + microstructure analytics in C++20

71
May 31, 2026
RepositoryC++
omerhalid/Real-Time-Market-Data-Feed-Handler-and-Order-Matching-Engine

High-Frequency Trading (HFT) order matching engine optimized for low latency. Features NUMA-aware memory allocation, thread pinning, RDTSC timestamps, lock-free SPSC queues, async logging, and UDP busy polling. Built with C++20 for Linux production environments.

53
Nov 10, 2025
RepositoryRust
0b01/tectonicdb

Database for L2 orderbook

752
Jan 25, 2024