Skip to content
Jatin Sharma
All projects

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

  1. 01

    BTreeMap orderbook per pair (BTC/USDT, ETH/USDT, SOL/USDT) with FIFO price-time priority and partial fills.

  2. 02

    Prices stored as fixed-point integers (×10⁸) to avoid floating-point rounding.

  3. 03

    One engine thread per trading pair, fed through its own mpsc channel.

  4. 04

    Redis pub/sub carries fills and book updates to a TypeScript/Bun WebSocket layer for client fan-out.

  5. 05

    PostgreSQL ledger for persisted orders and balances.

How it evolved

  1. In-memory TypeScript prototype with Express and bcrypt.
  2. 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.
  3. Current architecture: per-pair Rust engine threads, Redis pub/sub, and Bun WebSocket fan-out.