r/algorithmictrading • u/Federal_Tackle3053 • 6d ago
Strategy Working in mid-frequency trading. What should I study to get a C++ matching engine below 1 µs?
I currently work in mid-frequency trading and have been exploring low-latency execution through a C++ limit order book I’m building. My first goal is to measure how long a single fill takes, from the moment the matching thread receives an order to the moment it updates the book and records the fill. I’m aiming for sub-microsecond latency on that path, with queueing and network time measured separately.
For people who’ve worked on matching engines or execution systems, I’d appreciate your take on a few things:
* **Benchmarking:** How would you design a replay that gives you a meaningful p99? I can benchmark single fills, cancels, and orders that sweep several price levels individually, but I’d also like to understand what happens when orders arrive in bursts. * **Implementation:** What would you profile first? i am considering pre-allocated order storage, an order-ID index for cancels, and keeping each book on a single writer thread. I’m particularly interested in cases where a change improved average latency but made the tail worse. * **What to study:** In what order would you tackle CPU caches and memory layout, Linux scheduling and CPU affinity, hardware performance counters, and eventually networking topics like DPDK?
If you were starting again with a C++ book and a Linux machine, **what’s the first experiment you’d run, and what result would guide your next step?** I’d also appreciate any talks, books, or open-source engines that helped you connect the theory to something you could measure.
3
u/CoughRock 6d ago
unless you are planning to co-locate your server in the exchange, there is about 200ms delay just in the network delay. So most of the time your code will finish crunching and just waiting on network.
1
u/Fun-Combination-Arna 6d ago
IMHO, you can't really know in advance where your bottlenecks or latency issues will be. Why not measure the entire path first, see where the problems actually are, and then decide what to optimize?
1
u/akm76 6d ago
You have to just start building it and learn as you go. You'd be surprised to know that even in-production matching solutions for actual "exchanges" are sometimes... crap (looking at you, cryptos). Like would you rely on websockets anywhere in your stack? Would you only use them to talk to clients you despise? Would you even rely on TCP and in which layers? How would you balance throughput with fail over remediation? The rabbit hole goes deep, beware. Great engineering challenge though.
1
u/Hornstinger 5d ago
If you are MFT, why do you need <1μs execution?
(Not being facetious, obviously the faster the execution the better but MFT is about days and weeks)