speice.io/_drafts/writing-fast-code.md
Bradlee Speice 3814544256 Finish post body
Still needs a conclusion, but that's all the details that I wanted to cover
2019-06-05 15:02:48 -04:00

15 KiB

layout title description category tags
post On Building High Performance Systems

Prior to working in the trading industry, my assumption was that High Frequency Trading (HFT) is made up of people who have access to secret techniques mortal developers could only dream of. There had to be some secret art that could only be learned if one had an appropriately tragic backstory:

kung-fu fight > How I assumed HFT people learn their secret techniques

How else do you explain people working on systems that complete the round trip of market data in to orders out (a.k.a. tick-to-trade) consistently within 750-800 nanoseconds? In roughly the time it takes other computers to access main memory 8 times, trading systems are capable of reading the market data packets, deciding what orders to send, (presumably) doing risk checks, creating new packets for exchange-specific protocols, and putting those packets on the wire.

Having now worked in the trading industry, I can confirm the developers are mortal; I've made some simple mistakes at the very least. But more to the point, what shows up from reading public discussions is that philosophy, not technique, separates high-performance systems from everything else. Performance-critical systems don't rely on C++ optimization tricks to make code fast (though they can be useful); there's a lot more to worry about than just the code written for the project. Rather, what shows up time and again is a focus on variance, and reducing the gap between the fastest and slowest runs of the same code.

Don't get me wrong, I'm a much happier person when things are fast. Booting my computer in 10 seconds using SSD's, rather than 20 seconds with spinning plates? Awesome. But if every other day it takes a full 30 seconds to boot up because my computer is feeling temperamental? Not so great. When it comes to code, speeding up a function by an average 10 milliseconds doesn't mean much if there's a 1000ms difference between your fastest and slowest runs; you simply won't know until you call the function how long it takes to complete. High-performance systems should first optimize for time variance. Once you're consistent at the time scale you care about, then focus on improving overall time.

This focus on variance shows up all the time in public discussions (emphasis added in all quotes below):

  • In marketing materials for NASDAQ's matching engine, they specifically call out variance:

    Able to consistently sustain an order rate of over 100,000 orders per second at sub-40 microsecond average latency

  • The Aeron message bus has this to say about performance:

    Performance is the key focus. Aeron is designed to be the highest throughput with the lowest and most predictable latency possible of any messaging system

  • The company PolySync, which is working on autonomous vehicles, mentions why they picked their specific messaging format:

    In general, high performance is almost always desirable for serialization. But in the world of autonomous vehicles, steady timing performance is even more important than peak throughput. This is because safe operation is sensitive to timing outliers. Nobody wants the system that decides when to slam on the brakes to occasionally take 100 times longer than usual to encode its commands.

  • Solarflare, which makes highly-specialized network hardware, points out variance as a big concern for electronic trading:

    The high stakes world of electronic trading, investment banks, market makers, hedge funds and exchanges demand the lowest possible latency and jitter while utilizing the highest bandwidth and return on their investment.

So how exactly does one go about looking for and eliminating performance variance? To tell the truth, I don't think a systematic answer or flow-chart exists. There's no substitute for (A) building a deep understanding of the entire technology stack, and (B) actually measuring system performance (though (C) watching a lot of CppCon videos never hurt). Even then, each project cares about performance to a different degree; you may need to build an entire replica production system to accurately benchmark at nanosecond precision. Alternately, you may be content to simply avoid garbage collection in your Java code.

Even though everyone has different needs, there are still common things to look for when trying to isolate variance. In no particular order, these are places to focus on when building high-performance/low-latency systems:

Language-specific

Garbage Collection: How often does garbage collection happen? What are the impacts?

  • In Python, individual objects are collected if the reference count reaches 0, and each generation is collected if num_alloc - num_dealloc > gc_threshold whenever an allocation happens. The GIL is acquired for the duration of generational collection.
  • Java has many different collection algorithms to choose from, each with different characteristics. The default algorithms (Parallel GC in Java 8, G1 in Java 9) freeze the JVM while collecting, while more recent algorithms (ZGC and Shenandoah) are designed to keep "stop the world" to a minimum by doing collection work in parallel.

Allocation: Every language has a different way of interacting with "heap" memory, but the principle is the same; figuring out what chunks of memory are available to give to a program is complex. Allocation libraries have a great deal of sophisticated strategies to deal with this, but it's unknown how long it may take to find available space. Understanding when your language interacts with the allocator is crucial (and I wrote a guide for Rust).

