Overview
TCP and UDP are the two core transport-layer protocols used to move data between hosts, but they trade reliability for speed in opposite directions. TCP prioritizes reliable delivery through handshakes, acknowledgments, and retransmission, while UDP prioritizes low-latency delivery by sending datagrams with no setup or delivery guarantees. Choosing between them shapes how an application handles packet loss, ordering, and throughput.
Comparison Diagram
Comparison Table
| Aspect | TCP | UDP |
|---|---|---|
| Connection setup | Three-way handshake (SYN, SYN-ACK, ACK) establishes a stateful connection before any data moves | No handshake — sender transmits datagrams immediately with no prior negotiation |
| Delivery guarantee | Guaranteed via sequence numbers and acknowledgments; lost segments are detected and resent | Best-effort only; lost packets vanish silently with no notification to either side |
| Ordering | Segments are reassembled in the original order regardless of arrival sequence | No ordering guarantee; packets are delivered to the application in whatever order they arrive |
| Flow & congestion control | Dynamic window sizing and congestion-avoidance algorithms throttle the sender to match network capacity | None; the application sends at whatever rate it chooses, independent of network conditions |
| Error handling | Checksum plus automatic retransmission recovers corrupted or missing segments | Checksum only; corrupted or missing packets are simply dropped, not recovered |
| Overhead & latency | Larger 20+ byte header and handshake/ACK round trips add processing and latency | Minimal 8-byte header and no round trips keep per-packet overhead and latency low |
| Connection teardown | Explicit four-way FIN/ACK exchange formally closes the connection on both sides | No connection state exists, so transmission simply stops with nothing to tear down |
Key Differences
- TCP is connection-oriented, requiring a handshake before data flows, while UDP is connectionless
- TCP guarantees reliable delivery through acknowledgments and retransmission; UDP offers none
- TCP performs congestion control to avoid overwhelming the network; UDP has no such mechanism
- UDP’s minimal header overhead gives it consistently lower latency than TCP
- TCP preserves packet ordering end-to-end; UDP delivers packets in whatever order they arrive
When to Use Each
TCP
- Web & API traffic (HTTP/HTTPS): Requests and responses must arrive complete and in order, making TCP’s reliability essential
- File transfer (FTP, SFTP): Every byte must arrive intact, so retransmission of lost segments is worth the added latency
- Database connections: Queries and results demand guaranteed, ordered delivery to keep transactions consistent
- Email transmission (SMTP): Message integrity matters more than speed, favoring TCP’s reliability guarantees
UDP
- DNS lookups: A single small request/response benefits from UDP’s low overhead, with the application retrying on its own if needed
- Live video & voice calls: A dropped frame is preferable to the added latency of waiting for retransmission
- Online multiplayer gaming: Stale position data is worthless, so UDP’s speed matters more than guaranteed delivery
- IoT sensor telemetry: High-frequency readings can tolerate occasional loss, and UDP’s low overhead suits constrained devices