This project implements a simple order book service. It provides an HTTP API for managing limit orders, retrieving order book summaries, and accessing recent trade history.
The service exposes the following HTTP endpoints:
POST /v1/orders/limit- Submit a limit orderGET /{currencyPair}/orderbook- Get the order book summary for a specific currency pairGET /{currencyPair}/tradehistory- Retrieve recent trades for a specific currency pair
-
Order Book Structure: Each order book uses two
TreeMaps (one for bids, one for asks) to maintain sorted price levels. TheTreeMapuses a Red-Black tree internally, ensuring efficient sorting on the price key. -
Order Storage: At each price level, orders are stored in an
ArrayList<LimitOrder>, preserving the order of arrival.ArrayListwas chosen for its performance benefits due to better cache locality. -
Concurrency: Coroutine channels are used to ensure that each order book processes orders sequentially while allowing different order books to operate in parallel.
-
Trade Recording: Recent trades are stored in a fixed-size
ArrayDequefor efficient retrieval of the latest trades.
- The choice of
ArrayListfor storing orders at each price level was based on simulated performance tests. Real-world conditions may be different, so performance testing is recommended. - According to Jane Street, order addition and cancellation occur much more frequently than order fills, so optimization would likely be done on these use-cases.
To run the tests:
./gradlew clean test
To run the application:
./gradlew clean run
curl -X POST http://localhost:8080/v1/orders/limit \
-H "Content-Type: application/json" \
-d '{"side":"SELL","quantity":"0.4","price":"29800","pair":"BTCUSD"}'curl http://localhost:8080/BTCUSD/orderbookcurl http://localhost:8080/BTCUSD/tradehistorySupported currency pairs are defined in the conf/config.json file. Modify this file to add or remove supported trading pairs.