A high-performance, production-grade Limit Order Book matching engine built in C++. Designed for sub-microsecond latency and suitable for quantitative trading systems, this project demonstrates core exchange mechanics with strict price-time priority, O(1) order cancellation, and memory-safe implementation.
- Price-Time Priority Matching: Strict FIFO execution within each price level
- Order Types: Market orders, limit orders, cancellations, and modifications
- O(1) Order Cancellation: Achieved via hash map + doubly-linked list design
- Ultra-Low Latency: 96.62 ns/order average latency, 10.35M ops/sec throughput
- Memory Safe: Valgrind-verified, zero leaks, RAII-compliant C++17 code
- Fully Tested: Comprehensive Google Test suite with edge-case coverage
- Production-Ready CI: Automated builds, tests, and memory checks via GitHub Actions
- Dockerized: Reproducible builds and benchmarks in containerized environment
Measured on local hardware with 1,000,000 orders after warm-up:
Total Time: 96.617 ms Average Latency: 96.62 ns/order Throughput: 10,350,141 ops/sec
Key Takeaways:
- Sub-100 nanosecond order insertion/matching latency
- Over 10 million operations per second
- Measured in release mode with
-O3optimizations - Benchmark includes random limit/market orders across multiple price levels
The engine uses a three-layer architecture optimized for speed:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β OrderBook (per symbol) β
β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β std::map<Price, PriceLevel> β β
β β - Automatically sorted by price β β
β β - O(log n) price level lookup β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β PriceLevel β β
β β - std::list<Order> (doubly-linked) β β
β β - FIFO order queue β β
β β - O(1) insertion at back β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β std::unordered_map<OrderID, list::iterator> β β
β β - O(1) order lookup by ID β β
β β - O(1) cancellation via iterator β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
| Component | Data Structure | Purpose | Complexity |
|---|---|---|---|
| Price Levels | std::map<Price, PriceLevel> |
Maintain sorted prices for best bid/ask | O(log n) insert/delete |
| Order Queue | std::list<Order> |
FIFO within price level, stable iterators | O(1) push_back |
| Order Lookup | std::unordered_map<ID, iterator> |
Fast cancellation without search | O(1) lookup/erase |
Trade-off: We accept O(log n) price insertion for automatic sorting and clean best bid/ask retrieval. In production, a custom tree or skip list could reduce this further.
- C++17 compiler (GCC 7+, Clang 6+, or MSVC 2017+)
- CMake 3.14+
- Google Test (fetched automatically by CMake)
- Docker (optional, for containerized builds)
git clone https://github.com/san4b0t/lob-engine.git
cd lob-enginemkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Releasecmake --build . -j$(nproc)ctest --output-on-failure./benchmarks/run_benchmarkscmake .. -DCMAKE_BUILD_TYPE=Debug -DENABLE_ASAN=ON
cmake --build .
./tests/order_book_testsdocker build -t lob-engine .docker run --rm lob-engine ctest --output-on-failuredocker run --rm lob-engine ./run_benchmarkscd build
./tests/order_book_testsvalgrind --leak-check=full --show-leak-kinds=all ./tests/order_book_tests- β Basic order insertion and cancellation
- β Price-time priority enforcement
- β Limit order crossing spread
- β Market order full/partial fills
- β FIFO behavior within price levels
- β Edge cases: empty book, invalid orders, duplicate IDs
- β Stress tests: 10,000+ interleaved orders
.
βββ src/
β βββ Order.cpp
β βββ PriceLevel.cpp
β βββ OrderBook.cpp
β βββ MatchingEngine.cpp
βββ include/
β βββ Order.h
β βββ PriceLevel.h
β βββ OrderBook.h
β βββ MatchingEngine.h
βββ tests/
β βββ order_book_tests.cpp
βββ benchmarks/
β βββ latency_benchmark.cpp
βββ docker/
β βββ Dockerfile
βββ .github/
β βββ workflows/
β βββ ci.yml
βββ CMakeLists.txt
βββ README.md
Every push triggers:
- β Build (Debug + Release modes)
- β Unit Tests (Google Test)
- β Memory Safety (Valgrind leak checks)
- β Sanitizers (AddressSanitizer in Debug builds)
See .github/workflows/ci.yml for full configuration.
This matching engine is suitable for:
- Quantitative trading system prototypes
- Exchange simulation and backtesting
- Low-latency order routing systems
- Stop Orders: Conditional order activation
- Multi-Symbol Support: Concurrent order books with sharding
- Lock-Free Design: Move to lock-free queues for multi-threading
- Persistence Layer: Crash recovery and snapshotting
Contributions welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure CI passes
- Submit a pull request
MIT License. See LICENSE for details.
Sanfo
π§ sanfo.bt@gmail.com
π LinkedIn
π» GitHub
- Inspired by production exchange architectures at major trading venues
- Built with guidance from low-latency systems design best practices
- Uses Google Test, CMake, and Docker for modern C++ workflows