The main difference between TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) is that TCP prioritizes reliability, while UDP prioritizes speed and low latency.
Here's a side-by-side comparison:
| Feature | TCP | UDP |
|---|
| Connection | Connection-oriented (establishes a session first) | Connectionless (sends data immediately) |
| Reliability | Guarantees delivery | No guarantee of delivery |
| Packet ordering | Delivers packets in order | Packets may arrive out of order |
| Error checking | Detects errors and retransmits lost packets | Detects errors but doesn't retransmit |
| Speed | Slower due to acknowledgments and retransmissions | Faster because there is less overhead |
| Flow control | Yes | No |
| Congestion control | Yes | No |
How TCP works
TCP establishes a connection using a three-way handshake before sending data.
If a packet is lost:
- The receiver notices a missing packet.
- The sender retransmits it.
- Data is delivered in the correct order.
This makes TCP ideal when every byte of data matters.
Examples:
- Web browsing (HTTP/HTTPS)
- Email
- File transfers (FTP)
- Remote login (SSH)
How UDP works
UDP simply sends packets ("datagrams") without checking whether they arrive.
If packet #3 is lost, the receiver gets:
1 → 2 → 4 → 5
UDP does not stop and resend the missing packet. The application decides whether that's acceptable.
When would you choose UDP over TCP?
Choose UDP when speed and low latency are more important than perfect reliability.
Common examples include:
1. Live video streaming
A dropped frame is usually less noticeable than pausing the video to wait for retransmission.
Examples:
- Live sports
- Video conferencing
- IPTV
2. Voice over IP (VoIP)
In a phone call, it's better to lose a tiny bit of audio than to wait for missing packets.
For example:
- Missing 20 ms of speech is barely noticeable.
- Waiting 200 ms for retransmission creates an awkward delay.
3. Online gaming
Games need the latest player positions immediately.
If a player's position update is lost:
- The next update arrives milliseconds later.
- Resending the old position would actually make the game feel less responsive.
4. DNS lookups
When your computer asks:
"What's the IP address of example.com?"
The request is tiny.
If the response is lost, the client simply sends another request instead of maintaining a TCP connection.
5. IoT sensors
A temperature sensor sending readings every second can often tolerate the occasional missed update because a fresh reading is on the way almost immediately.
Why not always use UDP if it's faster?
Because many applications cannot tolerate missing or corrupted data.
Imagine downloading a PDF:
- Missing one packet could corrupt the file.
- TCP automatically detects the missing data and retransmits it.
- UDP would simply leave the file incomplete unless the application implemented its own recovery logic.
A simple analogy
- TCP is like sending a valuable package by certified mail. You get confirmation that it arrived, and if it's lost, it is sent again.
- UDP is like making announcements over a loudspeaker. The message is broadcast immediately, but there's no confirmation that everyone heard every word.
Rule of thumb
- Choose TCP when correctness and complete delivery are essential (web pages, downloads, emails, financial transactions).
- Choose UDP when timely delivery is more important than perfect delivery (live audio/video, online games, real-time sensor updates, DNS queries).
In practice, many real-time applications built on UDP add their own lightweight mechanisms—such as sequence numbers or selective retransmission—to recover from important losses without incurring all of TCP's latency and overhead.