Ecommet
Topic 13 of 75

TCP vs UDP

TCP gives you a reliable, ordered stream at the cost of handshakes and waiting for lost packets; UDP sends independent packets with no guarantees and no delay. Learn how each works and when to choose which.

Beginner 5 min read 4 quiz questions

Jump to the quiz

In a nutshell

  • TCP: connection-based, reliable, ordered, with flow and congestion control. Used by HTTP/1.1, HTTP/2, databases, email.
  • UDP: no connection, no guarantees, no ordering, very low overhead. Used by DNS, video calls, games, and QUIC (HTTP/3).
  • TCP waits for lost data, which adds delay; UDP lets the application decide what to do about loss.
  • Choose TCP when every byte must arrive; UDP when fresh data matters more than complete data.

Two ways to send data

Both TCP and UDP run on top of IP, which delivers individual packets between machines on a best-effort basis: packets can be lost, duplicated or arrive out of order. TCP and UDP take opposite approaches to that.

TCPUDP
ConnectionSet up with a handshake before data flowsNone: just send
DeliveryGuaranteed: lost packets are resentBest effort: lost packets are lost
OrderBytes arrive in the order sentPackets may arrive in any order
UnitA continuous stream of bytesSeparate messages (datagrams)
Speed controlFlow control and congestion control built inNone; the application must be polite
Header size20+ bytes8 bytes

The TCP handshake

Before any data flows, TCP spends one round trip agreeing on starting sequence numbers, which it uses to put bytes in order and detect what is missing.

Three-way handshake

Three-way handshake Client → Server: SYN (my sequence starts at x) Server → Client: SYN-ACK (got x; mine starts at y) Client → Server: ACK (got y) + first data ClientServerSYN (my sequence starts at x)SYN-ACK (got x; mine starts at y)ACK (got y) + first data

All steps SYN, SYN-ACK, ACK: one round trip before the first byte of data can be sent.

Diagram as text
  1. The client asks to open a connection and picks a starting sequence number.
  2. The server acknowledges it and picks its own starting number.
  3. The client acknowledges the server's number. The connection is open, and data can ride along with this packet.

After that, every chunk of data is acknowledged. If an acknowledgement does not arrive in time, the sender resends. The receiver holds back later data until the missing piece arrives, so the application always reads bytes in order.

The price of reliability

Waiting for a lost packet stalls everything behind it. This is head-of-line blocking. For a file download it does not matter: you need every byte anyway. For a video call it is harmful: by the time a resent piece of audio arrives, the moment has passed, and everything after it was delayed too.

One lost packet stalls the stream

One lost packet stalls the stream Packet 1 → Packet 2 Packet 2 → Packet 3: blocked Packet 3 → Packet 4 Packet 1deliveredPacket 2lost, resendingPacket 3waitingPacket 4waitingblocked
Packets 3 and 4 have arrived, but TCP holds them back until packet 2 is resent.
Diagram as text
  • Packet 1 → Packet 2
  • Packet 2 → Packet 3: blocked
  • Packet 3 → Packet 4

TCP also slows down when it sees loss (congestion control), which is good citizenship on a shared network but adds delay on poor connections.

When to use which

Use TCP for…Use UDP for…
Web pages and APIs over HTTP/1.1 and HTTP/2DNS lookups (small, a quick retry is cheaper than a handshake)
Database connectionsVoice and video calls, live streaming
File transfer, email, SSHMultiplayer games (the latest position matters, not old ones)
Anything where missing data is unacceptableProtocols that build their own reliability, such as QUIC

Common mistakes

  • Saying UDP is "faster" without qualification. It has no handshake and no waiting for resends, but the application must handle loss itself.
  • Using UDP for data that must arrive complete and then reinventing TCP badly.
  • Forgetting that many corporate firewalls block UDP, so UDP-based protocols need a TCP fallback.

Interview questions

Try answering out loud before you open each one.

Why do video calls use UDP?

Freshness matters more than completeness. With TCP, one lost packet delays everything behind it while it is resent, causing freezes. With UDP, a lost packet is simply skipped (maybe concealed by the codec) and the call keeps flowing with low latency.

Why does DNS mostly use UDP?

Queries and answers are small and fit in one packet. A UDP query avoids the TCP handshake round trip; if a packet is lost, the client simply asks again. DNS falls back to TCP for large responses and zone transfers.

Check yourself

1. Which guarantees that bytes arrive in order?
Show the answer

Answer: TCP. TCP numbers bytes and reorders them; UDP delivers datagrams as they come, if at all.

2. What is the TCP three-way handshake?
Show the answer

Answer: SYN, SYN-ACK, ACK. SYN, SYN-ACK, ACK sets up sequence numbers in one round trip.

3. Which application suits UDP best?
Show the answer

Answer: Live voice call. In a call, late audio is useless, so waiting for resends only hurts.

4. HTTP/3 runs on QUIC. What is QUIC built on?
Show the answer

Answer: UDP. QUIC is built on UDP and adds reliability and encryption itself.

Further reading

Your notes

Saved in this browser as you type.

Finished this topic?

Mark it read to track your progress, then carry on with the next one.

Next topic