Module 1 · FundamentalsLesson 6 of 886 minBeginner
TCP vs UDP
TCP guarantees every byte arrives in order. UDP just sends and hopes, which makes it faster.
Play both tabs. In each one, packet 2 gets lost. Watch what TCP does about it, and what UDP does.
The internet moves data in small packets. Some packets get lost, delayed or arrive out of order. TCP and UDP are two ways of dealing with that.
TCP: reliable and ordered
Before sending data, TCP does a handshake so both sides are ready. Then:
- every packet gets a sequence number,
- the receiver sends back acknowledgements (ACKs),
- if an ACK doesn't arrive in time, the sender resends the packet,
- the receiver puts packets back in order before handing them to the app.
The app sees a perfect stream of bytes. The cost: the handshake adds a round trip, and one lost packet holds up everything behind it until it is resent.
HTTP, databases, email and file transfers all use TCP.
UDP: fast and simple
UDP sends each packet on its own. No handshake, no ACKs, no resends, no ordering. If a packet is lost, it's gone.
That sounds bad, but for live things it's what you want:
- Video calls and voice: a frame from half a second ago is useless.
- Online games: the newest position matters, not an old one.
- DNS lookups: one tiny question and answer; if it's lost, just ask again.
Apps on UDP add only the reliability they need themselves.
A modern twist
QUIC, which powers HTTP/3, runs on UDP but adds its own reliability and encryption. It avoids one lost packet blocking unrelated data, and it sets up connections faster. So the line between the two keeps moving, but the trade-off stays the same: guarantees cost time.