Skip to content
AnnonnymmousssPublic

About

High-throughput, low latency, inmemory REST order matching engine for limit and market orders.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

To start the server

make run

Run through Docker

docker compose up -d 

To run the tests

make test

To run the benchmarks

make bench

To run the load test

make load_test

To run test scripts

make curl-routes
make curl-flow

To start orchestration

Start the container using make docker-start and run make k8s-start

Design

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.

API

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/orders
  • DELETE /api/v1/orders/{order_id}
  • GET /api/v1/orders/{order_id}
  • GET /api/v1/orderbook/{symbol}?depth=10
  • GET /api/v1/orderbook/{symbol}/tui?depth=10
  • GET /health
  • GET /metrics

The TUI of OrderBook:

curl -s http://127.0.0.1:8082/api/v1/orderbook/{SYMBOL}/tui?depth=10

alt text

Measured Performance

Latest 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
The full testing and API information are provided in /docs.

Assumptions

  • 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.

Limitation

  • Concurrency as there is always a better way to handle system design.

Note

  • I would have integerated logging using either kafka or reddis for entire record and try implementing bonus features with better optimization with more time.

About

High-throughput, low latency, inmemory REST order matching engine for limit and market orders.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages