r/algorithmictrading • • 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.

2 Upvotes

10 comments sorted by

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.

2

u/akm76 6d ago

He's talking about building a matching engine and (maybe?) a replay server(?).
Co-location is irrelevant, he's talking about how fast he can process incoming orders, match them and spit out trade records. Legit problem if you're building that system, or a super-realistic simulator, otherwise not directly applicable to trading.

1

u/cuby87 6d ago

Even collocated is 1-2ms on a 500$ machine..

1

u/No-Pay1929 6d ago

I think you meant 200 microseconds and not 200ms

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)

1

u/Federal_Tackle3053 5d ago

The overall proces like assessment+ order book execution+transferring money+ etc happens in days/weeks but the faster execution is better that's why