This project demonstrates low-latency, high-performance system design in C++ for real-time data processing, such as high-frequency trading (HFT). It focuses on:
- Lock-free data structures
- NUMA-aware memory management
- Thread affinity and concurrency
- Performance benchmarking
- Market Data Pipeline (
src/market_data.cpp,src/market_data.h):- CSV replay from
data/mock_market_data.csv - Replay modes: max speed, fixed rate, realtime
- Sequence gap, duplicate, and out-of-order detection
- Lock-free queue and memory pool for minimal latency
- Thread affinity for NUMA optimization
- CSV replay from
- Limit Order Book (
src/order_book.h):- Maintains L1/L2/L3 bid and ask levels per symbol
- Handles ADD, MODIFY, CANCEL, TRADE, QUOTE, and SNAPSHOT events
- Tracks best bid/ask, spread, mid-price, and depth imbalance
- Feature Engine (
src/feature_engine.h):- Computes microprice, rolling VWAP, rolling volatility, trade intensity, signed volume, and N-tick returns
- Uses fixed-size rings for O(1) rolling updates
- Strategy Sandbox (
src/strategy.h):- Simulates market making, momentum, mean reversion, and imbalance-driven orders
- Supports latency-aware backtest timestamps before risk checks
- Risk Layer (
src/risk.h):- Enforces kill switch, max position, per-symbol position, max order size, max notional, and stale-data rejection
- Order Gateway Simulator (
src/order_gateway.h):- Simulates NewOrder, Ack, PartialFill, Fill, Cancel, Reject, and Replace lifecycle events
- Models exchange latency and deterministic queue position
- Latency Metrics (
src/latency_metrics.h):- Reports min, mean, p50, p95, p99, p999, and max for feed, queue, strategy-decision, order-send, and exchange-ACK latency
- Lock-Free Queue (
src/lock_free_queue.h):- Single-producer, single-consumer lock-free queue
- Batch operations for throughput
- Memory Pool (
src/memory_pool.h):- NUMA-aware, fast allocation for
MarketData
- NUMA-aware, fast allocation for
- Thread Affinity (
src/thread_affinity.cpp,src/thread_affinity.h):- Pin threads to CPU cores for cache/NUMA locality
- Logger (
src/logger.h):- Async spdlog-backed file/console logger
- Benchmarking (
src/benchmark.cpp):- Compares mutex queue, current SPSC queue, MoodyCamel MPSC, bounded MPSC, and allocation paths
- Types (
src/types.h):- Shared data structures (e.g.,
MarketData)
- Shared data structures (e.g.,
low-latency-cpp/
├── CMakeLists.txt
├── README.md
├── LICENSE
├── .gitignore
├── data/
│ └── mock_market_data.csv
├── docs/
│ └── concurrency.md
├── src/
│ ├── benchmark.cpp
│ ├── lock_free_queue.h
│ ├── logger.h
│ ├── main.cpp
│ ├── feature_engine.h
│ ├── market_data.cpp
│ ├── market_data.h
│ ├── memory_pool.h
│ ├── order_book.h
│ ├── order_gateway.h
│ ├── latency_metrics.h
│ ├── risk.h
│ ├── strategy.h
│ ├── thread_affinity.cpp
│ ├── thread_affinity.h
└───└── types.h
mkdir build && cd build
cmake ..
makeDependencies:
- C++20 compiler (e.g., GCC 13.3)
libnuma-devfor thread affinitylibspdlog-devfor async logging
./build/hft_system
./build/hft_system data/mock_market_data.csv max
./build/hft_system data/mock_market_data.csv fixed 100000
./build/hft_system data/mock_market_data.csv realtime
./build/hft_system data/mock_market_data.csv max 100000 verbose- Replays CSV market data through the SPSC queue
- Logs output to
hft_system.log verboseenables sampled gateway lifecycle logging without changing default hot-path behavior
./build/benchmark- Compares mutex queue, current SPSC ring, Boost SPSC if installed, vendored MoodyCamel MPSC, bounded MPSC ring, memory pool, and standard allocation
- Error Handling & Robustness:
- Use custom exceptions and RAII for resource management
- Ensure all resources are released on error
- Thread Management:
- Use RAII wrappers (e.g.,
std::jthreadin C++20) - Prefer condition variables or atomics over
sleep_forfor synchronization
- Use RAII wrappers (e.g.,
- Lock-Free Queue:
- Review atomic memory ordering
- Benchmark against established libraries (e.g., MoodyCamel’s ConcurrentQueue)
- Memory Pool:
- Profile NUMA benefits; consider
std::pmrfor flexibility
- Profile NUMA benefits; consider
- Logging:
- Consider lock-free or buffered logging for high-frequency events
- Allow runtime log level configuration
- Benchmarking:
- Add multi-threaded benchmarks
- Output results in machine-readable formats (CSV, JSON)
- Modern C++:
- Use
constexpr,noexcept,[[nodiscard]], and smart pointers - Use
std::spanfor batch operations (C++20)
- Use
- Documentation & Testing:
- Add unit/integration tests (e.g., Google Test)
- Expand API and threading documentation
- Build System:
- Add sanitizer options and CI integration