← Back to all Build Your Own X challenges
$./matching_engine --bench --ticks 10000000 --latency-p99
Trading & Matching EnginesExpert
5 Stages• ~16 hours• 15+ Tests• 2 source files

Build Your Own Limit Order Book: 44ns Matching Engine in C++20

Build a lock-free, cache-aligned price-time priority matching engine from scratch with zero dynamic memory allocation.

#C++20#HFT#Market Making#Matching Engine#Low-Latency#Lock-Free#Cache Optimization

In high-frequency trading (HFT) and quantitative market making, median tick-to-trade latency is measured in nanoseconds. Any heap allocation (malloc / new), pointer chasing across fragmented memory, or virtual function call immediately loses the trade.

In this intensive course, you will engineer a production-ready C++20 Limit Order Book matching engine from first principles:

  1. Designing cache-line aligned order structs with alignas(64) and intrusive double-linked lists.
  2. Constructing a flat array-indexed price ladder for O(1)O(1) best-bid-and-offer (BBO) lookups.
  3. Writing a strict price-time priority (FIFO) matching algorithm executing fills in under 44 nanoseconds.
  4. Implementing a lock-free market data feed publisher streaming trades to trading bots without mutexes.
  5. Benchmarking 10,000,000 simulated order inserts, cancels, and executions with cycle-accurate rdtsc profiling.
₹1,799one-time purchase · full source code & all chapters
Instant unlock · Lifetime code repository access
Full lifetime access to this challenge
Interactive browser workspace with copyable code
Automated Test Runner Output
ALL TESTS PASSING

# Executing stage-by-stage verification benchmarks:

[✓] Stage 1: Driver ring interception hook compiled ... PASS (22.4ns)

[✓] Stage 2: Atomic SPSC lock-free ring buffer (zero malloc) ... PASS (1.8ns)

[✓] Stage 3: Stateful TCP SYN flood mitigation under 14.8M pps ... PASS (0 drops)

[✓] Stage 4: Zero-copy userspace telemetry extraction ... PASS

------------------------------------------------------------

Test Results: 15 passed, 0 failed, 100% assertions verified.

What You Will Build & Verify

01

Zero-allocation memory pool managing 1,000,000 pre-allocated order nodes

02

Cache-conscious intrusive doubly-linked list for $O(1)$ order insertions and cancels

03

Array-based price ladder providing sub-5ns Best Bid and Offer (BBO) queries

04

Deterministic matching core executing price-time fills in 44 nanoseconds

05

Real-time benchmark harness measuring p50, p99, and p99.99 latency with rdtsc

Prerequisites

  • Modern C++ (templates, move semantics, atomic operations)
  • Understanding of CPU cache lines (L1/L2/L3, false sharing)
  • Basic trading concepts (Bid, Ask, Spread, Limit Orders, Market Orders)

Stage-by-Stage Roadmap

5 Progressive stages · Each includes complete source code and line-by-line breakdown

1

Stage 1: Cache-Aligned Order Structures & Fixed Memory Pool

FREE PREVIEW
20 min read Start Stage

Eliminate dynamic memory allocation. Build a contiguous memory pool and align order structs to 64-byte CPU cache boundaries.

include/order.hpp
include/order_pool.hpp
2

Stage 2: Intrusive Doubly-Linked Price Levels & BBO Tracking

25 min read Locked

Group orders into price levels and track the Best Bid and Offer (BBO) with flat array indexing.

3

Stage 3: FIFO Price-Time Priority Matching Engine

30 min read Locked

Implement the matching core executing aggressive market orders against passive limit orders.

4

Stage 4: SPSC Lock-Free Market Data Disseminator

25 min read Locked

Stream fill and trade execution messages to listening market making algorithms without mutex locks.

5

Stage 5: Nanosecond Tick-to-Trade Profiling with rdtsc

25 min read Locked

Benchmark the complete engine against 10M ticks, plot latency histograms, and verify 44ns median response.