HTTP/2 & HTTP/3
HTTP/2 (2015) and HTTP/3 (2022) keep HTTP's meaning exactly the same: methods, status codes, headers and URLs don't change, so your application code doesn't either. What changes is how messages travel on the wire, to make pages load faster.
What was wrong with HTTP/1.1?
- Head-of-line blocking: on one connection, responses come back strictly in order. A slow response delays everything behind it.
- Many connections: browsers open ~6 TCP+TLS connections per host to get parallelism, each with its own handshake and slow start.
- Verbose headers: the same long headers (cookies, user-agent) are sent as text with every request.
HTTP/2: binary frames and multiplexing
HTTP/2 uses one TCP connection per origin and splits every message into small binary frames. Each frame carries a stream ID, so frames from many requests can be interleaved and reassembled at the other end. A slow response no longer blocks fast ones.
Every frame has a 9-byte header:
| Length (24) | Type (8) | Flags (8) | R (1) + Stream Identifier (31) | Payload ... |import struct
FRAME_TYPES = {0: "DATA", 1: "HEADERS", 3: "RST_STREAM", 4: "SETTINGS", 6: "PING", 7: "GOAWAY", 8: "WINDOW_UPDATE"}
END_STREAM, END_HEADERS = 0x1, 0x4
def frame(ftype, flags, stream_id, payload):
length = len(payload)
return struct.pack("!BHBBI", length >> 16, length & 0xFFFF, ftype, flags, stream_id & 0x7FFFFFFF) + payload
def parse_frames(data):
pos = 0
while pos < len(data):
hi, lo, ftype, flags, sid = struct.unpack("!BHBBI", data[pos:pos + 9])
length = (hi << 16) | lo
payload = data[pos + 9:pos + 9 + length]
flag_names = [n for bit, n in [(END_STREAM, "END_STREAM"), (END_HEADERS, "END_HEADERS")] if flags & bit]
print(f"stream {sid}: {FRAME_TYPES[ftype]:<8} {length:>3} bytes {'|'.join(flag_names)}")
pos += 9 + length
# Two responses interleaved on one connection: stream 1 (a big page) and stream 3 (a small icon).
wire = b"".join([
frame(1, END_HEADERS, 1, b"<compressed headers>"),
frame(1, END_HEADERS, 3, b"<hdrs>"),
frame(0, 0, 1, b"x" * 100),
frame(0, END_STREAM, 3, b"icon-bytes"),
frame(0, END_STREAM, 1, b"x" * 60),
])
print(frame(4, 0, 0, b"")[:9].hex(" "), "<- an empty SETTINGS frame header")
parse_frames(wire)00 00 00 04 00 00 00 00 00 <- an empty SETTINGS frame header
stream 1: HEADERS 20 bytes END_HEADERS
stream 3: HEADERS 6 bytes END_HEADERS
stream 1: DATA 100 bytes
stream 3: DATA 10 bytes END_STREAM
stream 1: DATA 60 bytes END_STREAM
The small icon on stream 3 finished before the big page on stream 1, all on one connection.
Header compression (HPACK)
HTTP/2 compresses headers with HPACK: both sides keep a table of recently sent headers, so a repeated header becomes a tiny index instead of the full text. Here's the idea in a simplified model:
requests = [
{":method": "GET", ":path": "/", "user-agent": "Mozilla/5.0 (X11; Linux x86_64) Firefox/131.0",
"cookie": "session=8c1f4a2e9b7d"},
{":method": "GET", ":path": "/style.css", "user-agent": "Mozilla/5.0 (X11; Linux x86_64) Firefox/131.0",
"cookie": "session=8c1f4a2e9b7d"},
{":method": "GET", ":path": "/app.js", "user-agent": "Mozilla/5.0 (X11; Linux x86_64) Firefox/131.0",
"cookie": "session=8c1f4a2e9b7d"},
]
table = []
for i, headers in enumerate(requests, start=1):
plain = sum(len(k) + len(v) + 4 for k, v in headers.items()) # "name: value\r\n"
encoded = 0
for pair in headers.items():
if pair in table:
encoded += 1 # one-byte index into the table
else:
encoded += len(pair[0]) + len(pair[1]) + 2
table.append(pair)
print(f"request {i}: {plain} bytes as HTTP/1.1 text, ~{encoded} bytes with a header table")request 1: 113 bytes as HTTP/1.1 text, ~105 bytes with a header table
request 2: 122 bytes as HTTP/1.1 text, ~20 bytes with a header table
request 3: 119 bytes as HTTP/1.1 text, ~17 bytes with a header table
The remaining problem: TCP head-of-line blocking
HTTP/2 removed blocking at the HTTP level, but all streams still share one TCP byte stream. If one TCP segment is lost, TCP holds back all data behind it until the retransmission arrives, even frames belonging to other streams that arrived intact:
# Segments on one TCP connection; each carries a frame of one stream. Segment 2 is lost once.
segments = [(1, "s1"), (2, "s3"), (3, "s5"), (4, "s3"), (5, "s1")]
lost = 2
def delivery(per_stream):
delivered_at = {}
for seq, stream in segments:
if per_stream:
# QUIC: only the stream that lost data waits for the retransmission
delay = "after retransmit" if (seq == lost or (stream == "s3" and seq > lost)) else "immediately"
else:
# TCP: everything after the hole waits
delay = "after retransmit" if seq >= lost else "immediately"
delivered_at.setdefault(delay, []).append(f"{stream}#{seq}")
return delivered_at
for name, per_stream in [("HTTP/2 over TCP", False), ("HTTP/3 over QUIC", True)]:
print(name)
for when, frames in delivery(per_stream).items():
print(f" {when:<17} {frames}")HTTP/2 over TCP
immediately ['s1#1']
after retransmit ['s3#2', 's5#3', 's3#4', 's1#5']
HTTP/3 over QUIC
immediately ['s1#1', 's5#3', 's1#5']
after retransmit ['s3#2', 's3#4']
HTTP/3 and QUIC
HTTP/3 runs over QUIC, a transport protocol built on UDP (originally designed at Google, standardised as RFC 9000). QUIC provides what TCP + TLS did, re-engineered:
| Feature | TCP + TLS 1.3 | QUIC |
|---|---|---|
| Streams | one byte stream | many independent streams: loss only blocks the affected stream |
| Handshake | TCP (1 RTT) + TLS (1 RTT) | combined: 1 RTT, or 0 RTT when resuming |
| Encryption | headers visible, payload encrypted | almost everything encrypted, including most transport headers |
| Connection identity | the 5-tuple: changing Wi-Fi → 4G breaks the connection | a connection ID: survives network changes |
| Implementation | in the OS kernel | in user space (browsers, libraries): evolves quickly |
rtt = 80 # ms
setups = {
"HTTP/1.1 or HTTP/2 over TCP + TLS 1.2": 3 * rtt,
"HTTP/2 over TCP + TLS 1.3": 2 * rtt,
"HTTP/3 (QUIC), first visit": 1 * rtt,
"HTTP/3 (QUIC), 0-RTT resumption": 0 * rtt,
}
for name, setup in setups.items():
print(f"{name:<40} first response after ~{setup + rtt} ms")HTTP/1.1 or HTTP/2 over TCP + TLS 1.2 first response after ~320 ms
HTTP/2 over TCP + TLS 1.3 first response after ~240 ms
HTTP/3 (QUIC), first visit first response after ~160 ms
HTTP/3 (QUIC), 0-RTT resumption first response after ~80 ms
Servers advertise HTTP/3 support with an Alt-Svc: h3=":443" response header (or an HTTPS DNS record); the browser then tries QUIC on UDP 443 and falls back to TCP if UDP is blocked.
curl -sI --http3 https://www.cloudflare.com/ | head -3Sample output:
HTTP/3 200
date: Sat, 17 Oct 2026 10:45:31 GMT
content-type: text/html; charset=utf-8Note: HTTP/2 and HTTP/3 header names are always lowercase, and the request line is replaced by pseudo-headers (
:method,:path,:scheme,:authority). Browser developer tools show the protocol per request in the Network tab's "Protocol" column (h2,h3).
Test yourself
What is the main improvement of HTTP/2 over HTTP/1.1?
- New status codes
- Multiplexing many requests as interleaved frames over one connection
- It uses UDP
- It removes headers
Which transport does HTTP/3 use?
- TCP
- QUIC over UDP
- SCTP
- Raw IP
Why does a lost packet hurt HTTP/2 more than HTTP/3?
- HTTP/2 has no retransmission
- TCP delivers bytes in order, so every stream waits for the lost segment
- HTTP/3 never loses packets
- HTTP/2 uses smaller packets