All projects

Project 03 / Trading systems · Go

Order Matching Engine

The core of an exchange, in Go: it matches buy and sell orders by price-time priority, at 92K orders a second with a p99 under 29ms.

Year
2025
Role
Solo — design, build, benchmark
Type
Exchange core
Status
Open source
Orders / sec
92,435
Target was 30,000
p50 latency
3.47ms
Target ≤ 10ms
p99 latency
28.93ms
Target ≤ 50ms
Cancel & status
O(1)
No book scan

01

The problem

A matching engine has one job and no slack in it: when a buyer and a seller agree on price, trade them — at the right price, in the right order, every time. The best price wins; at the same price, whoever arrived first wins. Get it wrong once and someone was filled ahead of where they stood in the queue.

I built that core against hard targets: at least 30,000 orders a second, a median latency under 10ms and a p99 under 50ms — with limit and market orders, partial fills, cancels and order status, all over a plain REST API.

02

How it works

The life of an order
  1. 01

    REST API

    POST /api/v1/orders validates side, type, price and quantity, then hands a typed order to the engine.

  2. 02

    Symbol router

    Finds the book for the symbol under a read lock. A book is created once, on first sight, with a double-checked write lock.

  3. 03

    Per-symbol lock

    Every symbol has its own RWMutex, so an AAPL order never waits on a BTC order.

  4. 04

    Match

    Walk the opposite side from the best price — a B-tree of price levels with a FIFO queue inside each. Fill, partially fill, or rest in the book.

  5. 05

    Order store

    Every order is indexed by ID in a hashmap, so cancel and status are a lookup, not a scan.

03

Key decisions

  • A B-tree for price levels

    O(log N) insert and delete, with ordered traversal for walking the book — and a more cache-friendly layout than pointer-heavy structures.

    Ruled outSorted arrays (O(N) inserts) and skip lists (sound, but less cache-efficient)

  • A FIFO queue inside each price level

    Time priority holds by construction, and append and remove are O(1) — exactly what partial fills and cancels need.

    Ruled outSorting a level by timestamp on every match

  • One lock per symbol

    Contention stays inside a single book. Unrelated symbols match in parallel.

    Ruled outA single global lock, which serialises every order on the exchange

  • Integer prices and quantities

    Every amount is an int64. No floating-point rounding in a system that moves money.

    Ruled outfloat64

  • Reject unfillable market orders up front

    A market order is checked against the liquidity in the book first. If the book can't fill it, it's rejected whole instead of half-executing.

  • Everything in memory

    A database round-trip costs more than the whole latency budget.

    Ruled outPersisting each order to a database on the hot path

04

In the code

The match loop for a buy order — price priority from the B-tree, time priority from the queue
// Walk the asks from the best (lowest) price until the buy is filled
for order.RemainingQuantity() > 0 && ob.asks.Len() > 0 {
    bestAskLevel, _ := ob.asks.Min()
    if order.Type == Limit && order.Price < bestAskLevel.Price {
        break // the rest of the order waits in the book
    }

    // Inside a price level, first in is first filled
    for bestAskLevel.Orders.Len() > 0 {
        element := bestAskLevel.Orders.Front()
        askOrder := element.Value.(*Order)

        tradeQuantity := min(order.RemainingQuantity(), askOrder.RemainingQuantity())
        trades = append(trades, ob.createTrade(order.ID, askOrder.ID, askOrder.Price, tradeQuantity))

        order.FilledQuantity += tradeQuantity
        askOrder.FilledQuantity += tradeQuantity

        if askOrder.RemainingQuantity() == 0 {
            askOrder.Status = StatusFilled
            filledOrders = append(filledOrders, askOrder)
            ob.removeOrder(element)
        } else {
            askOrder.Status = StatusPartialFill
        }

        if order.RemainingQuantity() == 0 {
            return trades, filledOrders
        }
    }
}

05

What I'd do next

  • Durability. A restart loses the book today. Next: a write-ahead log of accepted orders, replayed on boot.

  • Measure the tail. wrk doesn't report p999, and the worst request took 145.86ms. A harness with HDR histograms would put a real number on it.

  • Remove the last shared lock. Per-symbol locks keep matching parallel, but the global order store sits behind one mutex that every submit touches briefly. Sharding it by symbol removes that.

  • One pass for market orders. The liquidity check walks the book before the match walks it again; folding the check into the match loop removes the second pass.

06

Stack

Language
Go 1.24
Data structures
google/btreecontainer/listHashmaps
API
net/httpREST
Ship
Docker (multi-stage, distroless)Docker ComposeKubernetes
Test & measure
go testwrkk6Postman