Introduction
Low-latency Java is common in financial institutions, I wanted to find a familiar system to bring together all the mechanically sympathetic patterns. So here we are building out a low-latency FIX engine. The common open source Java FIX engine is QuickFIX/J, however it’s far from low-latency, especially as its FIX message representation is predominantly map-based. Beyond that, vendor based FIX engines are often black boxes. Alternatively, specialist consultants come in to build the engine and then depart. The engines can be pervasive but it was odd that the outright knowledge or ownership of the code that was critical to the trading flow didn’t stay within the in-house engineering team. Seems like a good reason to just build one.
FIX Messages
The FIX message contents are held in the receive buffer that resides off-heap and there are three parallel arrays that hold:
- the tag,
- the value start position, and
- the value length.
Let’s consider reading tag 44, it starts with a linear scan of the tag array. The index of tag 44 in the tag array is retrieved and that index is looked up in the next two arrays. This gives the value start position and value length of tag 44. The value start position and value length are used to retrieve the value from the receive buffer. These three arrays are cache-friendly since array portions are loaded into a cache line, and the tag list is generally short (10-30 tags). The return type depends on the retrieval method called:
getValueBytes(int tag, byte[] dest, int off);
getValueAsInt(int tag);
For tag 44 (price) getValueBytes is used, then it’s up to the caller to convert the bytes into a price. As we’re talking about FIX tags, note that in this current implementation there’s no support for repeating groups. And the current implementation has a limit of 64 tag fields. This is fine for the current flow that’s being tested against, future implementations need to size this limit accordingly.
Getting to zero GC
Other techniques employed are:
- object pools: objects created up-front that can be re-used, removing object creation and garbage collection. Holding 16 FixMessage flyweights.
- flyweights: a view onto data stored elsewhere, removes the need for copying. These are FixMessages consisting of three parallel arrays for the tags, value start positions and value lengths.
- cache-friendly data structure: choosing data structures that are read sequentially. Favouring arrays over linked lists and hash-based structures.
- off-heap memory: this memory is not garbage collected, reducing the memory for garbage collection. These are the send buffer, the receive buffer and the outbound ring.
When using a receive buffer and keeping data in-place, it’s important to reset the flyweight fully. Otherwise it can reference stale fields because it’s not safe to assume that further messages will have the same number or more tags than the current message, and so overwrite every entry from the previous one. The flyweight had a partially stale state until a long enough message overwrote the stale fields. The fix was a reset on every code branch.
Performance
- encode performance: ~163 ns encode. Write the message into the send buffer.
- decode performance: ~171 ns decode. This covers:
- checking the start, end and the trailer checksum.
- then taking a FixMessage from the pool and building the tag index arrays.
- finally marking the message bytes as read and returning the FixMessage flyweight to the pool.
- 0 bytes allocated per order, supporting evidence for this:
- running on EpsilonGC, there was no OutOfMemoryError over a 5 minute run.
- the sending thread showed the getThreadAllocatedBytes() as 0 (the session thread is covered in part 2).
- JMH with -prof gc reports how much memory each operation allocates. Encode and decode operations each reported ~0.001 bytes allocated per operation - likely caused by JMH bookkeeping.
- Throughput: ~44K orders/second on OpenJDK 25 using ZGC, on a 2020 Intel MacBook Pro. An order is a round trip: a sent NewOrderSingle, that’s acknowledged and then filled.
Closing
At ~44K orders/second, each order costs ~23 microseconds. The encode/decode performance is in the order of nanoseconds, a few percent of that processing time. I suspect that the majority of the remaining time is spent in kernel syscalls and thread scheduling.
~44K orders/second is our starting point, the immediate next step is JVM selection and JVM optimisation and let’s see what that achieves.