返回题库

系统 Tcp 与 Udp Market Data

Systems Tcp Vs Udp Market Data

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

题目详情

大多数行情数据源使用 UDP multicast 而非 TCP。此设计选择的首要原因是什么?

考虑两种协议在高频交易语境下的特性:TCP 提供可靠、有序和差错校验的字节流传输,但需要握手、确认和重传机制,引入可变延迟。UDP 提供无连接、无确认的数据报传输,延迟确定且开销极低。

任务:分析 TCP 的可靠性机制(握手、确认、重传、流控)对行情分发延迟的影响。解释高频交易为何优先选择 UDP 的确定性低延迟而非 TCP 的可靠性。

英文原题

Most market data feeds rely on UDP multicast rather than TCP. What is the primary reason for this design choice?
Consider the properties of each protocol in the context of high-frequency trading:
TCP: Transmission Control Protocol. Provides reliable, ordered, and error-checked delivery of a stream of bytes between applications running on hosts communicating via an IP network.
UDP: User Datagram Protocol. Provides a connectionless datagram service that prioritizes speed and efficiency over

解析

问题分析

Most market data feeds rely on UDP multicast rather than TCP. What is the primary reason for this design choice?
Consider the properties of each protocol in the context of high-frequency trading:
TCP: Transmission Control Protocol. Provides reliable, ordered, and error-checked delivery of a strea

解法

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

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

验证

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

复杂度与边界

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

英文解析

Analysis

Most market data feeds rely on UDP multicast rather than TCP. The primary reason is latency determinism. TCP provides reliability via retransmission, but this introduces variable latency - a lost packet triggers retransmission that can add 50-200ms delay. For market data, stale data is worse than missing data. A strategy operating on delayed (retransmitted) data makes incorrect decisions. UDP delivers data immediately; if a packet is lost, the next update supersedes it. Market data is inherently real-time - missing one update is preferable to waiting for it.

Solution

// TCP vs UDP for market data:
struct ProtocolComparison {
    double avg_latency_us;
    double max_latency_us;  // Worst case
    bool reliable;
    bool ordered;
};;
ProtocolComparison tcp = {100, 200000, true, true};   // Retransmission adds up to 200ms
ProtocolComparison udp = {50, 50, false, false};     // Deterministic, no retransmission
// Decision: UDP for market data (latency matters more than reliability)
//           TCP for order flow (reliability matters more than latency)

Complexity & Edge Cases

  • Time complexity: UDP O(1) per packet, TCP O(variable due to retransmission)
  • Space complexity: UDP O(1) no connection state, TCP O(connection state per subscriber)
  • Edge cases: (1) UDP requires application-level sequence checking for gap detection (2) TCP congestion control throttles during high-volume periods (3) UDP multicast enables one-to-many delivery efficiently

Verification

Simulate packet loss on TCP vs UDP market data feeds. Measure latency impact: TCP adds 50-200ms on retransmission, UDP continues with next update. Verify that UDP provides consistent latency even under packet loss.

Key Considerations

UDP multicast is the universal standard for exchange market data feeds because retransmission is counterproductive for time-sensitive data. The key insight: in market data, data has a short useful lifetime (milliseconds). Retransmitting a stale update wastes bandwidth and introduces dangerous latency. The correct approach is sequence-number tracking: detect gaps, discard stale retransmissions, and request a snapshot only when the gap exceeds a configurable threshold.