User Datagram Protocol (UDP)
1. Overview and Core Philosophy
The User Datagram Protocol (UDP) (RFC 768) is a lightweight, connectionless Transport Layer protocol in the TCP/IP protocol suite.
While TCP provides guaranteed, highly regulated delivery, UDP is designed with a minimalist philosophy: it provides the absolute minimum transport service required—process-to-process multiplexing via port numbers and basic optional error detection—without adding connection handshakes, retransmissions, or flow throttling.
APPLICATION DATA STREAM
|
UDP ENCAPSULATION
|
+---------------------------------------------+
| UDP Header (8 Bytes) | Application Payload |
+---------------------------------------------+
|
IP PACKET ENCAPSULATION
|
[ IP Header (20B) | UDP Header (8B) | Application Payload ]
2. Key Characteristics of UDP
- Connectionless:
- Transmits data immediately without performing a handshake (zero round-trip setup delay).
- Unreliable (Best-Effort Delivery):
- Does not acknowledge receipt of packets (
No ACKs). - Packets may be dropped, duplicated, delayed, or arrive out of order. If reliability is needed, the application layer itself must implement sequence tracking.
- Does not acknowledge receipt of packets (
- No Flow Control or Congestion Control:
- The UDP sender transmits datagrams at whatever rate the application generates them. It never slows down in response to network congestion or receiver buffer exhaustion.
- Message-Oriented (Preserves Message Boundaries):
- Unlike TCP's byte-stream model, UDP packages each write call into an independent datagram. If an application writes 500 bytes, UDP sends a 500-byte datagram; the receiver reads exactly 500 bytes in a single read operation.
- Supports Multicast and Broadcast:
- Because UDP is connectionless, a single packet can be addressed to a broadcast or multicast group (TCP only supports 1-to-1 unicast connections).
3. UDP Datagram Format
The UDP header is exceptionally compact, consisting of only 8 bytes (64 bits) divided into four 16-bit fields:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port (16 bits) | Destination Port (16 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Length (16 bits) | Checksum (16 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| DATA PAYLOAD |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Field Breakdown:
- Source Port (16 bits): Port of the sending process on the client or server. Optional (set to
0if no reply is expected). - Destination Port (16 bits): Port of the target process on the destination machine (mandatory for demultiplexing).
- Length (16 bits): Total length of the UDP segment in bytes (Header + Data). Minimum value is
8(a datagram with zero payload bytes). - Checksum (16 bits): One's complement checksum covering the UDP header, UDP payload, and an imaginary IP Pseudo-Header. (Optional in IPv4, but mandatory in IPv6).
4. The UDP Pseudo-Header
To verify that the packet was not delivered to the wrong IP destination due to a corrupted IP header, UDP computes its checksum over an imaginary Pseudo-Header prepended to the segment during calculation:
+---------------------------------------------------------------+
| Source IP Address (32 bits) |
+---------------------------------------------------------------+
| Destination IP Address (32 bits) |
+-------------------------------+-------------------------------+
| All Zeros (8) | Protocol: 17 (8) |
+-------------------------------+-------------------------------+
| UDP Length (16 bits) |
+---------------------------------------------------------------+
- If the computed checksum does not match at the receiver, the damaged UDP datagram is silently discarded.
5. When is UDP Preferred over TCP?
| Application Category | Why UDP is Ideal | Examples |
|---|---|---|
| Real-Time Multimedia | Retransmitting a dropped audio/video frame late is useless; humans prefer minor glitching over freezing audio. | VoIP (SIP/RTP), Zoom, Live Gaming, Video Streaming |
| Simple Request-Response | Completes in a single round-trip without connection setup/teardown overhead. | DNS (Port 53), DHCP (Port 67), NTP (Port 123) |
| Network Management | Works even on unstable, highly congested links. | SNMP (Port 161), TFTP (Port 69) |
| Multicast / Broadcast | TCP cannot maintain synchronized connection states with multiple recipients. | IPTV, mDNS, Routing updates (RIP) |
6. Key Exam Questions & Takeaways
- Why is the UDP header size fixed at 8 bytes?
- Unlike IPv4 and TCP which support variable Options fields, UDP does not support options. It contains exactly four 2-byte fields (Source Port, Destination Port, Length, Checksum), making its header permanently fixed at 8 bytes.
- What happens if a UDP segment is corrupted in transit?
- The receiver verifies the 16-bit checksum. If corruption is detected, the datagram is silently discarded. UDP generates no retransmission request.
- Why is UDP considered "Message-Oriented" while TCP is "Stream-Oriented"?
- UDP preserves application message boundaries: each write by an application results in one standalone datagram on the wire. TCP treats data as a continuous, boundaryless stream of bytes that can be fragmented into arbitrary segment sizes.