A real-time, concurrent, fault-tolerant Order Book built in Rust.
Supports order matching, order insert/cancel, WebSocket API, structured logging, stress testing, and benchmarking.
Designed with Rust best practices, SOLID principles, security, and fault tolerance.
- src/main.rs — WebSocket server bootstrap & logging initialization
- src/order.rs —
Ordermodel andSideenum (Buy/Sell) - src/order_book.rs — core matching engine (insert, cancel, match; price/size book)
- src/messages.rs — JSON commands:
Insert { order },Cancel { id, price, side } - src/ws.rs — WebSocket routing and per-connection async session handling
- src/bin/client_test.rs — load/stress client (simulates inserts/cancels)
- benches/ — Criterion benches (
insert,cancel,match,integration)
git clone <repo>
cd order_book
# start server (WebSocket on ws://127.0.0.1:3000/ws)
cargo run --bin ord_book
# run simulated clients (optional)
cargo run --bin client_test// Insert order
{ "action": "insert", "order": { "id": 1, "price": 101, "quantity": 10, "side": "Buy" } }
// Cancel order
{ "action": "cancel", "id": 1, "price": 101, "side": "Buy" }cargo test # run all tests
cargo test -- --nocapture # show println!/tracing output
# examples:
# cargo test partial_match
# cargo test -- --list- Unit tests: insert / cancel / match logic
- Integration tests: multi-threaded insert + cancel behavior
- Load tests:
client_testsimulating concurrent clients
cargo bench # run all benches
cargo bench --bench insert # run only the insert bench file
cargo bench --bench insert -- --list # list benchmarks in the file
cargo bench --bench insert -- --bench "insert 1M orders" # run by exact nameReports: target/criterion/<bench_id>/report/index.html (SVG/HTML charts).
Example (1M orders, indicative): Insert ~35–40 ms • Cancel ~12 ms • Match ~40 ms
- Concurrency:
async/awaiton Tokio; one async task per WebSocket connection - Shared state:
Arc<Mutex<OrderBook>>(short lock scopes) - Deterministic structure:
BTreeMapfor ordered price levels (price-time priority) - Serialization:
serdefor JSON I/O - Observability:
tracingwith daily-rotating file logs (ANSI disabled for clean files) - Design: Command pattern (Insert/Cancel), actor-like sessions, repository-style mutation boundary, SOLID module boundaries
- Validate incoming JSON commands and
sidevalues (Buy/Sell) - Isolate each client session; a misbehaving client won’t affect others
- Bound message sizes and consider timeouts/rate limits if exposed publicly
- Avoid long-held locks; keep
OrderBookcritical sections minimal - Fail-fast on unrecoverable states; log structured context for analysis
- Price priority matching (best price wins)
- Partial fills; limit order fallback when no match
- Cancel by
id/price/side - Structured logs for inserts, cancels, and matches
- Concurrent client handling via WebSockets
- Benchmarked core operations (Criterion)
- Live visualization (React or TUI)
- Persistence (Postgres/SQLite) for orders & trades
- Broadcast matched trades to all clients
- Sharded/lock-free design for high-throughput (HFT) workloads