Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

UDP: The Simple Protocol

UDP (User Datagram Protocol) is TCP’s minimalist counterpart. Where TCP provides reliability, ordering, and connection management, UDP provides almost nothing—just a thin wrapper around IP. This simplicity makes it fast, lightweight, and ideal for certain use cases.

What UDP Provides

┌─────────────────────────────────────────────────────────────┐
│                    UDP Provides                             │
├─────────────────────────────────────────────────────────────┤
│  ✓ Multiplexing via ports                                   │
│    (Multiple apps can use the network)                      │
│                                                             │
│  ✓ Checksum for error detection                             │
│    (Optional in IPv4, mandatory in IPv6)                    │
│                                                             │
│  That's it. Really.                                         │
└─────────────────────────────────────────────────────────────┘

What UDP Does NOT Provide

┌─────────────────────────────────────────────────────────────┐
│                 UDP Does NOT Provide                        │
├─────────────────────────────────────────────────────────────┤
│  ✗ Reliability (packets may be lost)                        │
│  ✗ Ordering (packets may arrive out of order)               │
│  ✗ Duplication prevention (same packet may arrive twice)    │
│  ✗ Connection state (no handshake, no teardown)             │
│  ✗ Flow control (can overwhelm receiver)                    │
│  ✗ Congestion control (can overwhelm network)               │
└─────────────────────────────────────────────────────────────┘

TCP vs. UDP at a Glance

TCP                              UDP
─────────────────────────────────────────────────────────────
Connection-oriented              Connectionless
Reliable delivery                Best-effort delivery
Ordered delivery                 No ordering guarantee
Flow control                     No flow control
Congestion control               No congestion control
Higher latency                   Lower latency
Higher overhead                  Lower overhead
Stream-based                     Message-based

Why Choose UDP?

If UDP lacks so many features, why use it?

1. Lower Latency

TCP connection setup:
  1. SYN ────────> (1 RTT)
  2. <──────── SYN-ACK
  3. ACK + Data ──> (another RTT for handshake)

UDP "setup":
  1. Data ────────> (immediate!)

For single request-response: UDP saves at least 1 RTT

2. No Head-of-Line Blocking

TCP: Packet 3 lost

  Received: 1, 2, [gap], 4, 5, 6, 7
                   │
                   └── Can't deliver 4-7 until 3 arrives!

UDP: Packet 3 lost

  Received: 1, 2, 4, 5, 6, 7  ← Deliver immediately!
                 │
                 └── Application decides what to do

For real-time apps, old data is often worthless anyway.

3. Message Boundaries Preserved

TCP is a byte stream:
  send("Hello")
  send("World")

  Receiver might get: "HelloWorld" or "Hell" + "oWorld"
  (No message boundaries)

UDP is message-based:
  sendto("Hello")
  sendto("World")

  Receiver gets: "Hello" then "World"
  (Each datagram is discrete)

4. Application Control

TCP decides:
  - When to retransmit
  - How fast to send
  - How to react to loss

UDP lets the application decide:
  - Custom retransmission logic
  - Application-specific rate control
  - Skip old data, prioritize new

When to Use UDP

┌─────────────────────────────────────────────────────────────┐
│                    UDP Is Good For:                         │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  Real-time Applications                                     │
│    • Voice/Video calls (VoIP)                               │
│    • Live streaming                                         │
│    • Online gaming                                          │
│    • Real-time sensor data                                  │
│                                                             │
│  Simple Request-Response                                    │
│    • DNS queries                                            │
│    • NTP (time sync)                                        │
│    • DHCP                                                   │
│                                                             │
│  Broadcast/Multicast                                        │
│    • Service discovery                                      │
│    • Network announcements                                  │
│    • LAN games                                              │
│                                                             │
│  Custom Protocols                                           │
│    • QUIC (UDP-based, adds reliability)                     │
│    • Custom game protocols                                  │
│    • IoT protocols                                          │
│                                                             │
└─────────────────────────────────────────────────────────────┘

What You’ll Learn

In this chapter:

  1. UDP Header and Datagrams: The simple packet format
  2. When to Use UDP: Detailed use cases and examples
  3. UDP vs TCP Trade-offs: Making the right choice

UDP’s simplicity is its strength. By providing just enough transport-layer functionality, it enables applications to build exactly what they need on top—nothing more, nothing less.