TLDR – The devil is in the details when optimizing critical components.
Can you write an order book algorithm that is optimized? First let’s tear apart the question:
An order book algorithm
The algorithm is certainly an important piece. A simplified view of the order book can mislead you into thinking the problem is an easy one to solve. But there are more questions when you dig in. Should the algorithm be optimized for matching? Perhaps the use case means the vast majority of transactions are updates. That necessitates a hard look at searching. Another use case could be a large spread with few participants and no facility to change an order once placed. The focus there would be in optimizing for insertions and deletions.
Optimized
We all understand the general term, but how optimized do you want it? We can tune it down to the microprocessor level, and even develop custom hardware, squeezing out the last few nanoseconds of speed. There is certainly a cost involved in development and testing. Add in the cost of refactoring when new hardware arrives and the costs may outweigh the benefits.
If we want our system to run on commodity hardware, we move back up to subjects such as implementation language and optimizations focusing on memory allocation and locality.
Specific use cases
There is an excellent video on YouTube from an engineer at ICE about what they did to develop a market engine. He mentions several times the balance between what is optimal for his use case and how his design would simply not work in all markets. Does that mean he was unsuccessful? Absolutely not. It sounds as if his solution was a great one that works for his target clients. Other requirements necessitate a different solution.
When speaking of optimization you will often hear warnings of “premature optimization.” And that is certainly a danger to project success. To lower project risk I often suggest an incremental approach. Get a minimally viable product (MVP) out there along with a testing framework. Get some benchmark numbers, find the hotspots, and iterate until the goals are met. Only when the goals are unachievable with typical tuning do you look for micro-optimizations or specialized hardware.
What is your use case?
As a typical software engineer, I am inquisitive and love a good challenge. Contact me about your project, or just put your war story in the comments below. Please note that comments are manually moderated, so may take some time to show.