make rundocker compose up -d make testmake benchmake load_testmake curl-routes
make curl-flowStart the container using make docker-start and run make k8s-start
Each symbol has one independent OrderBook protected by parking_lot::Mutex. Matching for a single symbol is serialized, which preserves strict price-time priority and avoids races. Different symbols can be processed in parallel because the symbol registry is a DashMap<Arc<str>, Arc<Mutex<OrderBook>>>, so there is no global order book lock.
The order book uses:
BTreeMap<i64, PriceLevel>for bids and asks.- Negative bid keys, so
first_key_value()returns the highest bid in O(1). - Positive ask keys, so
first_key_value()returns the lowest ask in O(1). VecDeque<OrderId>per price level for FIFO matching.FxHashMap<OrderId, Order>for order state.FxHashMap<OrderId, (side, price_key)>for O(1) cancel lookup.
Market orders must fill completely or they are rejected before any book mutation occurs. Limit orders can partially fill and rest the remaining quantity in the book.
Submit an order:
curl -s -X POST http://127.0.0.1:8082/api/v1/orders \
-H 'Content-Type: application/json' \
-d '{"symbol":"AAPL","side":"SELL","type":"LIMIT","price":15050,"quantity":100}'Required endpoints:
POST /api/v1/ordersDELETE /api/v1/orders/{order_id}GET /api/v1/orders/{order_id}GET /api/v1/orderbook/{symbol}?depth=10GET /api/v1/orderbook/{symbol}/tui?depth=10GET /healthGET /metrics
The TUI of OrderBook:
curl -s http://127.0.0.1:8082/api/v1/orderbook/{SYMBOL}/tui?depth=10Latest local verification on this machine:
cargo bench --bench throughput -- --sample-size 10:match_order_simple_fill: about 7.6 microseconds per order.match_order_walk_10_levels: about 9.0 microseconds per order.match_order_market_5_levels: about 8.3 microseconds per order.cancel_50_orders: about 12.0 microseconds per 50 cancels.
- Full HTTP matching-flow load test:
- Command:
LOAD_URL=http://127.0.0.1:8082 CLIENTS=100 WARMUP_SECS=5 DURATION_SECS=60 SYMBOLS=8 ./target/release/load_test- Successes: 4952683
- Failures: 0
- Throughput: 82544.72 orders/sec
- p50: 1.081 ms
- p99: 3.675 ms
- p999: 9.471 ms
- Command:
- as everything is inmemory there is no hardware limitation in storing 10000 removed transactions from order book in memory.
- a person will not trade his own trade to block the liquidity.
- Concurrency as there is always a better way to handle system design.
- I would have integerated logging using either kafka or reddis for entire record and try implementing bonus features with better optimization with more time.
