A high-performance matching engine built in Rust for HFT (High-Frequency Trading) environments. I can execute millions of trades per second on a single core.
The engine is designed with a strong focus on:
- ⚡ Ultra-low latency
- 🧠 Cache efficiency
- 🔒 Memory safety
- 📈 Deterministic performance
- 🔥 Zero-allocation hot path
- ⚡ O(1) matching & cancellation
- 🧩 Intrusive-style linked structures
- 🗂️ Cache-friendly slab allocator
- 🚄 Fast hashing using
FxHashMap - 📊 Built-in benchmark
The matching engine minimizes allocations and maximizes cache locality to achieve predictable performance under heavy load.
Orders are stored inside a pre-allocated Slab allocator, avoiding heap allocations during the critical matching loop.
Incoming Order
│
▼
Pre-allocated Slab
│
▼
Matching Engine
This keeps execution fast and deterministic.
Instead of heap pointers, orders use usize slab indices to form doubly-linked lists.
- Better cache locality
- Reduced pointer chasing
- Faster traversal
- Simpler memory management
[Order #12] <--> [Order #27] <--> [Order #31]
↑ ↑ ↑
slab idx slab idx slab idx
| Operation | Complexity |
|---|---|
| Match head order | O(1) |
| Cancel order | O(1) |
| Append order | O(1) |
| Price lookup | O(log P) |
Where P is the number of active price levels.
Order cancellation is optimized using:
FxHashMap<OrderId, SlabIndex>This allows direct access to orders without scanning price levels.
The central coordinator of the engine.
Maintains:
bids: BTreeMap<Price, PriceLevel>asks: BTreeMap<Price, PriceLevel>
Each price level internally stores a linked list of orders.
Represents an individual order.
Contains:
- Order ID
- Price
- Quantity
- Side (Bid/Ask)
- Linked-list pointers (
prev,next)
Primary entry point for incoming orders.
Automatically:
- Inserts liquidity
- Executes matching
- Routes to:
match_buymatch_sell
Internal matching routine responsible for:
- Trade execution
- Partial fills
- Removing fully filled orders
The main.rs file includes a lightweight benchmark that:
- Warms up the order book
- Executes trades
- Measures latency per trade
This demonstrates the engine’s performance-oriented design philosophy.
- 🦀 Rust
- 📦
slab - ⚡
rustc-hash - 🌳
BTreeMap
- Deterministic latency
- Minimal allocations
- Efficient memory usage
- HFT-ready architecture
- Simple & maintainable internals
let mut book = OrderBook::new();
book.limit_order(Order {
id: 1,
price: 100,
qty: 10,
side: Side::Buy,
});- Lock-free architecture
- SIMD optimizations
- Multi-threaded matching
- Persistent snapshots
- Market orders
- IOC/FOK order support
- Async networking layer
This project demonstrates how modern Rust techniques can be used to build a low-latency, cache-efficient, and allocation-aware matching engine suitable for high-frequency trading systems.