Back to all posts
PERFORMANCE & MIGRATION • 14 MIN READ • February 28, 2025

Eliminating the Python GIL: Migrating a 240,000 req/s Matching Engine to Rust with PyO3 and Apache Arrow Without a Full Rewrite

An architectural breakdown of an incremental production migration. We examine how introducing foreign function interfaces with zero-copy buffer sharing between Python processes and Tokio task worker pools dropped 99th-percentile jitter by 98.4% and reclaimed $420k in cloud computing overhead.

Prabhat Sharma Principal Systems Architect

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.

bridge.rs
rust
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

MetricBefore (CPython)After (Rust core)
P99 match latency240 ms3.8 ms
Sustained throughput240k req/s240k req/s (same load, 6% CPU)
Resident memory42 GB10 GB
Annual cloud spend$550k$130k