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.
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
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 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Try it yourself
Learn this first
Learn next
- TCP three-way handshakeSYN, SYN-ACK, ACK — how two machines agree to talk. Beginner
- WebRTCHow two browsers find each other and stream video straight across, encrypted. Intermediate
- Latency vs bandwidth vs throughputThree numbers that describe a connection, and why a fast one can still feel slow. Beginner
Spotted a mistake on this page?