UDP
Examine connectionless, lightweight datagram transmission, delivery trade-offs, and practical use cases like real-time media and online gaming.
Lesson goal
By the end of this lesson, you will understand how UDP delivers lightweight, connectionless datagrams, the trade-offs of unverified delivery, and why real-time systems like live streaming, gaming rely on UDP over TCP.
UDP (User Datagram Protocol) is a fast, lightweight communication tool used to send data packets across a network. It does not set up a connection first and does not check if data arrives, making it very fast but less reliable than TCP.
UDP Datagrams
A UDP datagram is a unit of data sent using UDP.
Unlike TCP—which requires a three-way handshake before any communication can start—UDP sends datagrams immediately without establishing a connection first. This eliminates initial connection latency entirely.
UDP Delivery Tradeoffs
UDP provides no built-in guarantee of:
- Delivery — a message may not arrive.
- Ordering — messages may not arrive in the order they were sent.
- Retransmission — UDP does not automatically resend missing or corrupted messages.
These properties are the central delivery tradeoffs of UDP. While TCP ensures 100% data integrity through acknowledgements and retransmissions (making it the right choice for web pages, APIs, and file downloads), UDP discards these checks in exchange for raw speed and minimal latency.
| Feature | TCP Guarantee | UDP Guarantee |
|---|---|---|
| Connection Setup | Three-way handshake required | None (Connectionless) |
| Delivery | Guaranteed via acknowledgements | Not guaranteed (fire-and-forget) |
| Packet Ordering | Guaranteed via sequence numbers | Not guaranteed (packets arrive as-is) |
| Retransmission | Automatic on packet loss | None |
| Overhead & Latency | Higher overhead, setup delay | Minimal overhead, near-zero latency |
| Best Used For | File transfers, HTTP/HTTPS, emails | Live streaming, voice calls, games |
UDP for Real-Time Media
Live video, voice calls, and other real-time streams have a strict priority: data should arrive in time to be useful.
Timely data versus perfect delivery
Real-time media often favors timely data over perfect delivery. If TCP were used here, a lost audio or video packet would cause playback to freeze while waiting for a retransmission. With UDP, the stream simply drops the missed frame and keeps playing in real time.
| Priority | Goal | Protocol Fit |
|---|---|---|
| Timely data | Keep the live stream moving in real time | UDP |
| Perfect delivery | Ensure every piece of data arrives correctly | TCP |
Main takeaway
For real-time media, useful data that arrives on time matters more than perfect data that arrives too late.
UDP for Online Games
Online multiplayer games use UDP when the current state matters more than recovering outdated packets.
Tick 1: Player at (10, 20) ---> [Dropped]
Tick 2: Player at (11, 21) ---> [Arrived] (Current state applied; Tick 1 ignored)If a multiplayer game used TCP, a single dropped packet would force the entire game client to pause and lag while waiting for the old packet to be resent. With UDP, the game simply applies the newest position update (Tick 2) and ignores the stale packet (Tick 1).
Key Takeaways
- Connectionless Transport: UDP sends standalone datagrams immediately without the three-way handshake delay of TCP.
- Delivery Trade-offs: UDP trades TCP's reliability, sequence ordering, and retransmissions for maximum transmission speed and low overhead.
- Timely Over Perfect: Real-time applications (voice calls, video calls, live streaming) favor timely data flow over TCP's retransmission pauses.
- State Over History: Fast-paced online multiplayer games use UDP to drop outdated state packets rather than lagging to retrieve old positions.
- TCP vs. UDP Choice: Use TCP when complete reliability and ordered delivery are required (web pages, database queries, file transfers); use UDP when speed, low latency, and real-time responsiveness are the priority.