Octet

What is the difference between TCP and UDP, and when would you pick each one?

Show the answer

Say this first

TCP gives the application a connection and a reliable, ordered byte stream, with flow control and congestion control. It pays for that with a handshake and with waiting for retransmissions. UDP adds only ports, a length and a checksum to IP: no connection, no retransmission, no ordering, and it keeps message boundaries. I pick TCP when every byte must arrive in order (web, SSH, BGP, databases, file transfer) and UDP when late data is useless or the application wants its own reliability (voice and video over RTP, DNS queries, DHCP, NTP, QUIC).

Go deeper

Side by side

TCPUDP
SetupThree-way handshakeNone
DeliveryReliable, in order, duplicates removedBest effort, may be lost, duplicated or reordered
UnitByte stream (no message boundaries)Datagram (one send, one receive)
Flow controlReceive windowNone
Congestion controlYes (CUBIC, BBR and others)None, the application must behave
Header20 to 60 bytes8 bytes
Multicast and broadcastNoYes
IP protocol number617

How I decide

  • Does late data still have value? A retransmitted voice sample arrives after the jitter buffer has played the gap. For real-time media, a lost packet is better than a late one, so RTP runs on UDP.
  • Is the exchange one small question and answer? DNS, NTP and SNMP fit in one datagram each way. A handshake would double the latency for no gain. DHCP has another reason: the client has no IP address yet and must broadcast.
  • Do I need one-to-many? Multicast and broadcast only work with UDP.
  • Do I want reliability but not TCP's behaviour? QUIC builds streams, retransmission and congestion control on top of UDP in user space, so it can evolve without kernel or middlebox changes.
  • Everything else that must be complete and correct goes on TCP.

Traps

  • Saying UDP is "worse" because it is unreliable. It is a different tool: lower latency, no state, multicast.
  • Saying TCP guarantees delivery. It guarantees that the application gets the data in order or gets an error.