Velocity DEX is a high-performance, crash-resilient Central Limit Order Book (CLOB) matching engine built from scratch in Rust.
Designed for low latency and high throughput, it utilizes the Actor Model to separate the synchronous matching logic from asynchronous network I/O. It supports high-frequency trading via gRPC, pushes real-time market data via WebSockets, and ensures data durability through an event-sourced Write-Ahead-Log (WAL).
-
Ultra-Fast Matching Engine:
- In-memory Orderbook with Price-Time Priority matching.
- Optimized memory layout using
Slaballocation (Zero-allocation hot path). - O(1) order lookup and cancellation via HashMap indexing.
- Supports Maker (Limit) and Taker (Market) orders with partial fills.
-
Crash Resilience (Persistence):
- Implements Event Sourcing with a binary Write-Ahead-Log (WAL).
- Automatic state recovery and log replay upon server restart.
- Serialized using
bincodefor maximum efficiency.
-
Dual-Interface Architecture:
- Trading API (gRPC): High-performance protobuf-based API for placing and canceling orders (
Tonic). - Market Data (WebSocket): Real-time push notifications for trade execution and order updates (
Axum).
- Trading API (gRPC): High-performance protobuf-based API for placing and canceling orders (
-
Safety & Compliance:
- Self-Trade Prevention (STP): Automatically prevents users from matching against their own orders.
- Atomic state transitions ensures orderbook consistency.
Velocity DEX was stress-tested using a custom load generation tool (crates/bench-tool).
Hardware: Lenovo ThinkPad T470s (Intel Core i5-6300U @ 2.40GHz - Dual Core)
Conditions: 50 Concurrent Users, 50,000 Orders
Result:
| Metric | Result |
|---|---|
| Throughput | ~4,410 TPS (Orders/Sec) |
| Avg Latency | 11.2 ms (End-to-End) |
| p99 Latency | 22.5 ms |
Note: These results are constrained by legacy dual-core hardware. On modern bare-metal servers, throughput is expected to exceed 50k+ TPS.
The system is organized as a Cargo Workspace with modular crates:
velocity-dex/
├── crates/
│ ├── engine-core/ # The Brain: Pure matching logic, Orderbook struct, WAL handler.
│ ├── api-server/ # The Mouth: gRPC server (Port 50051) & WebSocket server (Port 3000).
│ ├── trading-cli/ # The Hand: Command-line interface for manual trading.
│ └── bench-tool/ # The Hammer: Load testing tool for performance metrics.
└── proto/ # Shared Protobuf definitions.
Velocity uses tokio::sync::mpsc channels to funnel concurrent requests into a single-threaded MarketProcessor. This eliminates the need for complex Mutex locking on the orderbook, ensuring deterministic execution and reducing thread contention.
- Rust (latest stable)
- Protoc (Protocol Buffer Compiler)
The server starts both the gRPC Trading Engine (port 50051) and WebSocket Market Data feed (port 3000).
# Run in release mode for best performance
cargo run --release -p api-server
Open a new terminal to interact with the engine.
# Check Orderbook Depth
cargo run -p trading-cli -- depth
# Place a Sell Order (Maker)
cargo run -p trading-cli -- sell --price 100 --quantity 50 --user-id 1 --order-id 1001
# Place a Buy Order (Taker - Matches immediately)
cargo run -p trading-cli -- buy --price 100 --quantity 10 --user-id 2 --order-id 2001
You can use any WebSocket client (like browser extensions or wscat) to listen to live market data.
- URL: ws://127.0.0.1:3000/ws
Sample JSON Output:
{
"type": "ORDER_PLACED",
"id": 1001,
"price": 100,
"qty": 50,
"side": "Ask"
}
To reproduce the performance metrics:
First, Start the server in release mode (cargo run --release -p api-server). Then, Run the benchmark tool in a separate terminal:
# Simulate 50 concurrent users sending 50,000 orders
cargo run --release -p bench-tool -- --count 50000 --concurrency 50
- Language: Rust 🦀
- Runtime: Tokio (Async I/O)
- gRPC: Tonic (Prost)
- Web Framework: Axum (WebSockets)
- Serialization: Serde & Bincode
- Memory: Slab (Arena allocation)
- Metrics: HdrHistogram
This project is open-source and available under the MIT License.