UDP Faster than TCP..WHY

Gokhan Kosem
2 min readJun 15, 2022

Major differences between UDP and TCP

Here’s a sort of generalized abstraction of how a TCP session works:

  • Client: Hello.
  • Server: Okay, Hello.
  • Client: Okay.
  • Client: Here is my question.
  • Server: Okay.
  • Server: Here is your answer
  • Client: Okay.
  • Client: Bye.
  • Server: Gotcha. Bye yourself.
  • Client: Okay.

Whereas a UDP transaction is more like:

  • Client: Here’s my question
  • Server: Here’s the answer

For short transactions, UDP wins by a mile. But if that “Here’s the answer” part is long (ie., many packets), there’s a problem if any parts are lost or delayed in flight:

  • Server: Here
  • Server: is
  • Server: Answer
  • Server: Your
  • Server: to
  • Server: Question

The client goes to put that back together, and it doesn’t make sense, and he’d have to try the whole thing over again. In TCP, it would go like this:

  • Server: 1. Here
  • Server: 2. is
  • Client: Okay, gotcha up to 2
  • Server: 4. Answer
  • Client: Wait, I only heard you up to 2
  • Server: My bad. 3: the
  • Client: Gotcha up to 3
  • Server: 4. Answer
  • Client: Still with you up through 4
  • Server: 6. Your
  • Server: 5. To
  • Client: Okay, thought we were in trouble there, but I figured it out. I’m cool up to 6
  • Server: 7. Question
  • Client: Okay. I understood up to 7.

Exactly how often the client is going to send back those acknowledgement messages depends on the TCP congestion algorithm he’s using and the negotiated window size; typically, as things go well, the client will need to acknowledge less and less often so that over time, the TCP overhead will become a small fraction of the total time, but the cost of patching things up after a single problem gets higher.

So for short transactions, or over a very reliable link, UDP is much faster. But TCP becomes faster for longer transactions where losing a packet in the middle would force you to start over again.

The problem of losing or misordering packets isn’t as big a deal for protocols that are very interactive-back-and-forth like VOIP or chat, but as most internet traffic follows the http model of “One person asks a short question, the other person gives a long answer”, TCP’s advantages became dominant. However, some newer protocols provide reliability in other ways (In particular, if you are doing encryption, you can combine the encryption step and the reliability step into a single step to save time) and thus can be done faster over UDP

WHERE IS TCP USED?

Everywhere on the Internet.

And some other places too. Supposedly there are some secure networks that are disconnected from the Internet that use TCP/IP.

WHERE IS UDP USED?

UDP is commonly used for applications that are “lossy” (can handle some packet loss), such as streaming audio and video. It is also used for query-response applications, such as DNS queries.

--

--