HFT & Low-Latency Engineering in C++
Engineer software where nanoseconds are money.
Engineer software where nanoseconds are money. This is the specialization branch for high-frequency trading and ultra-low-latency systems: you learn to measure latency distributions (not averages), write 'mechanically sympathetic' C++ that never allocates or branches on the hot path, build the lock-free queues and ring buffers that move data between threads without contention, implement a limit-order-book matching engine with price-time priority, handle binary market-data feeds over the network with kernel-bypass techniques, and tune a Linux box for deterministic tail latency. The track culminates in a real matching engine fed by a simulated market-data handler, instrumented with latency histograms down to p99.9. Built on the computer-architecture and parallel-performance foundations, this track covers the low-latency C++ techniques used in trading-systems engineering at proprietary trading firms, market makers and hedge funds.
6 phases · 48 projects · v1.1.0
Target roles: Low-Latency C++ Engineer (HFT), Trading Systems Engineer, Market Data / Feed Handler Engineer, Performance Engineer
Stack: C++17/20, Linux, lock-free / std::atomic, perf / rdtsc, TCP/UDP networking
PHASE 1 · 8 projects
The Low-Latency Mindset & Measurement
Think in nanoseconds and tail latency: latency vs throughput, measuring with rdtsc/steady clocks, percentiles (p99/p99.9), jitter, and honest benchmarking.
01 · featured
Latency vs Throughput in Trading
Build latency/throughput measurement programs, model the tick-to-trade pipeline, and understand the economic rationale that drives HFT firms to spend millions optimizing nanoseconds.
C++17 · steady_clock · POSIX threads
02 · featured
Measuring Nanoseconds
Implement RDTSC/RDTSCP wrappers, calibrate TSC cycles to nanoseconds, compare clock sources by overhead, and build a reusable timing_utils.hpp header used throughout the track.
C++17 · x86 RDTSC/RDTSCP · CLOCK_MONOTONIC · std::chrono
03 · featured
Latency Distributions & Percentiles
Build a fixed-bucket logarithmic latency histogram, compute p50/p99/p99.9 from 1M samples, visualize distribution shapes, and understand coordinated omission — the most common latency benchmarking mistake.
C++17 · CLOCK_MONOTONIC · fixed-bucket histogram · HdrHistogram concept
04 · featured
Jitter & Determinism
Measure nondeterminism in a tight loop, attribute outliers to page faults, C-states, TLB shootdowns, and scheduler preemptions, and apply mitigations to reduce tail latency on Linux.
C++17 · CLOCK_MONOTONIC · pthread affinity · mlockall · perf stat
05 · featured
A Cost Model of Operations
Measure and memorize nanosecond costs for L1/L2/L3/DRAM accesses, branch mispredictions, atomic RMWs, syscalls, and malloc, then apply the model to estimate hot-path latency by inspection before benchmarking.
C++17 · pointer chasing · CLOCK_MONOTONIC · perf stat · x86 cache hierarchy
06 · featured
Microbenchmarking Correctly
Learn to defeat the optimizer with DoNotOptimize/escape idioms, apply warm-up and multi-run discipline, use Google Benchmark, and recognize five common ways a microbenchmark can lie.
C++17 · asm volatile · Google Benchmark · CLOCK_MONOTONIC · compiler optimization
07 · featured
Profiling for Tail Latency
Build a program with four known latency-spike sources, then use perf stat/record, flamegraphs, perf c2c (false sharing), and off-CPU analysis to attribute each spike and practice the production tail-latency profiling workflow.
C++17 · perf stat · perf record · perf c2c · FlameGraph · CLOCK_THREAD_CPUTIME_ID
08 · featured
Build a Latency-Measurement Harness
Build latency_harness.hpp — a 200-line single-header library combining RDTSC timing, TSC calibration, logarithmic histogram, timer-overhead subtraction, and a multi-run BenchmarkRunner — and validate it with a 7-test self-test suite.
C++17 · RDTSC/RDTSCP · logarithmic histogram · TSC calibration · DoNotOptimize · single-header library
PHASE 2 · 8 projects
Mechanical Sympathy: C++ for Speed
Write hot-path C++ that respects the machine: no allocations, cache-friendly layout, branch reduction, inlining, object pools, and avoiding virtual/exception costs.
01 · featured
The Hot Path Principle
Identify the common-case hot path in a market-data handler, separate it from cold-path error handling and logging using __builtin_expect and [[gnu::cold]], and measure the latency improvement with an rdtsc timer.
C++17 · x86 rdtsc · GCC cold/noinline attributes
02 · featured
Eliminating Hot-Path Allocations
Identify and remove heap allocations (new/delete, std::string concat, unreserved std::vector) from a market-data serializer's hot path, replacing them with a pre-allocated fixed stack buffer, and measure the p50/p99/p99.9 latency improvement.
C++17 · x86 rdtsc · malloc interposition
03 · featured
Object Pools & Custom Allocators
Build a free-list object pool and an arena bump allocator for Order objects, use std::pmr::monotonic_buffer_resource with pmr::vector, and measure the p50/p99/p99.9 latency improvement over raw new/delete.
C++17 · std::pmr · placement new · x86 rdtsc
04 · featured
Cache-Friendly Data Layout
Redesign an instrument table from fat AoS to SoA and hot/cold split layouts, measure the cache-line-fetch reduction and scan throughput improvement for 4096 instruments, and explain the result in terms of bytes-useful-per-cache-line.
C++17 · alignas · x86 rdtsc · SoA/AoS
05 · featured
Branch Reduction & Predictability
Implement a quote classifier four ways — branchy, sorted-input, branchless arithmetic, and lookup table — and measure the branch-misprediction cost on random vs structured input using an rdtsc timer.
C++20 · x86 rdtsc · branchless arithmetic · constexpr LUT
06 · featured
Inlining & Killing Indirection
Benchmark four call-dispatch mechanisms (virtual, function pointer, std::function, CRTP) on a per-order risk check, measure the latency gap between indirect and inlined calls, and implement a CRTP-based static dispatcher that matches plain-inline performance.
C++17 · CRTP · x86 rdtsc · templates
07 · featured
Avoiding Hidden Costs
Measure the latency of exceptions (thrown vs not-thrown), RTTI dynamic_cast, accidental struct copies, and std::shared_ptr atomic refcounting; produce a documented hot-path ban list with measured cost justification for each item.
C++17 · x86 rdtsc · RTTI · std::shared_ptr · exceptions
08 · featured
Mechanical-Sympathy Lab
Apply all Phase 2 techniques sequentially to a deliberately slow message handler (v0 through v7), measuring p50/p99 latency after each change and documenting the transformation log — targeting at least 20x improvement at p50 and 50x at p99.
C++17 · CRTP · object pool · SoA · branchless · x86 rdtsc
PHASE 3 · 8 projects
Lock-Free Concurrency for Trading
Move data between threads without locks: atomics and memory ordering, SPSC/MPSC ring buffers, the LMAX Disruptor sequencer, busy-spin, and thread-per-core pinning.
01 · featured
Atomics & Memory Ordering for Messaging
Build a series of focused experiments mapping each std::memory_order to hardware instructions and costs, implementing the publish-data-then-release-flag pattern used between network and strategy threads in HFT pipelines.
C++17 · std::atomic · pthreads · ThreadSanitizer
02 · featured
The SPSC Ring Buffer (Production-Grade)
Build a production-grade SPSC ring buffer with power-of-two masking, cache-line-separated head/tail, correct acquire/release orderings, and measured throughput exceeding 100 M msg/s — the workhorse of HFT inter-thread pipelines.
C++17 · std::atomic · alignas(64) · pthreads · ThreadSanitizer
03 · featured
MPSC & Multi-Producer Queues
Demonstrate why two producers on an SPSC queue is an immediate data race, then build a Vyukov-style bounded MPSC queue using CAS on the tail, and benchmark it against SPSC sharding to understand when each design wins.
C++17 · std::atomic · compare_exchange_weak · pthreads · ThreadSanitizer
04 · featured
The LMAX Disruptor Pattern
Build a simplified but functionally complete LMAX Disruptor with pre-allocated ring buffer, monotonic sequence counter, single-writer producer, and multiple independent broadcast consumers — then benchmark it against a mutex queue to understand each source of performance gain.
C++17 · std::atomic · alignas(64) · pthreads · ThreadSanitizer
05 · featured
Busy-Spin vs Blocking
Measure wakeup latency for busy-spin vs condition_variable, implement the _mm_pause() spin hint and exponential backoff, and build the decision framework for choosing spinning vs blocking per pipeline stage in an HFT system.
C++17 · std::atomic · _mm_pause() · std::condition_variable · clock_gettime · pthreads
06 · featured
Thread-Per-Core & Pinning
Pin threads to dedicated CPU cores with pthread_setaffinity_np, measure p99 jitter for pinned vs unpinned threads under background load, demonstrate NUMA-local vs remote memory latency, and build a 3-stage pinned pipeline with measured per-stage handoff latency.
C++17 · pthread_setaffinity_np · sched_getcpu · clock_gettime · pthreads
07 · featured
Seqlocks & Wait-Free Reads
Implement a templated Seqlock<T> with version counter and acquire/release orderings, verify zero torn reads over 480 M read operations, benchmark it against shared_mutex for a one-writer/many-reader workload, and analyze retry rate at different write frequencies.
C++17 · std::atomic · std::shared_mutex · memcpy · clock_gettime · pthreads
08 · featured
Lock-Free Pipeline Lab
Assemble a complete 3-stage lock-free pipeline (producer → processor → publisher) connected by SPSC rings on pinned threads, push 10 million messages end-to-end, and measure p50/p99/p99.9 latency and throughput — the capstone of Phase 3 applying every technique from projects 1–7.
C++17 · std::atomic · pthread_setaffinity_np · clock_gettime · SPSCRing · pthreads
PHASE 4 · 8 projects
The Order Book & Matching Engine
Build the core of an exchange: market microstructure, order types, a price-time-priority limit order book, and a matching engine with O(1) best bid/ask.
01 · featured
Market Microstructure Basics
Build the vocabulary of a matching engineer: bid/ask/spread/mid, price levels and depth, all four order types (LIMIT/MARKET/IOC/FOK), maker vs taker, the complete order lifecycle, and integer tick pricing — the foundation every later project depends on.
C++17 · g++/clang++ · integer arithmetic
02 · featured
The Limit Order Book Data Structure
Build a correct two-sided limit order book using std::map and std::queue: implement add_order, cancel_order, and best-bid/ask queries with strict price-time FIFO priority, print multi-level depth snapshots, assert the crossed-book invariant, and benchmark add throughput to understand why std::map is the wrong choice for HFT.
C++17 · g++/clang++ · std::map · std::queue · std::unordered_map
03 · featured
The Matching Engine — Price-Time Priority
Implement the price-time priority matching loop: an aggressive order walks the opposite side from best price, fills FIFO within each level, generates paired fill records, and either rests or cancels the remainder — with correct handling of MARKET, LIMIT, IOC, and FOK semantics.
C++17 · g++/clang++ · std::map · std::queue · matching algorithms
04 · featured
An Efficient Order Book
Replace the std::map level index with a flat array indexed by price offset and the queue with an intrusive doubly-linked list, achieving O(1) add, O(1) cancel, and O(1) best-bid/ask tracking — then benchmark the 10x+ speedup over the map-based design.
C++17 · g++/clang++ · intrusive linked list · array indexing · benchmarking
05 · featured
Order Management & Fast Cancel
Complete the fast order book's management layer: an order pool (slab allocator) for allocation-free hot-path operation, an ID→Order* hash map for O(1) cancel, and correct exchange-standard amend priority semantics (price-change/qty-up loses priority; qty-down retains it).
C++17 · g++/clang++ · slab allocator · unordered_map · intrusive list
06 · featured
Book Updates & Outgoing Market Data
Add a market data publication layer to the fast LOB: emit incremental BookUpdate messages (ADD_LEVEL, CHANGE_LEVEL, REMOVE_LEVEL) and BBOUpdate messages on BBO changes, build a full-depth snapshot function for new subscribers, and implement sequence-number-based gap detection.
C++17 · g++/clang++ · std::optional · incremental feed design
07 · featured
Testing the Book for Correctness
Build a systematic test harness for the order book and matching engine: define and assert five book invariants, replay a golden order sequence fill-by-fill, run 50,000+ randomized property tests, and cover edge cases including empty book, duplicate cancel, qty conservation, and FOK rejection.
C++17 · g++/clang++ · std::mt19937 · property-based testing · assert
08 · featured
Single-Symbol Matching Engine (Finale)
Assemble a complete price-time-priority matching engine integrating all Phase 4 components: fast array-indexed LOB, order pool, ID map, market data publisher, and correctness checks — then measure throughput (M msgs/sec) and per-message latency distribution (p50/p99/p99.9) and identify the dominant bottleneck.
C++17 · g++/clang++ · high_resolution_clock · slab allocator · intrusive list · array LOB
PHASE 5 · 8 projects
Market Data & Low-Latency Networking
Get data off the wire fast: TCP/UDP and multicast, binary feed parsing (ITCH/FIX-style), gap recovery, kernel-bypass concepts, busy-poll I/O, and clock synchronization.
01 · featured
TCP vs UDP for Trading
Build UDP and TCP sockets that model exchange market-data and order-gateway protocols, measure Nagle's algorithm and head-of-line blocking, and understand why exchanges split traffic across both transports.
C++20 · POSIX sockets · UDP · TCP · TCP_NODELAY
02 · featured
Parsing Binary Market-Data Protocols
Implement an ITCH 5.0-inspired binary protocol parser using packed structs, zero-copy field views, and correct big-endian byte swapping, then benchmark the decode path to confirm zero heap allocation.
C++20 · POSIX · binary protocols · ITCH 5.0 · endianness · zero-copy
03 · featured
Building a Feed Handler
Build a complete UDP feed handler that decodes ITCH-style binary messages, dispatches by message type via a switch table, updates an order book, and measures end-to-end decode+apply latency.
C++20 · POSIX sockets · UDP · binary protocol · order book
04 · featured
Sequencing & Gap Recovery
Build a recovery state machine that detects sequence gaps, fetches a TCP snapshot, buffers live incrementals during recovery, replays them in order, and implements A/B redundant-feed arbitration by sequence number.
C++20 · POSIX sockets · UDP · TCP · state machine · recovery
05 · featured
Kernel Bypass & Fast I/O (Concepts + Busy-Poll)
Study the kernel packet path's latency sources, implement SO_BUSY_POLL and a manual non-blocking spin-poll loop on standard sockets, and survey the kernel-bypass technology landscape (DPDK, AF_XDP, io_uring, a kernel-bypass NIC ef_vi).
C++20 · POSIX sockets · SO_BUSY_POLL · sched_setaffinity · io_uring concepts
06 · featured
Multicast UDP Programming
Build a multicast sender and receiver using IP_ADD_MEMBERSHIP on loopback, batch-receive with recvmmsg, and read software receive timestamps via SO_TIMESTAMPING from the ancillary control message chain.
C++20 · POSIX sockets · IP multicast · recvmmsg · SO_TIMESTAMPING
07 · featured
Timestamps & Clock Synchronization
Measure RDTSC and clock_gettime overhead, demonstrate cross-core TSC skew, build a tick-to-trade latency histogram, and model the PTP offset calculation that enables sub-100 ns clock synchronization for MiFID II compliance.
C++20 · x86 TSC · RDTSC/RDTSCP · clock_gettime · PTP/IEEE-1588
08 · featured
Feed-Handler Lab
Capstone: build a complete multicast feed handler that decodes a binary ITCH stream with zero allocation, detects sequence gaps, updates a pre-allocated order book, and reports p50/p99/p99.9 decode+apply latency over 200,000 messages.
C++20 · POSIX sockets · UDP multicast · binary protocol · order book · latency histogram
PHASE 6 · 8 projects
Systems Tuning & the Matching-Engine Capstone
Tune the box for determinism (core isolation, IRQ affinity, NUMA, huge pages, C-states) and build the capstone matching engine + feed handler with latency histograms.
01 · featured
Tuning Linux for Latency
Configure Linux kernel boot parameters (isolcpus, nohz_full, rcu_nocbs), the CPU frequency governor, C-state limits, IRQ affinity, and measure the before/after jitter reduction with a latency histogram.
C++20 · Linux kernel · cpupower · chrt · taskset · CLOCK_MONOTONIC
02 · featured
NUMA & Huge Pages
Benchmark NUMA-local vs NUMA-remote DRAM latency with pointer chasing, allocate memory on the correct node with libnuma, reduce TLB miss pressure using MAP_HUGETLB, and lock all process memory with mlockall.
C++20 · libnuma · hugetlbfs · mlockall · numactl · CLOCK_MONOTONIC
03 · featured
Core Isolation & Thread Pinning
Pin threads to isolated CPU cores with sched_setaffinity and pthread_setaffinity_np, apply SCHED_FIFO real-time priority with chrt, move workqueue threads off hot cores, and measure jitter reduction across four isolation configurations.
C++20 · pthreads · chrt · taskset · SCHED_FIFO · tuna
04 · featured
Hunting the p99.9
Identify and eliminate tail-latency spikes from page faults, TLB shootdowns, and hot-path allocations using pre-faulting, cache warming, object pools, perf stat, and a pre-flight validation script.
C++20 · perf · mlockall · mmap · CLOCK_MONOTONIC · object pool pattern
05 · featured
Tick-to-Trade Latency Budgeting
Decompose wire-to-wire tick-to-trade latency across feed-handler, book-update, strategy, and order-gateway stages, instrument each with CLOCK_MONOTONIC, set a per-stage budget, and validate budget compliance with a reusable LatencyBudget checker.
C++20 · CLOCK_MONOTONIC · chrt · taskset · PTP concepts · DPDK concepts
06 · featured
Reliability Without Sacrificing Speed
Build an async SPSC log queue, an atomic kill switch, and a stale-data graceful degradation guard — each adding under 50 ns to the hot path — and benchmark the reliability overhead versus naive synchronous alternatives.
C++20 · std::atomic · pthreads · SPSC ring buffer · __builtin_expect · CLOCK_MONOTONIC
07 · featured
Architecture of a Trading System
Map the six components of an HFT trading system (feed handler, order book, strategy, order gateway, risk, logger) onto isolated cores connected by typed SPSC queues, implement the skeleton, and annotate every technique from the track to its architectural home.
C++20 · pthreads · SPSC queue · thread-per-core · CLOCK_MONOTONIC
08 · featured
Limit-Order-Book Matching Engine + Feed Handler (Track Capstone)
Assemble the complete HFT track: a price-time-priority matching engine fed by a simulated binary feed handler over a lock-free SPSC queue, running on pinned threads with no hot-path allocation, instrumented with a latency histogram, packaged with CMake and GoogleTest, and documented with architecture, measurements, and two explicit tradeoffs.
C++20 · CMake 3.14+ · GoogleTest FetchContent · pthreads · SPSC queue · CLOCK_MONOTONIC · chrt · taskset
Next step
Request HFT & Low-Latency Engineering in C++
An advisor reviews the fit and confirms terms in writing. A request is not an acceptance and nothing is charged until you agree.
