A high-performance Central Limit Order Book (CLOB) matching engine built with Rust and Axum.
- Price-Time Priority Matching: Orders are matched based on best price first, then by arrival time (FIFO)
- Limit Orders: Support for buy and sell limit orders
- Real-time Matching: Immediate order matching against resting orders
- RESTful API: Simple HTTP API for order management
- Thread-safe: Concurrent access via
RwLockfor safe orderbook operations
┌─────────────────────────────────────────────────────────┐
│ ORDERBOOK │
│ ┌─────────────────────┐ ┌─────────────────────┐ │
│ │ BIDS (Buy Side) │ │ ASKS (Sell Side) │ │
│ │ Sorted by price │ │ Sorted by price │ │
│ │ (highest first) │ │ (lowest first) │ │
│ └─────────────────────┘ └─────────────────────┘ │
│ ↑ ↑ │
│ └─── SPREAD (gap) ──────────┘ │
└─────────────────────────────────────────────────────────┘
- Rust (edition 2024)
- Cargo
git clone <repository-url>
cd clob
cargo build --releasecargo runServer starts at http://localhost:8080
GET /Response:
{
"status": "ok",
"message": "Server is running on 8080"
}POST /create
Content-Type: application/jsonRequest Body:
{
"price": 100,
"quantity": 10,
"user_id": 1,
"side": "Buy"
}| Field | Type | Description |
|---|---|---|
price |
u32 |
Limit price |
quantity |
u32 |
Order quantity |
user_id |
u32 |
User identifier |
side |
string |
"Buy" or "Sell" |
Response:
{
"order_id": 0,
"filled_qty": 0,
"remaining_qty": 10,
"status": "open",
"trades": []
}Order Status:
open- No matches, order resting in bookpartially_filled- Some quantity matchedfully_filled- Entire order matched
GET /depthResponse:
{
"bids": [[99, 20], [98, 15]],
"asks": [[100, 10], [101, 25]],
"last_updated_id": "5"
}Each entry is [price, total_quantity], sorted by best price first.
DELETE /deleteNot yet implemented
curl -X POST http://localhost:8080/create \
-H "Content-Type: application/json" \
-d '{"price": 100, "quantity": 10, "user_id": 1, "side": "Sell"}'curl -X POST http://localhost:8080/create \
-H "Content-Type: application/json" \
-d '{"price": 100, "quantity": 10, "user_id": 2, "side": "Buy"}'curl http://localhost:8080/depthRun the test suite:
cargo testRun tests with output:
cargo test -- --nocaptureclob/
├── src/
│ ├── main.rs # Application entry point, router setup
│ ├── orderbook.rs # Matching engine logic
│ ├── routes.rs # API route handlers
│ ├── input.rs # Request DTOs
│ ├── output.rs # Response DTOs
│ └── errors.rs # Error handling
├── tests/
│ └── api_tests.rs # Integration tests
└── Cargo.toml
MIT