Blog / Engineering
Low Latency Networking
Engineering
The biggest bottleneck in current high-frequency trading isn't your algorithm's logic. It's the Operating System. It's Linux.
The Context Switch Problem
Standard Networking
- Packet arrives at NIC.
- NIC fires hardware interrupt to CPU.
- Kernel stops your app (Context Switch).
- Kernel copies packet to kernel buffer.
- Kernel copies packet to user buffer.
- Kernel wakes up your app (Context Switch).
Latency: ~5-10 microseconds
Kernel Bypass
- Packet arrives at NIC.
- NIC uses DMA (Direct Memory Access) to write directly into your app's memory space.
- Your app is already busy-polling that memory address.
- Your app sees the data immediately.
Latency: ~500 nanoseconds
Code Concept: Busy Polling
In HFT, we don't use select() or epoll() because those are system calls that put the thread to sleep. We burn 100% of a CPU core doing nothing but checking a memory address in a tight loop.
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
Layer 1: The Physics
Speed of light in glass (fiber) is ~2/3 speed of light in vacuum (refractive index ~1.5).
Speed of light in air (microwave) is ~99% speed of light in vacuum.
This is why HFT firms build microwave towers between Chicago (CME Futures) and New Jersey (NYSE Equities). The air path is faster than the fiber path, even if the fiber was perfectly straight (which it isn't).