A Rust-based implementation of a centralized exchange with order book management and matching engine, designed for learning high-performance systems programming.
- Throughput: 100,000+ trades per second on M4 Mac Pro
- Concurrent Users: 10,000+ trading agents
- Scalability: Handles high-frequency trading scenarios
- Order Book: Price-time priority matching with FIFO queues
- Matching Engine: Real-time order matching and execution
- Ledger System: Atomic balance management and trade settlement
- Async Runtime: Tokio-based concurrent processing
- Price-Time Priority: Orders matched by best price, then timestamp
- Atomic Operations: All balance updates and settlements are atomic
The order book uses B-tree maps for efficient price level management:
- Bids: Stored in descending price order (highest bid first)
- Asks: Stored in ascending price order (lowest ask first)
- FIFO Queues: Orders at the same price level maintain time priority
- Immediate Execution: Marketable orders execute instantly against resting orders
- Multi-Asset Support: USD and BTC balances tracked per account
- Pre-trade Validation: Balance checks before order placement
- Atomic Settlement: Trade execution and balance updates in single operation
- Order Ownership: Tracks open orders for settlement
- Order Creation: Trading agents generate orders with random parameters
- Balance Check: Ledger validates sufficient funds
- Order Insertion: Order book attempts immediate matching
- Trade Execution: Matched orders create trades
- Settlement: Ledger updates balances atomically
- Order Management: Unfilled orders rest in the book
cargo run --releaseThe simulation will:
- Initialize 100,000 trading accounts with initial balances
- Spawn 100,000 concurrent trading bots
- Process orders at 1ms intervals
- Display real-time performance metrics
Adjust simulation parameters in src/main.rs:
- Number of trading agents
- Initial account balances
- Order generation frequency
- Price ranges and quantities
- Async Task Spawning: Efficient concurrent processing
- Lock-free Data Structures: DashMap for concurrent access
- Memory Layout: Optimized data structures for cache locality
- Batch Processing: Efficient trade settlement
# Run unit tests
cargo test
# Run with logging
RUST_LOG=debug cargo test
This is a toy/educational implementation and should NOT be used in production trading environments.