Crate order_book

Crate order_book 

Source
Expand description

A high-performance tested and benchmarked price-priority order-book implementation using an external cache to minimize lock contention and maximize concurrency.

§Architecture

This library separates concerns into two independent services:

  1. OrderBook: The core order book that maintains price-time priority
  2. MarketDepthCache: An external cache that aggregates market depth

These services communicate via events (OrderEvent), allowing them to operate with separate locks and enabling high concurrency for readers and writers.

§Example Usage

use order_book::{OrderBook, MarketDepthCache, Order, Side};
use rust_decimal::Decimal;
use parking_lot::RwLock;
use std::sync::Arc;

// Create the order book and cache
let order_book = Arc::new(RwLock::new(OrderBook::new()));
let market_depth_cache = Arc::new(MarketDepthCache::new());

// Insert an order
let order = Order::new(100.50, 100, Side::Bid);

// 1. Acquire write lock briefly to insert order
let event = {
    let mut book = order_book.write();
    book.insert_order(order)
}; // Write lock released immediately

// 2. Update cache (uses its own lock)
market_depth_cache.process_order_event(event);

// 3. Query spread (read lock on order book)
let (best_bid, best_ask, spread) = order_book.read().compute_spread();

// 4. Query market depth (read lock on cache)
let (bid_depth, ask_depth) = market_depth_cache.get_aggregated_market_depth();

The order-book and the cache use separate locks, which means that multiple readers can access the book and cache simultaneously without blocking each other.

Also, on the performance side, order insertions only hold the lock for a brief period, that is a $O(\log{N})$, because we’re relying on the BTreeMap’s efficient insertions.

Lastly, the cache is updated asynchronously, which means that it does not block the order book. This allows for high concurrency and responsiveness in the order book.

Structs§

Decimal
Decimal represents a 128 bit representation of a fixed-precision decimal number. The finite set of values of type Decimal are of the form m / 10e, where m is an integer such that -296 < m < 296, and e is an integer between 0 and 28 inclusive.
MarketDepthCache
An external cache service that maintains aggregated market depth.
Order
Represents a single order in the order book.
OrderBook
The core order book structure that maintains price-time priority.
OrderEvent
Represents an event published by the OrderBook when its state changes.

Enums§

Side
Represents the side of an order in the order book.

Type Aliases§

AggregatedDepthMap
Type alias for aggregated market depth cache.
ExactPriceLevelMap
Type alias for a price level in the order book.
RwLock
A reader-writer lock