Leanpub Header

Skip to main content

Deterministic Matching Engines in C++23/26

Lock-Free Order Books, Cache-Line Aligned Price-Time Priority, and Zero-Jitter Trade Execution.

Deterministic Matching Engines in C++23/26

The engine never crashed. Every test passed. Then the opening auction cleared, order flow jumped fivefold, and for eight minutes it matched every order correctly against a book that was already 700 milliseconds out of date. Several hundred clients got fills that were individually plausible and collectively wrong. Nothing in the logs said why.

This book is about that gap between "thread-safe" and "deterministic," and how to close it: lock-free CAS design, cache-line layout measured with perf c2c, zero-allocation hot paths, jitter hunted down to the interrupt level, and proof that the same event log replays byte-for-byte identical ten thousand times, including after a crash.

Minimum price

$29.00

$49.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
About

About

About the Book

Stop treating a matching engine's correctness as something a demo can prove. Learn to build lock-free order books, cache-line aligned price-time priority, and zero-jitter trade execution, guided by a verification discipline that takes the system from a passing test suite to a build you can prove holds under real concurrency, real load, and a real crash.

Key Features

  • Learn the precise, falsifiable definition of determinism and build order book data structures, matching semantics, and price-time priority guarantees that hold under real order flow rather than only under a synthetic benchmark
  • Engineer out non-determinism directly with lock-free CAS loops, hazard-pointer-safe memory reclamation, cache-line alignment verified against real perf c2c measurements, zero-allocation memory pools, and full OS-level jitter elimination
  • Take the engine from a correctness baseline to a field-tested, recoverable production system with adversarial fuzzing, ten-thousand-run deterministic replay, fault-injected crash recovery, and a full annotated reference implementation

Book Description

A matching engine that never crashes in testing is not a matching engine that behaves correctly under real order flow, and the gap between those two claims is not a corner case, it is where production trading systems actually fail. This book treats that gap as the engineering problem it actually is, starting with the honest question of what "deterministic" precisely guarantees and what it does not, before a single line of optimization code gets written.

From there it covers the full stack in order: order book data structures benchmarked under real volume, order types and matching semantics including icebergs, stop-cascades, and amendment priority rules, and price-time priority as a fairness guarantee that individually correct features can still violate the moment they interact, illustrated by a full regulatory-inquiry case study. The engineering chapters that follow eliminate non-determinism at its source, lock-free compare-and-swap design, the ABA problem and hazard pointers, cache-line alignment measured rather than assumed, zero-allocation hot paths backed by pooled memory, and the nine sources of OS and scheduler jitter most engineers building their first low-latency system never think to check.

The final part closes the loop: measuring tail latency honestly with HDR histograms and realistic burst-load generation instead of misleading averages, proving correctness with libFuzzer-driven edge-case discovery and property-based testing, confirming determinism with byte-identical replay across ten thousand adversarially-scheduled runs, and validating crash recovery with fault injection and measured recovery-time objectives, so that every guarantee this book opens with still holds on the other side of a real production incident.

What You Will Learn

  • Define determinism precisely and distinguish it from thread safety, fairness, and low latency
  • Choose and benchmark order book data structures against real, not synthetic, order flow shapes
  • Implement lock-free CAS loops and hazard-pointer-based memory reclamation correctly
  • Diagnose and eliminate false sharing using perf c2c instead of assumed struct padding
  • Build zero-allocation hot paths with pooled memory verified through allocator instrumentation
  • Eliminate OS and scheduler jitter with core pinning, SCHED_FIFO, NUMA placement, and interrupt isolation
  • Measure tail latency correctly using HDR histograms and realistic, burst-shaped load generation
  • Prove correctness with property-based testing, coverage-guided fuzzing, and deterministic replay
  • Design and test crash recovery without silently breaking price-time priority guarantees

Who This Book Is For

This book is for C++ systems engineers, quantitative developers, and trading infrastructure teams building matching engines, order management systems, or any latency-sensitive concurrent system where correctness under load is non-negotiable. Familiarity with modern C++, concurrency fundamentals, and Linux systems programming is assumed; prior exposure to lock-free data structures is helpful but not required.

Table of Contents

  1. The Anatomy of a Production Failure
  2. Order Book Data Structures Under Real Volume
  3. Order Types and Matching Semantics
  4. Price-Time Priority and Fairness Guarantees
  5. Lock-Free Design with Compare-and-Swap
  6. Cache-Line Alignment and False Sharing
  7. Memory Pools and Zero-Allocation Hot Paths
  8. Eliminating OS and Runtime Jitter
  9. Measuring Tail Latency, Not Averages
  10. Deterministic Replay and Property-Based Testing
  11. Fault Recovery Without Losing Determinism; Appendices; Conclusion

Author

About the Author

Weston Ashgrove

Weston Ashgrove is a C++ quant trading developer who builds the parts of a trading system that cannot be allowed to be wrong: order books, matching logic, and the concurrency and memory-layout work underneath them. He has a simple rule for latency-critical code: nothing is true until it has been profiled, fuzzed, and replayed. That rule runs through this book, from the first order book benchmark to byte-identical crash recovery. He writes for engineers who ship and debug these systems, and he skips the theory that never survives contact with real order flow.https://x.com/dxled_dc

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub