What is UDP? User Datagram Protocol Explained
What is UDP?
UDP stands for User Datagram Protocol. It's one of the core protocols of the internet protocol suite, sitting alongside TCP in the transport layer. If TCP is about making sure every piece of data arrives intact, UDP is about speed. It just sends data and doesn't wait to confirm receipt.
That's the whole point. UDP sacrifices reliability for speed. No handshake, no error correction, no flow control. Just fire and forget.
How UDP Works
When an application sends data using UDP, it packages that data into a datagram and tosses it toward the destination. The protocol doesn't establish a connection first. There's no three-way handshake like TCP uses.
Here's what happens:
- Application creates a message
- UDP wraps it in a datagram with a header
- Packet gets sent to the destination IP
- UDP doesn't wait for acknowledgment
- It moves on to the next packet immediately
The receiving end doesn't send acknowledgments. The sending end doesn't know if the packet arrived. Lost packets stay lost. Corrupted packets get discarded silently.
UDP Header Structure
The UDP header is tiny. Just 8 bytes total. Compare that to TCP's minimum 20-byte header. Here's what those 8 bytes contain:
- Source Port (2 bytes) — Where the packet came from
- Destination Port (2 bytes) — Where it's going
- Length (2 bytes) — Total size of the datagram
- Checksum (2 bytes) — Basic error detection
That's it. No sequence numbers, no flags, no acknowledgment fields. Minimal overhead means less processing time and smaller packets.
UDP vs TCP: The Direct Comparison
| Feature | UDP | TCP |
|---|---|---|
| Connection | Connectionless | Connection-oriented |
| Handshake | None | Three-way handshake |
| Reliability | None | Guaranteed delivery |
| Ordering | Not guaranteed | Guaranteed in sequence |
| Speed | Fast | Slower |
| Header size | 8 bytes | 20+ bytes |
| Flow control | None | Yes |
| Congestion control | None | Yes |
TCP is like certified mail with return receipt. UDP is like throwing a letter over a fence and walking away.
Where UDP Shows Up in the Real World
UDP is everywhere, even if you don't notice it. These are the common use cases:
Video Streaming
Services like video calls, live TV, and gaming streams use UDP. A few dropped frames won't ruin your experience. Waiting for retransmission of a frame from 2 seconds ago would. Users prefer slightly glitchy video over video that freezes every time a packet gets lost.
DNS Queries
When you type a website address, your computer sends a DNS query. Most of these use UDP on port 53. The request is small, and a failed query just gets resent. Waiting for TCP handshake overhead would make web browsing feel sluggish.
Online Gaming
Multiplayer games send position updates dozens of times per second. If a packet drops, you want the next position update, not a retransmit of the old one. UDP keeps the game responsive even when the network hiccups.
VoIP (Voice over IP)
Phone calls over the internet use UDP. A momentary glitch is tolerable. The conversation stopping completely while packets retransmit is not. Quality degrades gracefully with UDP.
DHCP
Your router uses UDP to assign IP addresses to devices on your network. The protocol handles this assignment automatically without needing the overhead of a reliable connection.
Advantages of UDP
- Speed — No connection setup means lower latency
- Low overhead — Small headers leave more bandwidth for actual data
- Broadcast support — You can send to all devices on a network easily
- Simplicity — Easier to implement and requires less processing power
- No blocking — Packets don't wait for previous ones to be acknowledged
Disadvantages of UDP
- No delivery guarantee — Packets may never arrive
- No ordering — Packets may arrive out of sequence
- No congestion control — Can flood a network easily
- No protection against duplication — Same packet may arrive multiple times
- Firewall issues — Some networks block UDP entirely
UDP doesn't pretend to be something it's not. It's an unreliable, connectionless protocol by design. The application layer has to handle whatever UDP doesn't cover.
Getting Started with UDP Programming
Most languages have built-in support for UDP sockets. Here's a basic Python example:
Sender (client):
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('192.168.1.100', 12345)
message = b'Hello, server'
sock.sendto(message, server_address)
sock.close()
Receiver (server):
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('192.168.1.100', 12345)
sock.bind(server_address)
while True:
data, address = sock.recvfrom(4096)
print(f'Received: {data} from {address}')
Key differences from TCP in socket programming:
- Use
SOCK_DGRAMinstead ofSOCK_STREAM - No
listen()oraccept()calls needed - Use
sendto()andrecvfrom()instead ofsend()andrecv() - The server doesn't need to handle multiple client connections separately
When to Use UDP
Use UDP when:
- Speed matters more than reliability
- You can tolerate some data loss
- You're sending small, frequent messages
- You need broadcast or multicast capabilities
- The application handles its own error recovery
Don't use UDP when:
- Every single byte must arrive (databases, file transfers)
- You need guaranteed ordering
- You're dealing with sensitive data that can't be duplicated
The Bottom Line
UDP is not a lesser version of TCP. It's a different tool for a different job. The internet would crawl if every video call waited for packet acknowledgments. The protocol's simplicity is its strength, not a weakness.
If your application can handle lost or reordered packets gracefully, UDP will almost always be faster. If it can't, stick with TCP or build your own reliability layer on top of UDP.