A limit order book and matching engine in Rust for high-frequency trading. Processes 44 million orders per second on Apple M4.
- Price-time priority matching
- 64-byte cache-aligned order structs
- Pre-allocated slab memory (16 million orders, 1GB)
- Lock-free SPSC ring buffer for message passing
- Zero heap allocation on the hot path
cargo build --releasecargo testcargo benchMeasured on Apple M4:
| Metric | Result |
|---|---|
| Throughput | 44.3 million orders/sec |
| Order struct size | 64 bytes |
| Slab capacity | 16,777,216 orders |
src/
├── lib.rs Module exports
├── order.rs Order struct (64-byte aligned)
├── slab.rs Pre-allocated memory arena
├── price_level.rs Price level aggregation
├── orderbook.rs Bid/ask matching logic
├── ring_buffer.rs Lock-free SPSC queue
└── engine.rs Matching engine
The Order struct fits exactly in one cache line:
| Field | Type | Bytes |
|---|---|---|
| id | u64 | 8 |
| price | u64 | 8 |
| quantity | u64 | 8 |
| remaining | u64 | 8 |
| timestamp | u64 | 8 |
| next | u32 | 4 |
| prev | u32 | 4 |
| side | u8 | 1 |
| order_type | u8 | 1 |
| padding | [u8; 6] | 6 |
| Total | 64 |
Pre-allocates all memory at startup. Uses 32-bit indices instead of 64-bit pointers. Lock-free allocation via compare-and-swap.
Single-producer single-consumer queue with Release/Acquire memory fences. Cache-padded indices prevent false sharing.
MIT