Every message that crosses the internet rides on one of two transport protocols. TCP makes sure your data arrives complete and in order, and pays for it in time. UDP just sends, and leaves the rest to the application. Which one an application uses decides how it behaves when the network is slow or drops packets.
This page compares the two side by side. For how TCP's handshake, acknowledgements and congestion control work inside, read the TCP deep dive.
The Difference in One Line#
TCP is a phone call: you connect first, everything you say arrives in order, and anything that drops out gets said again. UDP is a stack of postcards: each one goes out on its own, and some may arrive late, out of order or not at all.
Side by Side#
| TCP | UDP | |
|---|---|---|
| Connection | Connection-oriented: a three way handshake before any data | Connectionless: send straight away |
| Reliability | Lost data is detected and sent again | Lost data stays lost |
| Order | Bytes arrive in the order they were sent | Datagrams can arrive in any order |
| Data unit | A byte stream with no message boundaries | Separate datagrams; each recv gets one whole message |
| Flow and congestion control | Yes: slows down for a busy receiver or network | No: sends as fast as the application asks |
| Header size | 20 bytes, up to 60 with options | 8 bytes |
| Speed | Slower to start (one round trip to connect) and stalls on loss | No setup, no waiting for retransmissions |
| Broadcast and multicast | No, one connection is two endpoints | Yes |
| Used by | Web pages, SSH, email, file transfer, databases | DNS, DHCP, video calls, online games, QUIC (HTTP/3) |
What TCP Adds#
TCP builds four guarantees on top of IP, which on its own only makes a best effort:
- A connection. SYN, SYN-ACK, ACK: both sides agree on starting sequence numbers before any data moves. That costs one round trip.
- Reliability. Every byte has a sequence number, and the receiver acknowledges what it got. Anything not acknowledged in time is sent again.
- Order. The receiver puts segments back in sequence order before handing them to the application.
- Flow and congestion control. The receiver advertises how much it can buffer, and the sender backs off when the network shows signs of congestion.
The price is latency. If one segment is lost, everything after it waits in the receiver's buffer until the retransmission arrives, even data that got there fine. This is called head-of-line blocking.
What UDP Leaves Out#
UDP adds almost nothing to IP: port numbers, so the right application gets the datagram, and a checksum to catch corruption. Its whole header is four 2-byte fields:
| Field | Size | Purpose |
|---|---|---|
| Source port | 2 bytes | Where replies go |
| Destination port | 2 bytes | Which application receives it |
| Length | 2 bytes | Header plus data |
| Checksum | 2 bytes | Detects corrupted bytes (optional in IPv4, required in IPv6) |
A TCP header carries all of that and more: sequence and acknowledgement numbers, flags (SYN, ACK, FIN, RST), the window size, and options.
Leaving things out is the point. An application that would rather have new data than old data, or that adds its own recovery, gets nothing in its way.
When to Use Each#
Use TCP when every byte matters and a short delay is fine:
- Web pages and APIs over HTTP/1.1 and HTTP/2: a page with a missing chunk is broken
- SSH: a dropped keystroke would run the wrong command
- Email (SMTP, IMAP, POP3) and file transfer (FTP, SFTP)
- Database connections such as MySQL and PostgreSQL: a half-applied query is worse than a slow one
Use UDP when fresh beats complete, or the exchange is one small message:
- DNS lookups: a query and its answer usually fit in one packet each, so there is nothing to set up. If the reply doesn't come, the resolver just asks again
- Video and voice calls (WebRTC, as in Google Meet): a frame from half a second ago is useless, so resending it only adds lag
- Online games: the game sends the latest positions many times a second, and the next update replaces a lost one
- DHCP: a new device has no IP address yet, so it has to broadcast, which TCP can't do
- QUIC and HTTP/3: reliability rebuilt on top of UDP, per stream, so one lost packet doesn't stall every stream (Google and YouTube serve much of their traffic this way)
Common Interview Questions#
Why does DNS use UDP? Most lookups are one small question and one small answer. Over UDP that is a single round trip, with no handshake first and no connection state on a server handling millions of queries. DNS falls back to TCP when an answer is too big for one UDP packet, and for zone transfers between servers.
Why do games and video calls use UDP? Retransmitting stale data makes things worse. TCP would pause the whole stream to resend an old position or frame; over UDP the application simply uses the next one.
Can UDP be reliable? Yes, if the application adds it. QUIC adds sequence numbers, acknowledgements and retransmission on top of UDP, but tracks each stream separately, which avoids TCP's head-of-line blocking.
Is UDP always faster? It starts faster and never waits on a retransmission, but it doesn't slow down for a congested network either. Without its own congestion control, a UDP application can flood the network and lose more data.
In Code#
The two show up as different socket types. TCP needs a connection; UDP sends to an address directly:
With TCP, one recv can return part of a message or several messages joined together, so the application has to mark where each one ends. With UDP, each recvfrom returns exactly one datagram, or the timeout fires.
What to Remember#
- TCP is connection-oriented, reliable and ordered; UDP is connectionless and best-effort
- TCP's guarantees cost a handshake round trip, a 20 to 60 byte header, and stalls when packets are lost
- UDP's header is 8 bytes: two ports, a length and a checksum
- Use TCP when every byte matters (web, SSH, email, databases); use UDP when fresh data beats complete data (calls, games) or for small one-shot exchanges (DNS, DHCP)
- QUIC, which carries HTTP/3, rebuilds reliability on top of UDP to avoid head-of-line blocking