TCP and UDP
π The Internet Protocol Suite (TCP/IP)
The Internet Protocol Suite, often called TCP/IP, is the foundational model for the internet.
- It includes more than just TCP and IP; it also encompasses UDP and other protocols.
- Protocols like TCP and UDP sit on top of the bare-bones Internet Protocol (IP) layer.
- This means the data packets for TCP or UDP are encapsulated within an IP packet for transmission over the network.
π¦ Transmission Control Protocol (TCP)
TCP is the most common transport layer protocol, known for being reliable and ordered, but it has more overhead and is generally slower.
Key Properties & Guarantees
- Ordered Data Delivery: TCP ensures that packets arriving out of sequence are reassembled into the correct order on the receiving end.
- Reliability: It is a reliable protocol. If a packet is lost during transmission, TCP handles the retransmission of lost packets, ensuring the complete data set arrives.
- Connection-Oriented: TCP establishes a dedicated, two-way connection between the two communicating machines before data transfer begins. This is done via a process called the three-way handshake.
Cost and Overhead
- Latency: The connection setup (three-way handshake) adds initial latency and overhead, making TCP slower than UDP.
- Expense: The mechanisms for reliability, ordering, and connection management require sending additional data (overhead), making it more βexpensiveβ on the network.
Common Application Protocols Built on TCP
Because of its reliability, TCP is used for most standard web and data transfer applications:
- HTTP/HTTPS (Web Browsing, API communication)
- SMTP (Email transfer)
- Web Sockets
- FTP (File transfer)
π User Datagram Protocol (UDP)
UDP is a connectionless protocol that is fast and has low overhead, but it offers no guarantees of delivery or ordering.
Key Properties & Characteristics
- Connectionless: UDP does not establish a connection (no handshake). The client just starts sending data.
- Unreliable: It is an unreliable protocol. Lost packets are not resent and are lost forever.
- Unordered: Data packets (datagrams) may arrive out of order and are not reordered by the protocol.
Benefits
- Speed: Much faster than TCP due to the lack of connection setup (handshake) and minimal overhead for reliability and ordering.
- Low Overhead: Less data needs to be sent for protocol management.
Use Cases (When Speed Trumps Reliability)
UDP is used in scenarios where real-time speed is critical and a minor loss of data is acceptable or preferred over delay:
- Live Streaming/Video Conferencing: A dropped frame is better than a 5-second delay to re-establish and retransmit the old data. The focus is on the live feed.
- Online Gaming: Minor lost or out-of-order packets are less detrimental than the latency caused by waiting for retransmission.
βοΈ Trade-offs for Software Developers
The choice between TCP and UDP is a classic engineering trade-off:
| Feature | TCP (Transmission Control Protocol) | UDP (User Datagram Protocol) |
|---|---|---|
| Reliability | Yes (Lost packets are resent) | No (Packets may be lost) |
| Ordering | Yes (Packets are reassembled in order) | No (Packets may arrive out of order) |
| Connection | Connection-Oriented (Requires three-way handshake) | Connectionless (No handshake required) |
| Speed/Overhead | Slower (High overhead) | Faster (Low overhead) |
| Best For | Web browsing, Email, File Transfer (Data integrity is essential) | Live Streaming, Gaming, DNS (Speed is essential) |
For most software development tasks, you will primarily be dealing with TCP-based protocols like HTTP, as reliability is essential for web applications, APIs, and data exchange.