返回题库

系统 Nagles Algorithm Latency

Systems Nagles Algorithm Latency

专题
Systems & Architecture / 系统与架构
难度
L2
来源
MyntBit

题目详情

高频交易系统通过 TCP 与交易所通信。Nagle 算法在大多数系统上默认启用。为什么交易系统必须通过设置 TCP_NODELAY 标志禁用 Nagle 算法?

任务:Nagle 算法将小包缓冲直到积累足够数据或收到 ACK 才发送——这引入 40-200ms 延迟。高频交易订单通常很小(几十字节),Nagle 会延迟订单发送。TCP_NODELAY 禁用 Nagle,每个订单立即发送。代价是更多小包和轻微带宽浪费。

英文原题

A high-frequency trading system interacts with an exchange via TCP. Nagle's algorithm is enabled by default on most systems. Why must trading systems disable Nagle's algorithm by setting the TCP_NODELAY flag?

解析

问题分析

A high-frequency trading system interacts with an exchange via TCP. Nagle's algorithm is enabled by default on most systems. Why must trading systems disable Nagle's algorithm by setting the TCP_NODELAY flag?

解法

根据题目要求实现相应功能。核心逻辑需要:

// 核心数据结构和方法——根据题目 API 约定实现
// 1. 确定状态表示——选择支持所需操作的数据结构
// 2. 实现核心算法——确保 O(·) 时间复杂度和正确性
// 3. 处理边界条件——空输入、极值参数、并发访问

验证

用具体输入验证:构造已知输入的测试用例,确认输出匹配预期结果。

复杂度与边界

  • 时间复杂度:取决于选用的算法
  • 空间复杂度:取决于数据规模
  • 关键边界条件:空输入、极值参数、并发场景下的正确性保证

英文解析

Analysis

A high-frequency trading system interacts with an exchange via TCP. Nagle's algorithm is enabled by default on most systems. Trading systems must disable Nagle's algorithm by setting TCP_NODELAY because Nagle buffers small packets until an ACK arrives or the buffer fills, adding up to 200ms of latency for order messages. A typical FIX order message is ~200 bytes - well below the MSS threshold. Without TCP_NODELAY, Nagle would delay sending this order until receiving an ACK for the previous packet, adding unacceptable latency to order submission.

Solution

int createOrderSocket(const char* host, int port) {
    int sock = ::socket(AF_INET, SOCK_STREAM, 0);
    int one = 1;
    ::setsockopt(sock, SOL_TCP, TCP_NODELAY, &one, sizeof(one));  // Disable Nagle
    // Also increase send buffer for burst order submission
    int sndbuf = 256 * 1024;
    ::setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf));
    struct sockaddr_in addr;
    addr.sin_family = AF_INET; addr.sin_port = htons(port);
    inet_pton(AF_INET, host, &addr.sin_addr);
    ::connect(sock, (struct sockaddr*)&addr, sizeof(addr));
    return sock;
}

Complexity & Edge Cases

  • Time complexity: Configuration O(1) at connection setup
  • Space complexity: O(1)
  • Edge cases: (1) Disabling Nagle increases small-packet count on the network (2) Must be set on BOTH sides (client and exchange gateway) (3) TCP_QUICKACK on receive side complements TCP_NODELAY on send side

Verification

Send small order messages with and without TCP_NODELAY. Measure round-trip time. Verify Nagle adds 40-200ms latency for small packets. Confirm TCP_NODELAY eliminates this delay.

Key Considerations

TCP_NODELAY is the single most important socket option for trading systems. Every production FIX/order gateway connection must have TCP_NODELAY enabled. The latency impact is dramatic: without it, a 200-byte order message may wait up to 200ms for an ACK before transmission. With TCP_NODELAY, the order is sent immediately, achieving sub-millisecond submission latency.