# User Datagram Protocol (UDP)
## What is UDP
UDP, the User Datagram Protocol, is a connectionless, unreliable transport layer protocol that provides a simple datagram-based service. Unlike TCP, UDP does not establish a connection before sending data, does not guarantee delivery, does not ensure ordering of received datagrams, and does not provide flow control or congestion control. UDP simply takes data from the application, attaches a minimal header, and hands it to the network layer for transmission. Despite its lack of reliability features, UDP is widely used for applications where speed and low overhead are more important than guaranteed delivery.
## Why UDP is Used
UDP is faster than TCP because it has much less overhead. There is no connection setup, no acknowledgment mechanism, no retransmission, and no flow control. This makes UDP suitable for applications where any delay introduced by reliability mechanisms would be unacceptable. For real-time applications like voice calls and video conferencing, a slightly degraded call due to occasional lost packets is preferable to a call that freezes while TCP retransmits lost data. For DNS queries, the query and response are typically small enough to fit in a single datagram, and if the response does not arrive, the application simply sends the query again.
## UDP Datagram
A UDP datagram consists of an 8-byte header followed by the data payload. The header contains the source port number, destination port number, length of the UDP datagram including header and data, and a checksum. The checksum is optional in IPv4 but mandatory in IPv6. The checksum is calculated over the UDP header, the data, and a pseudo-header containing the source IP address, destination IP address, protocol number, and UDP length.
## Applications That Use UDP
DNS uses UDP for queries because the request-response exchange is small and fast retransmission is straightforward if no response arrives. DHCP uses UDP because it must operate before a host has an IP address and cannot establish a TCP connection. SNMP uses UDP for network management queries. NTP uses UDP for time synchronization. Streaming media applications often use UDP because they prefer to skip a missing frame rather than delay playback waiting for retransmission. Online gaming uses UDP to minimize latency. VoIP applications use UDP because real-time voice communication requires consistent low latency that TCP's retransmission mechanism would disrupt.
## UDP vs TCP Decision
The choice between UDP and TCP depends on the application's requirements. Use TCP when data must be delivered completely and in order, when data loss cannot be tolerated, and when the overhead of connection management is acceptable. Use UDP when speed and low latency are more important than guaranteed delivery, when the application can tolerate some packet loss, when the application implements its own reliability if needed, and when the communication is a simple request-response that does not justify TCP's connection overhead.Back to Subject