getcracked
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

  1. Packet arrives at NIC.
  2. NIC fires hardware interrupt to CPU.
  3. Kernel stops your app (Context Switch).
  4. Kernel copies packet to kernel buffer.
  5. Kernel copies packet to user buffer.
  6. Kernel wakes up your app (Context Switch).

Latency: ~5-10 microseconds

Kernel Bypass

  1. Packet arrives at NIC.
  2. NIC uses DMA (Direct Memory Access) to write directly into your app's memory space.
  3. Your app is already busy-polling that memory address.
  4. 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
// Conceptual C++ for Kernel Bypass Polling
2
// Using a library like Solarflare ef_vi or DPDK
3

4
while (keep_running) {
5
    // Check if new data has arrived in the ring buffer
6
    // No system calls. Just reading formatting memory.
7
    if (ring_buffer->has_data()) {
8
        
9
        Packet* pkt = ring_buffer->pop();
10
        
11
        // Process packet immediately
12
        process_market_data(pkt);
13
        
14
        // Send order immediately
15
        // Write directly to TX ring buffer
16
        send_order(pkt->symbol);
17
    }
18
    
19
    // Do NOT sleep.
20
    // Do NOT yield.
21
    // Burn the CPU.
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).