Data Layout: How your data is arranged in memory matters; data-oriented design and cache locality can have huge impacts on performance. The C family of languages (C, value types in C#, C++) and Rust all have guarantees about the shape every object takes in memory that others (like Java and Python) can't make. Cachegrind and kernel perf counters are both great for understanding how performance relates to memory layout.

Just-In-Time Compilation: Languages that are compiled on the fly (LuaJIT, C#, Java, PyPy) are great because they optimize your program for how it's actually being used. However, there's a variance cost associated with this; the virtual machine may stop executing while it waits for translation from VM bytecode to native code. As a remedy, some languages now support ahead-of-time compilation (CoreRT in C# and GraalVM in Java). On the other hand, LLVM supports Profile Guided Optimization, which should bring JIT-like benefits to non-JIT languages. Benchmarking is incredibly important here.

Programming Tricks: These won't make or break performance, but can be useful in specific circumstances. For example, C++ can use templates instead of branches.

Kernel

Code you wrote is almost certainly not the only code running on your system. There are many ways the operating system interacts with your program, from system calls to memory allocation, that are important to watch for.

Scheduling: Set the processor affinity of your program, and make sure only your program can run on that processor. The kernel, by default, is free to schedule any process on any core, so it's important to reserve CPU cores exclusively for the important programs. Also, turning on NO_HZ and turning off hyper-threading are probably good ideas.

System calls: Reading from a UNIX socket? Writing to a file? In addition to not knowing how long the I/O operation takes, these all trigger expensive system calls (syscalls). To handle these, the CPU must context switch to the kernel, let the kernel operation complete, then context switch back to your program. We'd rather keep these to a minimum. Strace is your friend for understanding when and where syscalls happen.

Signal Handling: Far less likely to be an issue, but signals do trigger a context switch if your code has a handler registered. This will be highly dependent on the application, but you can block signals if it's an issue.

Interrupts: System interrupts are how devices connected to your computer notify the CPU that something has happened. It's then up to the CPU to pause whatever program is running so the operating system can handle the interrupt. We don't want our program to be the one paused, so make sure that SMP affinity is set and the interrupts are handled on a CPU core not running the program we care about.

NUMA: While NUMA is good at making multi-cell systems transparent, there are variance implications; if the kernel moves a process across nodes, future memory accesses must wait for the controller on the original node. Use numactl to handle memory/cpu pinning.

Hardware

CPU Pipelining/Speculation: Speculative execution in modern processors gave us vulnerabilities like Spectre, but it also gave us performance improvements like branch prediction. However, there's variance involved because the CPU might mis-predict. And while the compiler knows a lot about how your CPU pipelines instructions, code can be structured to help the branch predictor.

Paging: For most systems, virtual memory is incredible. Applications live in their own worlds, and the CPU/MMU figures out the details afterward. However, there's a variance penalty associated with memory paging and caching; if you access more memory pages than the TLB can store, you'll have to wait for the page walk. Kernel perf tools are necessary to figure out if this is an issue, but techniques like huge pages can reduce TLB burdens. Alternately, running applications in a hypervisor like Jailhouse allows one to skip virtual memory entirely, but this is potentially more work than the benefits are worth.

Network Interfaces: When more than one computer is involved, variance can go up dramatically. Tuning kernel network parameters may be helpful, but modern systems more frequently opt to skip the kernel altogether with a technique called kernel bypass. This typically requires specialized hardware and custom drivers, but even industries like telecom are finding the benefits.

Networks

Routing: There's a reason financial firms are willing to pay millions of euros for rights to land and cell towers - having a straight-line connection from point A to point B means the path their data takes is incredibly simple. In contrast, there are currently 6 computers in between me and Google, but that may change at any moment that my ISP decides a more efficient route is available. Whether it's using research-quality equipment or just making sure there's no data inadvertently going between data centers, routing matters.

Protocol: TCP as a network protocol is awesome: guaranteed and in-order delivery, flow control, and congestion control all built in. But these attributes make the most sense when networking infrastructure is reasonably lossy. For systems that expect nearly all packets to be delivered correctly, the setup handshaking and packet acknowledgment are just overhead. Using UDP (unicast or multicast) may make sense in these contexts as it avoids the chatter needed to track connection state, and gap-fill strategies can handle the rest.

Switching: Many routers/switches handle packets by waiting for the whole packet, validating checksums, and then sending to the next device. This behavior is referred to as "store-and-forward." In variance terms, the time needed to move data between two nodes is proportional to the size of that data; the switch must "store" all data before it can calculate checksums and "forward" to the next node. With cut-through designs, switches will begin forwarding data as soon as they know where the destination is, checksums be damned. For communications within a datacenter, this can have a huge benefit.

Miscellaneous

  • Do you know where you care about latency? If any humans are involved, none of these tools make a difference, the humans are already too slow
  • If you benchmark, are you benchmarking in a way that's actually helpful? All the same variance rules from above apply to your benchmarks