Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Redis Integration

Redis serves as the coordination layer — it handles queuing, ID generation, leader election, state sharing, and event broadcasting.

Key Namespace

KeyTypePurposeCommands
engine:order_id_counterStringUnique order IDsINCR
engine:order_queueListOrder FIFO queueRPUSH / LPOP
engine:leaderStringLeader lock (30s TTL)SET NX EX
orderbook:snapshotStringBook state for queriesSET / GET
fills:eventsPub/SubFill broadcast channelPUBLISH / SUBSCRIBE

Connection Setup

The server uses the fred crate with connection pooling:

#![allow(unused)]
fn main() {
let config = Config::from_url(&redis_url)?;
let redis = Builder::from_config(config).build()?;
redis.init().await?;
}

Pub/Sub requires a separate dedicated client (Redis protocol constraint):

#![allow(unused)]
fn main() {
let subscriber = Builder::from_config(config)
    .build_subscriber_client()?;
subscriber.init().await?;
subscriber.subscribe("fills:events").await?;
}

Why Not Store the Book in Redis?

Matching requires iterating price levels and mutating individual orders — doing this in Redis would need complex Lua scripts or multiple round-trips per match.

In-memory matching takes ~1-10 microseconds. Redis round-trips take ~100-200 microseconds each. The in-memory approach is simpler and orders of magnitude faster.

The trade-off: the book is lost if the engine crashes. Acceptable for a toy system.