UDP (User Datagram Protocol) is a transport layer network protocol that provides a fast and efficient means of transmitting data. It doesn’t always provide a perfect transmission, but when time is of the essence, it’s typically the protocol of choice.
UDP Overview | User Datagram Protocol
UDP is a connectionless transport network protocol. As such, it doesn’t establish a “handshake” connection between the sender and receiver before transmitting data. Still, when combined with IP, the UDP/IP suite enables communication between two devices over a network.
UDP works by receiving data from the application layer and breaking it down into smaller packets. Each of these has a UDP header attached to it with information fields regarding things like the source and destination port as well as a checksum.
The source port identifies the application sending the data, and the destination port identifies the application on the receiving device. There’s also a length field that contains the total length of the UDP header plus the data or payload. Lastly, the checksum is optional and used for minimal error handling.
On the network layer, IP collects the packets and is responsible for addressing and routing them across the network to their destination.
From a technical standpoint, UDP is “unreliable” for several reasons, but mostly because it doesn’t guarantee packet delivery. Instead, it sends data without confirming if it arrived at the destination. This makes it fast, but imperfect.
In practice, UDP is suitable for applications that require low latency and can tolerate some degree of packet loss. It’s most commonly used for VoIP calls, live audio or video streaming, and online gaming. All of these activities require a steady data flow moving as quickly as possible.
UDP also supports multicasting, which is when data is transmitted to multiple receivers from a single source. This is possible because UDP enables packet switching. In this case, each packet uses a different route to get to the destination.
Advantages of UDP
UDP’s so-called unreliability is useful in certain scenarios because of the accompanying benefits or tradeoffs.
- Low latency: UDP is less prone to delays because it doesn’t need to establish a connection or receive an acknowledgment to start or continue transmitting data.
- Simplicity and efficiency: UDP has lightweight overhead because of its small header size, lack of handshake, and lack of extra features like congestion control—meaning it doesn’t require a lot of resources or memory for processing.
- Multicasting: UDP supports packet switching, where each packet is handled independently and uses a different route to arrive at its destination. This is useful in one-to-many communication broadcasts because any receiver can disconnect without affecting the others.
Disadvantages of UDP
UDP doesn’t have many features, so it comes with a few drawbacks.
- Packet loss: Since UDP doesn’t guarantee packet delivery, it doesn’t even know if they arrive at the destination. Therefore, if packets are lost or corrupt, UDP will not retransmit them.
- No sequencing: There are no sequence numbers in the UDP header. This means packets can have accidental duplicates and arrive out of order.
- No congestion control: Without congestion control, UDP will transmit data as fast as it can, regardless of the state of the network. In cases where the receiver can’t handle the incoming packets, it can become overwhelmed.
- Limited checksum: The UDP checksum is optional, meaning it can detect errors in individual packets, but it cannot request retransmission. Instead of fixing errors, corrupt packets are discarded, and the information is lost.
Although faster can be seen as better, there are some scenarios and applications where you wouldn’t want to use UDP—such as sending emails, for example. If you send an email with UDP, it’s possible that the recipient receives an incomplete message. Furthermore, since the packets have no sequence numbers to keep them in order, the message can arrive scrambled up, attachments can go missing, and certain parts of the email might be duplicated.
What If You Need Perfect Transmission?
UDP provides a best-effort service—meaning it sends the data and hopes for the best. If you want an error-free transmission, then TCP (Transmission Control Protocol) is the perfect alternative.
TCP is a connection-oriented transport protocol that provides so-called reliable data transfer from one application to another. It establishes connections via a three-step handshake between the sender and receiver before data can be transmitted.
Here’s how it works:
The sender requests for a connection by sending a SYN (synchronization) packet to the receiver. The receiver then responds with an ACK packet acknowledging the request and a SYN request of its own. Finally, the sender responds with an ACK to complete the connection.
During the transmission, TCP confirms the delivery of every data packet by getting an ACK from the receiver. If the sender doesn’t get an ACK, the lost packets are retransmitted. TCP also uses sequence numbers (which are contained in the SYN packets) so the receiver can reassemble packets in the correct order regardless of how they arrive. TCP can also discard any duplicate packets it comes across.
TCP also implements flow and congestion control, meaning it can throttle the transmission rate based on the state of the network or the capacity of the receiver. Not only can TCP slow down the transmission rate if the receiver becomes overwhelmed with packets, but it can also speed up the rate whenever necessary.
Unlike UDP, TCP’s use of a checksum is mandatory, and this ensures error-free packet delivery. This process starts when the sender calculates a checksum and adds it to the TCP header. Next, the receiver does the same calculation and checks if both values match. If it doesn’t match, the receiver discards the packet and requests for a retransmission.
Another key differentiator of TCP is that it provides encryption and authentication, making its transmissions more secure than those of UDP—which are prone to eavesdropping and unauthorized access.
Why not use TCP all the time?
TCP has more features than UDP, but it cannot be used for simple and real-time communication.
A DNS lookup, for example, is a server request that requires just two packets with UDP—send and response. Meanwhile, with TCP, this same transmission would involve 11 packets. (Three for the handshake between the client and server, four for the DNS request that includes ACKs in addition to the send and response packets, and four more to close the connection.)
Due to these heavier-weight transmissions, using TCP for real-time communication (like live streaming and VoIP calls) can cause unnatural delays and disruptions. Think of it this way: you wouldn’t restart a call every time there was a slight blip in the audio. Instead, you carry on with the conversation because live transmissions like that can handle some degree of packet loss.
TCP is also not ideal for multicasting because it has to connect with every receiver individually via its own handshake. Imagine approaching all 200 people in an auditorium and asking each person if they would like to hear the presentation you’re about to give. With UDP, you can just blast it through a microphone without looking like a crazy person.
UDP vs. TCP recap
TCP’s many features make it more reliable than UDP, but the major tradeoff is speed. As such, TCP is best suited for applications and activities where accuracy is the top priority. Examples of these include sending emails, file transfer, and web browsing.
Alternatively, if you want UDP’s speed and efficiency with a little more reliability, it is possible to add extra protocols in the application layer to handle things like security and sequencing—but that may not always be worth the trouble.
Instead, it’s usually best if you stick to using UDP in situations where packet loss is inconsequential, such as time-sensitive live communication.