orderbook_rs/lib.rs
1//! # High-Performance Lock-Free Order Book Engine
2//!
3//! A high-performance, thread-safe limit order book implementation written in Rust. This project provides a comprehensive order matching engine designed for low-latency trading systems, with a focus on concurrent access patterns and lock-free data structures.
4//!
5//! ## Key Features
6//!
7//! - **Lock-Free Architecture**: Built using atomics and lock-free data structures to minimize contention and maximize throughput in high-frequency trading scenarios.
8//!
9//! - **Multiple Order Types**: Support for various order types including standard limit orders, iceberg orders, post-only, fill-or-kill, immediate-or-cancel, good-till-date, trailing stop, pegged, market-to-limit, and reserve orders with custom replenishment logic.
10//!
11//! - **Thread-Safe Price Levels**: Each price level can be independently and concurrently modified by multiple threads without blocking.
12//!
13//! - **Advanced Order Matching**: Efficient matching algorithm that correctly handles complex order types and partial fills.
14//!
15//! - **Performance Metrics**: Built-in statistics tracking for benchmarking and monitoring system performance.
16//!
17//! - **Memory Efficient**: Designed to scale to millions of orders with minimal memory overhead.
18//!
19//! ## Design Goals
20//!
21//! This order book engine is built with the following design principles:
22//!
23//! 1. **Correctness**: Ensure that all operations maintain the integrity of the order book, even under high concurrency.
24//! 2. **Performance**: Optimize for low latency and high throughput in both write-heavy and read-heavy workloads.
25//! 3. **Scalability**: Support for millions of orders and thousands of price levels without degradation.
26//! 4. **Flexibility**: Easily extendable to support additional order types and matching algorithms.
27//!
28//! ## Use Cases
29//!
30//! - **Trading Systems**: Core component for building trading systems and exchanges
31//! - **Market Simulation**: Tool for back-testing trading strategies with realistic market dynamics
32//! - **Research**: Platform for studying market microstructure and order flow
33//! - **Educational**: Reference implementation for understanding modern exchange architecture
34//!
35//! ## Status
36//!
37//! This project is currently in active development and is not yet suitable for production use.
38//!
39//! # Performance Analysis of the OrderBook System
40//!
41//! This analyzes the performance of the OrderBook system based on tests conducted on an Apple M4 Max processor. The data comes from two types of tests: a High-Frequency Trading (HFT) simulation and contention pattern tests.
42//!
43//! ## 1. High-Frequency Trading (HFT) Simulation
44//!
45//! ### Test Configuration
46//! - **Symbol:** BTC/USD
47//! - **Duration:** 5000 ms (5 seconds)
48//! - **Threads:** 30 threads total
49//! - 10 maker threads (order creators)
50//! - 10 taker threads (order executors)
51//! - 10 canceller threads (order cancellers)
52//! - **Initial orders:** 1020 pre-loaded orders
53//!
54//! ### Performance Results
55//!
56//! | Metric | Total Operations | Operations/Second |
57//! |---------|---------------------|---------------------|
58//! | Orders Added | 587,937 | 117,563.67 |
59//! | Orders Matched | 324,096 | 64,806.12 |
60//! | Orders Cancelled | 4,063,600 | 812,555.98 |
61//! | **Total Operations** | **4,975,633** | **994,925.77** |
62//!
63//! ### Initial vs. Final OrderBook State
64//!
65//! | Metric | Initial State | Final State |
66//! |---------|----------------|--------------|
67//! | Best Bid | 9,900 | 9,840 |
68//! | Best Ask | 10,000 | 10,110 |
69//! | Spread | 100 | 270 |
70//! | Mid Price | 9,950.00 | 9,975.00 |
71//! | Total Orders | 1,020 | 87,155 |
72//! | Bid Price Levels | 21 | 10 |
73//! | Ask Price Levels | 21 | 6 |
74//! | Total Bid Quantity | 7,750 | 688,791 |
75//! | Total Ask Quantity | 7,750 | 912,992 |
76//!
77//! ## 2. Contention Pattern Tests
78//!
79//! ### Configuration
80//! - **Threads:** 12
81//! - **Duration per test:** 3000 ms (3 seconds)
82//!
83//! ### Read/Write Ratio Test
84//!
85//! | Read % | Operations/Second |
86//! |------------|---------------------|
87//! | 0% | 716,117.91 |
88//! | 25% | 32,470.83 |
89//! | 50% | 29,525.75 |
90//! | 75% | 35,949.69 |
91//! | 95% | 73,484.17 |
92//!
93//! ### Hot Spot Contention Test
94//!
95//! | % Operations on Hot Spot | Operations/Second |
96//! |----------------------------------|---------------------|
97//! | 0% | 8,166,484.48 |
98//! | 25% | 10,277,423.77 |
99//! | 50% | 13,767,842.77 |
100//! | 75% | 19,322,454.84 |
101//! | 100% | 28,327,212.19 |
102//!
103//! ## 3. Analysis and Conclusions
104//!
105//! ### Overall Performance
106//! The system demonstrates an impressive capability to handle nearly **1 million operations per second** in the high-frequency trading simulation, distributed across order creations, matches, and cancellations.
107//!
108//! ### Read/Write Behavior
109//! - **Notable observation:** Performance is highest with 0% and 95% read operations, showing a U-shaped curve.
110//! - Pure write operations (0% reads) are extremely fast (716,117 ops/s).
111//! - Performance significantly improves when most operations are reads (95% reads = 73,484 ops/s).
112//! - Performance is lowest in the middle range (50% reads = 29,525 ops/s), indicating that the mix of reads and writes creates more contention.
113//!
114//! ### Hot Spot Contention
115//! - Surprisingly, performance **increases** as more operations concentrate on a hot spot, reaching its maximum with 100% concentration (28,327,212 ops/s).
116//! - This counter-intuitive behavior might indicate:
117//! 1. Very efficient cache effects when operations are concentrated in one memory area
118//! 2. Internal optimizations to handle high-contention cases
119//! 3. Benefits of the system's lock-free architecture
120//!
121//! ### OrderBook State Behavior
122//! - During the HFT simulation, the order book handled a massive increase in order volume (from 1,020 to 87,155).
123//! - The spread increased from 100 to 270, reflecting realistic market behavior under pressure.
124//! - The concentration of orders changed significantly, with fewer price levels but higher volume at each level.
125//!
126//! ## 4. Practical Implications
127//!
128//! - The system is suitable for high-frequency trading environments with the capacity to process nearly 1 million operations per second.
129//! - The lock-free architecture proves to be extremely effective at handling contention, especially at hot spots.
130//! - Optimal performance is achieved when the workload is dominated by a single type of operation (mostly reads or mostly writes).
131//! - For real-world use cases, it would be advisable to design the workload distribution to avoid intermediate read/write ratios (25-75%), which show the lowest performance.
132//!
133//! This analysis confirms that the system design is highly scalable and appropriate for demanding financial applications requiring high-speed processing with data consistency.
134
135mod orderbook;
136
137mod utils;
138
139pub use orderbook::{OrderBook, OrderBookError, OrderBookSnapshot};
140pub use utils::current_time_millis;