The Ceiling We Hit
The engine matched 240,000 orders per second at peak, but the Global Interpreter Lock serialized every mutation of the orderbook. Under microbursts, tail latency ballooned: a P99 of 240ms meant market makers were quoting against stale state. Horizontal scaling only multiplied the coordination overhead.
A full rewrite was off the table — the surrounding risk, settlement, and compliance code was 180k lines of battle-tested Python. The question was surgical: can we move only the orderbook into Rust and leave everything else untouched?
The Zero-Copy FFI Boundary
We exposed the Rust core to Python with PyO3, but the win came from never copying order payloads across the boundary. Apache Arrow gave us a columnar memory layout both runtimes agree on, so Python handed Rust a pointer, not a serialized blob.
use pyo3::prelude::*;use arrow::array::{Float64Array, UInt64Array}; /// Matches a batch of orders already laid out in Arrow buffers./// No deserialization: we read straight from Python-owned memory.#[pyfunction]fn match_batch(prices: &Float64Array, qty: &UInt64Array) -> PyResult<usize> { let mut filled = 0; for i in 0..prices.len() { if ORDERBOOK.with(|b| b.borrow_mut().cross(prices.value(i), qty.value(i))) { filled += 1; } } Ok(filled)}Production Results
| Metric | Before (CPython) | After (Rust core) |
|---|---|---|
| P99 match latency | 240 ms | 3.8 ms |
| Sustained throughput | 240k req/s | 240k req/s (same load, 6% CPU) |
| Resident memory | 42 GB | 10 GB |
| Annual cloud spend | $550k | $130k |