TCP vs UDP
Reliable delivery vs raw speed
TCP and UDP are the two main transport layer protocols of the Internet. They represent a fundamental tradeoff between reliability and performance that every networked application must consider.
TCP
TCP (Transmission Control Protocol) provides reliable, ordered delivery of data. It establishes a connection via a three-way handshake, tracks every packet with sequence numbers, retransmits lost packets, and controls flow to prevent overwhelming the receiver. Every web page, email, file transfer, and API call you use relies on TCP. It guarantees your data arrives complete and in order.
UDP
UDP (User Datagram Protocol) sends packets without establishing a connection, tracking delivery, or guaranteeing order. It's a 'fire and forget' protocol — fast and lightweight but unreliable. Applications that use UDP (video calls, online games, DNS, live streaming) handle any needed reliability themselves. What you gain is minimal latency and no head-of-line blocking.
Key Differences
- **Reliability**: TCP guarantees delivery. UDP does not. - **Ordering**: TCP delivers data in order. UDP packets can arrive in any order. - **Connection**: TCP requires a handshake (setup overhead). UDP is connectionless. - **Speed**: UDP has less overhead and lower latency. - **Flow control**: TCP throttles to avoid congestion. UDP sends at whatever rate it wants. - **Header size**: TCP has 20+ bytes overhead. UDP has only 8 bytes.
When to Use Each
**Use TCP** for web traffic (HTTP/HTTPS), file transfers, email, database connections, APIs — anything where data must arrive complete and correct. **Use UDP** for real-time applications where speed matters more than perfection: video conferencing, online gaming, live streaming, VoIP, DNS lookups. A dropped video frame is better than a delayed one.
Analogy
**TCP** is like sending a registered letter — you get delivery confirmation, it's tracked, and if it gets lost, it's resent. Reliable but slower. **UDP** is like shouting across a room — fast and immediate, but if someone doesn't hear you, too bad. The message is already gone.