Networking / TCP vs UDP

TCP vs UDP

Send the same four packets down two lanes and watch what each protocol does about set-up, loss and ordering, and what those guarantees cost.

Beginner7 steps~6 min
Sender — has a message in four pieces
Receiver — hands what arrives to the application
Packet — one piece; the dark band is a TCP number
// THE SHORT VERSION

In 30 seconds

TCP and UDP are the two main ways programs send data across the internet. TCP opens a connection first, numbers every packet, waits for acknowledgements, resends what goes missing and delivers everything in order.

UDP does none of that. It sends each packet on its own and hopes. That makes it lighter and free of waiting, but packets can vanish or arrive shuffled.

So the web, email and downloads use TCP, while video calls, games and DNS lookups use UDP.

TCP vs UDP, step by step

STEPS01 / 07

Simplified: real TCP numbers bytes rather than packets, its ACKs name the next byte expected, and it also paces sending to avoid congestion. DNS switches to TCP when an answer is too large.

STEP 01 / 07 Two ways to send
THE MATH
TCP: connect, confirm, order UDP: just send
  1. Step 1: Two ways to send. A message is cut into four pieces, A to D. The network underneath (IP) may lose, duplicate or reorder packets. TCP is connection-oriented: it sets up a conversation and takes responsibility for delivery. UDP is connectionless: it adds an address label and sends each piece on its own.
  2. Step 2: TCP shakes hands first. Before any data moves, TCP exchanges three small messages so both sides agree to talk and learn each other’s starting sequence numbers. That costs one round trip. UDP has no such step: the first packet is already on its way.
  3. Step 3: TCP numbers and confirms. Every TCP packet carries a sequence number, and the receiver sends back acknowledgements (ACKs) saying what it has. The sender therefore knows what arrived. UDP packets carry no numbers and nothing comes back, so the sender never finds out.
  4. Step 4: A packet is lost. Packet C vanishes on the way. With TCP, no ACK for 3 comes back, so after a short wait the sender transmits it again and the receiver ends up with everything. With UDP nobody notices: the application simply gets A, B and D, with a hole in the middle.
  5. Step 5: Packets arrive out of order. Packets can take different routes, and C overtakes B. TCP uses the sequence numbers to put the pieces back in order before handing them to the application. UDP passes each packet up the moment it arrives, so the application sees A, C, B, D and must cope.
  6. Step 6: The price of the guarantees. TCP’s promises cost something. Each packet carries a header of at least 20 bytes against UDP’s 8, the handshake adds a round trip before data, and when a packet is lost everything behind it waits for the resend. UDP is lighter and never waits, because it promises nothing.
  7. Step 7: Which one for what. Use TCP when every byte must arrive and a brief wait is fine: web pages, email, downloads. Use UDP when fresh matters more than complete, as in video calls and games where a resent packet would arrive too late to be useful, or for one small question and answer such as a DNS lookup.
VALUES SO FAR
messageA B C Ddata
set-up····TCP
set-up····UDP
loss····TCP
loss····UDP
order····TCP
order····UDP
header····TCP
header····UDP

Spotted a mistake on this page?