UDP is the internet’s surprise party guest: it shows up unannounced, delivers its message, and leaves without waiting for a "thank you," which is precisely why it’s so fast.

Let’s watch UDP in action with a simple Python example. Imagine a server listening for UDP packets on port 12345:

import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 12345

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

print(f"UDP server listening on {UDP_IP}:{UDP_PORT}")

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print(f"Received message: {data.decode()} from {addr}")
    # No response sent back, just received and printed

And a client sending a message:

import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 12345
MESSAGE = b"Hello, UDP!"

print(f"UDP target IP: {UDP_IP}")
print(f"UDP target port: {UDP_PORT}")
print(f"message: {MESSAGE}")

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))

When you run the server and then the client, you’ll see the server output Received message: Hello, UDP! from ('127.0.0.1', <client_port>). Notice how the server doesn’t acknowledge receipt, and the client doesn’t wait for one. It’s a one-way street.

UDP solves the problem of needing to send data quickly without the overhead of establishing and maintaining a connection. Think of sending a postcard versus making a phone call. The postcard (UDP) is quick to send, but there’s no guarantee it arrives or that the recipient is even there. The phone call (TCP) requires dialing, waiting for an answer, and confirming the conversation, which is slower but reliable.

Internally, UDP is incredibly simple. It takes your data, wraps it in a small header containing source and destination ports, length, and a checksum, and slaps it onto the network. There’s no handshake, no sequence numbers, no acknowledgments, and no retransmissions. This lack of features is its superpower for speed. The UDP header is only 8 bytes, compared to TCP’s 20-60 bytes.

The key levers you control with UDP are the source and destination ports. The source port is usually assigned dynamically by the operating system for outgoing packets, while the destination port is what your application listens on. The "127.0.0.1" is the loopback address, meaning the client and server are on the same machine. For communication across a network, you’d replace this with the actual IP address of the server.

The UDP checksum is optional in IPv4 (though usually enabled) and mandatory in IPv6. It’s a simple calculation to detect accidental corruption of the data during transmission. If a packet arrives with a bad checksum, the operating system will typically discard it without notifying the sender or receiver. This is a crucial difference from TCP, which would detect the error and request a retransmission.

The real beauty of UDP lies in its unreliability. This sounds like a bug, but it’s a feature for applications that can tolerate or even benefit from dropped packets. Streaming media (like video calls or live broadcasts) often uses UDP. If a few frames of video are lost, the viewer might see a momentary glitch, but the stream continues without interruption. Waiting for TCP to retransmit those lost frames would cause a noticeable stutter or pause, which is often worse. Similarly, online games use UDP for real-time actions; a slight lag from retransmitting a lost packet is unacceptable.

When you’re building a UDP application, you often have to implement your own reliability mechanisms on top if you need them. This might involve sequence numbers to detect missing packets and application-level acknowledgments to confirm receipt. However, this gives you fine-grained control over how reliability is handled, allowing you to tailor it to your specific needs (e.g., prioritizing certain packets or defining how many times a packet should be resent).

The next concept you’ll grapple with is how to build reliable communication on top of UDP, often referred to as "UDP hole punching" or implementing custom transport protocols.

Want structured learning?

Take the full Udp course →