What is User Datagram Protocol (UDP)?
User Datagram Protocol Definition
UDP is a network protocol that operates at the transport layer of the Internet Protocol (IP) suite. It's designed to provide a lightweight and low-overhead method for sending datagrams, or chunks of data, across a network.
UDP is a connectionless communication protocol. This means it doesn't establish a connection between the sender and receiver before data transmission. Instead, UDP treats each datagram as an independent unit, with no guarantee of delivery or order. UDP doesn't provide error checking, flow control, or data retransmission in case of packet loss, making it faster but less reliable.
One of the key advantages of UDP is its simplicity and efficiency. UDP has lower overhead and latency by eliminating the need to establish and maintain a connection compared to TCP. This makes it ideal for applications that require fast and real-time data transmission, such as online gaming, streaming media, and voice-over IP (VoIP) services.
UDP Key Characteristics
Connectionless Protocol: UDP has no connection setup or teardown like TCP's handshake. Data can be sent without communication, making UDP ideal for time-sensitive applications.
Stateless: Because it's connectionless, neither sender nor receiver maintains any state or connection-related information. The protocol doesn't maintain session information between client and server.
No Congestion Control: Unlike TCP, UDP does not have mechanisms to detect or avoid network congestion. It sends data regardless of network conditions, which can lead to packet loss if the network is overwhelmed.
No Flow Control: Flow control ensures that a sender doesn't overwhelm a receiver with too much data. Since UDP doesn’t have flow control, it doesn’t have mechanisms like TCP’s sliding window, which adjusts the data flow rate.
No Error Recovery or Sequencing: UDP provides no guarantees that packets will arrive, arrive in order, or arrive only once (there’s no deduplication mechanism). Packets may arrive out of order, duplicated, or not at all. This makes it faster but less reliable.
How User Datagram Protocol works?
Unlike TCP (Transmission Control Protocol), UDP does not establish a connection before sending data and does not guarantee delivery or order of packets. Here's how UDP works:
Data Packet Creation: To send data app creates a UDP packet with a header and payload. The header contains metadata like source and destination port numbers.
Transmission: The UDP packet is sent over the network without any handshaking or acknowledgment process. This means that the sender doesn't wait for a response or confirmation from the receiver.
Routing: Routers on the network forward the UDP packet towards its destination based on the destination IP address.
Reception: When the UDP packet reaches the destination host, the UDP protocol stack forwards it to the appropriate application based on the destination port number specified in the header.
Data Processing: The receiving application extracts the data from the UDP packet and processes it. Unlike TCP, UDP does not reassemble packets or ensure that they arrive in order. It's up to the program to handle packet loss, duplication, or out-of-order delivery if necessary.
No Connection Termination: UDP has not connection termination phase like TCP. Once the UDP packet is sent, the communication is considered complete.
UDP is often used for applications where speed and simplicity are more important than reliability, such as real-time multimedia streaming, online gaming, DNS (Domain Name System) queries, and network monitoring.
UDP Packet Structure
UDP packets are simple and lightweight. Each UDP datagram contains the following fields:
Source Port (16 bits): The port of the sending application.
Destination Port (16 bits): The port of the receiving application.
Length (16 bits): Specifies the total length of the UDP header and data.
Checksum (16 bits): Used to detect errors in the UDP header and data. However, this is optional in IPv4 (but mandatory in IPv6).
Data (variable length): The actual payload being transmitted.
Lack of reliability in UDP can also be a drawback in certain scenarios. Since it does not provide acknowledgments or retransmissions, any lost or corrupted packets may go unnoticed. This makes UDP less suitable for applications that require guaranteed delivery, such as file transfers or email.
UDP is often used with other communication protocols, such as IP and application layer protocols like Domain Name System (DNS) and Simple Network Management Protocol (SNMP). These protocols can utilize UDP's speed and low overhead for efficient data transfer.
What is the default port for UDP?
The default port for UDP (User Datagram Protocol) is 0. UDP is a connectionless protocol that does not require a dedicated port for communication. Unlike TCP (Transmission Control Protocol), which has well-known port numbers for specific services, UDP does not have a default port number. UDP packets are identified by the source and destination IP addresses and the source and destination port numbers specified within the packet's header. This flexibility allows UDP to be used for a wide range of apps and services. Developers can choose any port number for their UDP-based applications if another service or application hasn't already reserved it.
Multiplexing and Demultiplexing
UDP supports multiplexing and demultiplexing of data using port numbers. Each packet sent over UDP includes source and destination port numbers that help the operating system identify which application or service should receive the data. This allows multiple applications to use the network simultaneously without interference.
UDP in NAT and Firewalls
Network Address Translation (NAT) and firewalls often affect UDP traffic in different ways than TCP. For instance, NAT devices can map UDP traffic to different internal/external addresses and ports. Firewalls can block or restrict UDP traffic more aggressively because, without a connection state, it can be harder to secure.
UDP-Lite
There is a variant of UDP called UDP-Lite which relaxes the checksum feature. It allows partial checksum coverage for cases where applications can tolerate some level of corruption, such as certain types of media streams.
UDP is also used for tunneling protocols like VXLAN (Virtual Extensible LAN), which encapsulates Layer 2 frames in UDP packets for network virtualization.
RFC References
UDP is defined in RFC 768, which specifies its structure and behavior.
UDP-Lite is described in RFC 3828.
UDP Advantages and Use Cases
The main advantage of UDP is its low-latency and lightweight nature. Because it skips the overhead of connection establishment, error recovery, and flow control, it is preferred for applications where speed is critical, and minor packet loss is acceptable. Examples include:
DNS lookups DNS queries use UDP because they require very small packets, and the overhead of a TCP connection is unnecessary. If a DNS request is lost, the client can just re-request it.
Real-time Streaming Live video or audio streams use UDP because losing a few frames of video or a few milliseconds of audio is usually less important than maintaining low latency.
VoIP For voice calls over IP, UDP is often used because small delays can degrade call quality more than occasional packet loss.
Gaming In multiplayer games, UDP is used for transmitting real-time player position, actions, and other time-sensitive data. Dropping a few packets is better than waiting for retransmission and creating latency.
UDP vs other communication protocols
TCP vs UDP - Brief summary of key differences and use cases for both protocols
UDP in PubNub
PubNub primarily uses a combination of HTTP and HTTPS protocols for its messaging infrastructure. Provides developers with a scalable, secure, and feature-rich platform for building real-time applications. With our networking infrastructure, SDKs, and extensive library of tutorials, developers can focus on creating innovative and engaging user experiences. While, PubNub takes care of the underlying complexities of real-time communication you can focus on building apps that engage users.
Check out our Github or sign up for a free trial you’ll get up to 200 MAUs or 1M monthly transactions for free.