2025Systems & Infrastructure
CEX Matching Engine
High-Performance Exchange Matching Engine
Overview
A centralized exchange matching engine built from scratch: a Rust core with one engine thread per trading pair, streaming fills through Redis pub/sub to a TypeScript/Bun WebSocket layer. Load-tested at ~5,000 orders/sec with zero errors.
Results
Load test: 30,000 orders, 0 errors, ~5,000 orders/sec sustained, p99 latency ~15 ms.
How it works
- 01
BTreeMap orderbook per pair (BTC/USDT, ETH/USDT, SOL/USDT) with FIFO price-time priority and partial fills.
- 02
Prices stored as fixed-point integers (×10⁸) to avoid floating-point rounding.
- 03
One engine thread per trading pair, fed through its own mpsc channel.
- 04
Redis pub/sub carries fills and book updates to a TypeScript/Bun WebSocket layer for client fan-out.
- 05
PostgreSQL ledger for persisted orders and balances.
How it evolved
- In-memory TypeScript prototype with Express and bcrypt.
- Rust/Actix-web prototype with JWT auth, REST registration, USD and multi-asset deposits, and balance queries, using worker threads with mpsc and oneshot channels.
- Current architecture: per-pair Rust engine threads, Redis pub/sub, and Bun WebSocket fan-out.