TCP and UDP are two of the most important transport protocols used in computer networks and IoT systems. If your microcontroller communicates over WiFi, Ethernet or the internet, it will often use one of these protocols.
While both move data between devices, they behave very differently. Choosing the right one affects speed, reliability, latency and overall system behavior.
What Are TCP and UDP?
TCP and UDP are transport-layer communication protocols used on IP networks.
That means they sit above the network layer and handle how data is delivered between applications.
TCP:
- Transmission Control Protocol
- Connection-oriented
- Reliable delivery
UDP:
- User Datagram Protocol
- Connectionless
- Fast but less reliable
Both are used with IPv4 and IPv6 networks.
Why Transport Protocols Matter
Sending data over a network is not just about transmitting bits.
Important questions include:
- Did the packet arrive?
- Did packets arrive in the correct order?
- Should lost packets be retransmitted?
- How much delay is acceptable?
TCP and UDP solve these questions differently.
How TCP Works
TCP creates a connection between two devices before sending data.
Typical behavior:
- Connection setup (handshake)
- Data transmission
- Acknowledgment of received data
- Retransmission if packets are lost
- Order correction if packets arrive out of sequence
TCP focuses on reliable communication.
How UDP Works
UDP sends packets without creating a formal connection.
Typical behavior:
- Data is sent immediately
- No acknowledgment required
- No automatic retransmission
- No guarantee of packet order
UDP focuses on speed and low overhead.
TCP vs UDP Comparison
| Feature | TCP | UDP |
|---|---|---|
| Connection | Yes | No |
| Reliability | High | No guarantee |
| Packet ordering | Guaranteed | Not guaranteed |
| Retransmission | Automatic | None |
| Latency | Higher | Lower |
| Overhead | Higher | Lower |
TCP Example
Imagine downloading a firmware update.
Requirements:
- Every byte must arrive
- Data must be in the correct order
- Missing packets must be resent
TCP is ideal here because reliability matters more than speed.
UDP Example
Imagine sending live sensor broadcasts or game position updates.
Requirements:
- Fast delivery
- Low delay
- Old packets are less useful than new ones
UDP is ideal here because speed matters more than perfect delivery.
TCP in Embedded Systems
Microcontrollers often use TCP for:
- HTTP web requests
- MQTT connections
- Cloud communication
- Firmware downloads
- Web servers running on ESP32
TCP is common whenever reliable communication matters.
UDP in Embedded Systems
Microcontrollers often use UDP for:
- NTP time synchronization
- Device discovery
- Broadcast communication
- Fast sensor updates
- Custom lightweight protocols
UDP is common where low overhead matters.
TCP Handshake
TCP establishes communication before sending data.
Typical process:
- Client requests connection
- Server acknowledges
- Client confirms
This is commonly called the three-way handshake.
It adds overhead, but improves reliability.
Packet Loss
Packet loss affects TCP and UDP differently.
TCP:
- Detects missing packets
- Requests retransmission
UDP:
- Lost packets are simply gone
If your application cannot tolerate missing data, UDP may not be appropriate.
Latency
Latency is the delay between sending and receiving data.
TCP increases latency because of:
- Connection setup
- Acknowledgments
- Retransmissions
UDP minimizes latency by avoiding these mechanisms.
Broadcast and Multicast
UDP supports broadcasting and multicasting.
This means:
- One packet can reach multiple devices
This is useful for:
- Discovery protocols
- Status announcements
- Local device communication
TCP is point-to-point only.
Security
Neither TCP nor UDP is secure by itself.
Security depends on higher-level protocols:
- HTTPS over TCP
- TLS over TCP
- DTLS over UDP
Do not assume network transport alone provides security.
Common Mistakes
Typical design mistakes:
- Using UDP when reliable delivery is required
- Using TCP when ultra-low latency matters
- Ignoring packet loss behavior
- Assuming UDP is always faster in practice
- Ignoring firewall restrictions
When to Use TCP
TCP is a good choice when:
- Reliable delivery matters
- Data integrity is critical
- Packet order matters
- Cloud or server communication is required
When to Use UDP
UDP is a good choice when:
- Low latency matters
- Small packet loss is acceptable
- Broadcast communication is useful
- Lightweight messaging is needed
TCP vs UDP in IoT Practice
Typical examples:
| Application | Typical Protocol | Why |
|---|---|---|
| MQTT | TCP | Reliable messaging |
| HTTP web requests | TCP | Reliable transfer |
| NTP | UDP | Fast lightweight requests |
| Device discovery | UDP | Broadcast support |
Conclusion
TCP and UDP solve network communication in different ways. TCP prioritizes reliability, ordering and guaranteed delivery. UDP prioritizes speed, simplicity and low latency.
For IoT and embedded systems, both are useful. The right choice depends entirely on what your application needs.
