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:

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:

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

Disadvantages of UDP

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:

When to Use UDP

Use UDP when:

Don't use UDP when:

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.