UDP & QUIC
Choose between TCP and UDP and explain how QUIC builds reliability on top of UDP.
By Invicly Tech, , in Computer Networking. About 13 minutes to read.
In short
UDP sends independent datagrams with just ports, a length and a checksum: no handshake, no retransmission, no ordering. That suits DNS queries, voice, video, games and protocols that build their own reliability. QUIC is such a protocol: it runs over UDP and provides encrypted, multiplexed, reliable streams for HTTP/3.
Explained simply
UDP works like sending a postcard. You write the address and drop it in the box. There is no handshake, no confirmation and no promise about order. Each send becomes one datagram, a single message that arrives whole or not at all.
The UDP header is tiny: 8 bytes with four fields. Because UDP adds almost nothing, the application decides everything: when to send, what to resend and what to skip. That is perfect when late data is useless, as in a voice call or a game, or when a whole request fits in one packet, as in a DNS lookup.
Building reliability yourself is hard, and QUIC is a protocol that does it well. QUIC runs on top of UDP and adds connections, encryption, reliable delivery and congestion control. HTTP/3 runs on QUIC. Why UDP? Firewalls and home routers mostly pass only TCP and UDP, and operating system kernels change slowly, so a new protocol is easiest to ship inside UDP.
QUIC brings real wins. A new connection with encryption is ready after one round trip, and a returning client can send data immediately. Many independent streams share one connection, and a lost packet delays only its own stream. A connection can even survive a switch from Wi-Fi to mobile data.
Press Play on a diagram to walk through it one step at a time.
- Connection-oriented handshake
- Reliable, ordered byte stream
- Flow and congestion control
- 20+ byte header
- Web, SSH, databases
- Connectionless
- No delivery or order guarantee
- 8-byte header
- Low latency
- DNS, VoIP, games, QUIC
- HTTP/2 with HPACK headers
- TLS 1.3 on top of the byte stream
- TCP, inside the kernel
- IP
- HTTP/3 with QPACK headers
- QUIC with TLS 1.3 built in, in user space
- UDP, usually port 443
- IP
- 0x0: client-opened, two-way
- IDs 0, 4, 8 ...
- One HTTP/3 request each
- 0x1: server-opened, two-way
- IDs 1, 5, 9 ...
- Not used by HTTP/3
- 0x2: client-opened, one-way
- IDs 2, 6, 10 ...
- HTTP/3 control and QPACK streams
- 0x3: server-opened, one-way
- IDs 3, 7, 11 ...
- Control, QPACK and push streams
In depth
The UDP header and the limits of a datagram
The UDP header has four 16-bit fields. The source port is optional and may be 0 when no reply is expected. The destination port picks the receiving socket. Length covers header plus data, so it is at least 8. The checksum covers a pseudo-header (the source and destination IP addresses), the UDP header and the data. In IPv4 the checksum may be 0, meaning "not computed"; in IPv6 it is required.
Over IPv4 a single datagram can carry at most 65,507 bytes (65,535 minus 20 bytes of IP header and 8 of UDP). Anything larger than the path MTU is split into IP fragments, and losing any one fragment loses the whole datagram. So real protocols keep datagrams small; QUIC and DNS aim for around 1,200 to 1,400 bytes.
When a socket's receive buffer is full, the kernel silently drops new datagrams and counts them in UdpRcvbufErrors.
- 1Source port (16) | Destination port (16)
Source may be 0 if no reply is needed
- 2Length (16) | Checksum (16)
Length includes the 8-byte header
- 3Data (length minus 8 bytes)
One whole application message
Workloads that suit UDP
UDP fits four patterns. First, a small request with a small answer, where a handshake would double the time: DNS on port 53, NTP on 123, DHCP on 67 and 68. Classic DNS replies were limited to 512 bytes; EDNS allows larger ones, and when an answer still does not fit, the server sets the TC (truncated) bit and the client retries over TCP. Second, real-time media where a resent packet would arrive too late to use: voice and video calls (RTP, WebRTC) and online games. Third, broadcast and multicast, which TCP cannot do at all. Fourth, protocols that bring their own reliability or tunnel other traffic: QUIC, WireGuard (commonly on 51820) and OpenVPN (1194 by default).
UDP has costs too. NAT routers forget idle UDP mappings quickly, often in 30 seconds to a few minutes, so apps send keepalives. And applications must add their own congestion control so they do not flood the network (RFC 8085).
- Request and response: DNS 53, NTP 123, DHCP 67/68, SNMP 161
- Real-time: RTP voice and video, games
- Broadcast and multicast
- Own reliability: QUIC on 443, WireGuard, OpenVPN
How QUIC builds a reliable transport on UDP
QUIC is defined in RFC 9000 (transport), RFC 9001 (how it uses TLS 1.3) and RFC 9002 (loss detection and congestion control). Data travels in frames, such as STREAM frames for application data, ACK frames and CRYPTO frames for the TLS handshake, packed into encrypted packets.
Every packet gets a new packet number that is never reused. Lost data is resent in a new packet with a new number, so an ACK always shows exactly which packet arrived, and RTT samples are never ambiguous as they can be in TCP. ACK frames list many ranges, like a SACK without the 4-block limit. Congestion control works like TCP's; implementations use CUBIC or BBR.
Almost all of each packet is encrypted, including most header fields, so middleboxes cannot meddle with it. To stop QUIC being used for amplification attacks, the client's first datagram is padded to at least 1,200 bytes, and a server sends at most 3 times what it received until the client's address is validated.
Streams, 0-RTT and connection migration
A QUIC connection carries many streams, each an ordered byte stream of its own with its own flow control. The two lowest bits of a stream ID say who opened it and whether it is two-way or one-way. In HTTP/3, each request uses a new client-opened two-way stream.
QUIC carries the TLS 1.3 handshake inside its own, so encryption and transport are ready after one round trip; TCP plus TLS 1.3 needs two. A client that has talked to the server before can use a session ticket to send its request in the very first flight (0-RTT). 0-RTT data can be replayed by an attacker who copies it, so it should only carry safe, repeatable requests such as a GET, and servers may refuse it.
QUIC connections are identified by connection IDs, not by the 5-tuple. When a phone moves from Wi-Fi to mobile data and its IP changes, the connection continues after a quick path check.
- Client to Server: Initial: TLS ClientHello, padded to 1,200 bytes1Initial: TLS ClientHello, padded to 1,200 bytes
- Server to Client: Initial: TLS ServerHello2Initial: TLS ServerHello
- Server to Client: Handshake: certificate and Finished3Handshake: certificate and Finished
- Client to Server: Handshake Finished + 1-RTT: HTTP/3 request4Handshake Finished + 1-RTT: HTTP/3 request
- Server to Client: 1-RTT: HTTP/3 response51-RTT: HTTP/3 response
Head-of-line blocking: TCP versus QUIC
Head-of-line blocking means one missing piece holds up everything queued behind it. HTTP/2 sends many requests as streams over one TCP connection. But TCP only knows a single byte stream. If one packet carrying part of stream A is lost, the receiving kernel cannot hand the browser any later bytes, even ones that belong to streams B and C, until the retransmission arrives. One loss stalls every stream.
QUIC knows about streams, so it keeps each one in order separately. A lost packet with stream A data delays only stream A; data for B and C is delivered at once. Order still matters inside a single stream, and HTTP/3 uses QPACK header compression, designed so headers do not create new blocking between streams.
On a clean wired network the difference is small. On lossy mobile and Wi-Fi links, where loss is common, it is clearly visible in page load times.
- Kernel holds all bytes after the gap
- Streams B and C wait too
- Everything resumes after the retransmit
- Only stream A waits
- Streams B and C are delivered at once
- Stream A resumes after the retransmit
Learn it in 4 short steps
About 30m each. Track them with a timer in your plan.
- 1Read the UDP header~30m
- 2List workloads that suit UDP~30m
- 3Explain QUIC streams and 0-RTT~30m
- 4Compare head-of-line blocking in TCP and QUIC~30m
Key points
- UDP is message-based; each send is one datagram.
- Applications on UDP must handle loss and reordering themselves.
- QUIC combines transport and TLS 1.3 handshakes, often 1 RTT or 0 RTT.
- QUIC streams avoid TCP's head-of-line blocking across streams.
Key terms
Tap a card to flip it. 0/4 known
Examples
dig example.com # DNS over UDP port 53
sudo tcpdump -i any -nn udp port 53
ss -ulnp # listening UDP sockets
curl --http3 -I https://cloudflare.com # needs curl built with HTTP/3UDP in Python: messages, not a stream
This script runs a UDP server and client in one process. Each sendto() becomes exactly one datagram, and each recvfrom() returns exactly one whole message, never half of one or two glued together as can happen with TCP reads. There is no connection: the server replies to whatever address the datagram came from. If a reply never comes, nothing resends it; the client's timeout fires and the application itself must decide to retry. Run sudo tcpdump -i lo -nn udp port 9200 alongside to see one packet per message, each with length set.
import socket
srv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
srv.bind(("127.0.0.1", 9200))
cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
cli.settimeout(1.0)
for msg in [b"one", b"two", b"three"]:
cli.sendto(msg, ("127.0.0.1", 9200))
data, addr = srv.recvfrom(2048) # one datagram = one message
srv.sendto(data.upper(), addr)
try:
print(cli.recvfrom(2048)[0])
except socket.timeout:
print("lost: the app must retry") # UDP never resendsIs this site using HTTP/3?
Browsers do not try QUIC blindly. Step 1: a server announces HTTP/3 in an Alt-Svc response header (h3 on UDP port 443, remembered for ma seconds) or in an HTTPS record in DNS. Step 2: ask curl for HTTP/3 and print the version it used; this needs a curl built with HTTP/3 support. Step 3: watch UDP 443 with tcpdump to see the QUIC packets. If a firewall blocks UDP 443, browsers quietly fall back to HTTP/2 over TCP, so a site can look fine while never using HTTP/3.
curl -sI https://cloudflare.com | grep -i alt-svc
# alt-svc: h3=":443"; ma=86400
dig +short HTTPS cloudflare.com
curl --http3 -s -o /dev/null -w '%{http_version}\n' https://cloudflare.com
# 3
sudo tcpdump -i any -nn -c 5 'udp port 443'Quick reference
- UDP header: 8 bytes = source port, destination port, length, checksum (16 bits each)
- Max UDP payload over IPv4 is 65,507 bytes; stay under the path MTU to avoid fragmentation
- UDP checksum is optional in IPv4 (0 means none) and required in IPv6
- QUIC: RFC 9000 transport, RFC 9001 TLS, RFC 9002 recovery; HTTP/3 is RFC 9114
- New connection: QUIC needs 1 RTT, TCP plus TLS 1.3 needs 2; QUIC resumption can use 0-RTT
- A client's first QUIC datagram is padded to at least 1,200 bytes
- ss -ulnp lists UDP listeners; nstat -az UdpRcvbufErrors counts drops from full buffers
- Servers advertise HTTP/3 with Alt-Svc: h3=":443" or with an HTTPS DNS record
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
List the UDP sockets listening on your machine, then capture one DNS query and its answer and note the ports and lengths.
- Exercise 2medium
Show that UDP silently drops datagrams when the receiver does not read fast enough: send 100,000 datagrams to a socket that reads nothing until the end, then count what arrived and check the kernel counter.
- Exercise 3hard
Write a client function that makes a UDP request reliable: number each request, retry on timeout with a doubling wait, and ignore replies that carry the wrong number. Assume the server echoes the message back.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is UDP faster than TCP?
- UDP skips the handshake and never waits for retransmissions, so it can have lower latency. Raw throughput is not automatically higher, and once you add reliability and congestion control you are doing the same work TCP does.
- Is QUIC only used for HTTP/3?
- No. QUIC is a general transport. DNS over QUIC (RFC 9250), SMB over QUIC and MASQUE tunnelling also use it, though HTTP/3 is by far the most common use.
- What happens if a network blocks UDP port 443?
- Browsers notice that QUIC fails and fall back to HTTP/2 over TCP, usually without the user seeing an error. Pages still load, just without the benefits of QUIC.
- Does connect() on a UDP socket create a connection?
- Not on the wire: no packets are sent. It only sets a default destination, so you can use send() instead of sendto(), and makes the socket ignore datagrams from other addresses and report ICMP errors such as port unreachable.