10

I was told that for a High Frequency Trading (HFT) system that requires low-latency, TCP is used over UDP. I was told that with TCP you can make point to point connections, whereas you cannot with UDP, however from my understanding you can send UDP packets to specific IP/port.

There are several arguments used in this article as to why UDP > TCP for gaming but I can see relevance for HFT.

Why would TCP be a better protocol to use for HFT?

(Admins: My previous post of this question was silently removed with no explanation. If I am violating terms of use please alert me of this instead of silently removing the question)

n00b
  • 5,843
  • 11
  • 52
  • 82
  • 3
    I don't have any experience with the HFT field, but I suspect that TCP is used because it guarantees that the packet gets to it's destination, whereas UDP does not. – hafichuk Oct 31 '11 at 02:49
  • 3
    A UDP connection would be _faster_ but not as reliable. It would be bad if you tried to sell your plummeting stock but your UDP packet didn't get through. That's probably why most people use TCP rather than UDP for HFT. – Seth Carnegie Oct 31 '11 at 02:51
  • 1
    Here's a [post](https://www.corvil.com/kb/what-are-the-relative-merits-of-tcp-and-udp-in-high-frequency-trading) that talks to exactly this. – rbinnun Mar 17 '19 at 22:14

5 Answers5

17

UDP is superior to TCP if you don't need some of the features TCP provides. Every feature has a cost, and so if you don't need features, you are paying that cost for no reason.

In an HFT application, you need pretty much every feature TCP requires. So if you picked UDP, you'd have to implement those features yourself. That means you'd have to implement connection establishment, connection teardown, retransmissions, transmit pacing, windows, and so on.

If there was a way to do all those things that was better than the way TCP was doing it, TCP would be doing it that way. You'd have one hand tied behind your back because TCP is heavily optimized by some of the best minds on the planet and implemented in/with the kernel.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 2
    There are some funds which had their own optimised version of the kernel to reduce latency from network card to algo. – Suminda Sirinath S. Dharmasena Dec 03 '11 at 13:59
  • 6
    Not sure I agree. When a packet is lost, TCP will block the data flow to the application until successful retransmission. HFT data is like video - if you don't have a frame in time, forget it - it's too late - just get the next one. –  Jun 23 '12 at 21:25
  • @BlankXavier: You have to weigh that against all the other things that TCP does that you *do* need, such as transmit pacing, duplicate packet detection, connection setup and tear down, and so on. You also have to consider the relative maturity and reliability of something you cook up yourself versus TCP. And from a performance standpoint, every modern platform has a heavily-optimized TCP stack including things like hardware offload. It's hard to match that yourself. – David Schwartz Jun 23 '12 at 22:59
  • 4
    Those same platforms have heavily optimized UDP stacks, too and in these platforms there's a big return for the effort to write this code. Hell, these companies are implementing softwarei n FPGAs to get performance improvements. Implementing their own UDP transport wrapper is the right thing to do. –  Jun 24 '12 at 09:20
6

There's no reasons to expect a stream of data over an already-established TCP connection would be slower than the same data over UDP, plus you get checksumming, retries, and all the other TCP goodness. UDP mainly wins in cases where you can afford to discard the reliability or where the overhead of many TCP handshakes would be too expensive, such as with common DNS queries.

Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65
3

TCP is faster for when using a few connections, the important difference is that modern NICs perform significant amounts of acceleration on TCP and not really that much for UDP. This means there is more overhead to process each UDP packet and as such they cannot compete unless you need to send to multiple recipients simultaneously.

However the UDP multicast route still suffers the same problems as unicast UDP per datagram overheads. Therefore many HFT systems use hardware accelerated systems that can multiplex the streams across many NICs via TCP, example Solace.

These days though you want to completely bypass the kernel with say a userspace IP stack such as by Solarflare or Mellanox, or even skip both the kernel and IP stack with RDMA.

Steve-o
  • 12,678
  • 2
  • 41
  • 60
2

Quite simply, if you need connection reliability (ensuring that every byte of data transmitted is received), you should be using TCP regardless.

As you mentioned, UDP is more suitable for games, where 100% accurate real-time tracking of every object would use quite a large amount of bandwidth and is unnecessary (this is where slow connections encounter lag).

There is no special difference between a TCP port and a UDP port, beyond the type of connection being used (send the packet and forget it, UDP style, or negotiate a connection and sustain it, TCP style) and the service listening on the server side. e.g. TCP/25 would usually reveal a SMTP server, whereas UDP/25 would not.

septical
  • 430
  • 2
  • 8
1

Basically, modern TCP implementations are going to be just as fast as UDP, if you're keeping the connection alive. If TCP is having to resend a packet, you'd need to resend it in UDP too. Plus for UDP you're going to end up implementing the same reliability code (retransmission of dropped packets) that TCP has already implemented.

Jeremiah Gowdy
  • 5,476
  • 3
  • 21
  • 33
  • With UDP, you can send batches of messages in one datagram, the newest one + some number of previous ones, so you can actually handle the packets in real-time; if one datagram gets lost, you receive its "newest" message in the next datagram. In TCP, you don't receive the next segment (and the newest message) until the lost one is retransmitted, period. – tonso Apr 05 '23 at 12:26
  • @tonso Would you care to explain how, within the context of OP's question which is HFT, that losing messages is a viable strategy rather than doing UDP based retransmission similar to SIP over UDP? Period? – Jeremiah Gowdy Jun 07 '23 at 19:35