HTTP/2 and HTTP/3
medium 30 minHTTP/2 cuts messages into small frames and mixes many requests on one TCP connection.
See it run
Streams on one connection, the packet that gets lost, fewer round trips, and a phone that changes networks.
Operations
Mixing streams
Three answers of two pieces each. One after the other, then mixed.
Step 1 of 4
HTTP/1.1: red
| 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|
| red | r1 | r2 | ||||
| yellow | ||||||
| green |
The red answer takes the first two slots.
Step 1: HTTP/1.1: red
def http1(answers): for a in answers: for piece in a: send(piece)def http2(streams): while any(streams): for s in streams: # by priority if s: send(frame(s.id, s.pop(0)))- Waits for
- Nothing in front of it
- Wins when
- Answers are many or uneven
The total is the same. A small answer no longer waits for a large one.
In the real world
The same http/2 and http/3 doing real jobs. Pick one and step through it.
Every hop has its own protocol.
Step 1 of 4
The long hop
The phone speaks HTTP/3 to an edge near it. Loss and movement happen here.
Step 1: The long hop
Common mistakes
The bugs interviewers watch for, with the fix beside each.
0-RTT for everything
criticalA recorded first packet can be sent again. An order or payment would be made twice.
Wrongif request.early_data: handle(request)Rightif request.early_data and not is_safe(request): return 425 # Too Early: ask againhandle(request)HTTP/3 without a fallback
criticalSome networks block UDP. For them the site is gone.
Wronglisten = ["udp/443"]Rightlisten = ["udp/443", "tcp/443"]# ALPN: h2, http/1.1Keeping the old tricks
minorSeveral host names mean several handshakes and connections that do not know each other's priorities.
Wronghosts = ["img1", "img2", "img3", "img4"]Righthost = "www"# long cache times, sensible bundlesBalancing per connection
majorOne HTTP/2 connection carries all calls of a client, so one server gets them all.
Wrongserver = pick(client.ip, client.port)Rightfor stream in connection.streams: server = fewest_open_requests()Promising a percentage
minorThe gain depends on loss, distance and server. On clean networks it may be nothing.
Wrong"HTTP/3 is 30% faster"Rightcompare(ttfb, by=["protocol", "network", "country"])
Interview tips
How to explain it
- Same HTTP, other transport.
- HTTP/2 mixes streams on TCP.
- HTTP/3 separates them on QUIC.
Memory trick
- 2 mixes, 3 separates.
- QPACK goes with QUIC.
- Round trips: 3, 2, 1 to the first byte.
Reach for HTTP/3 when
- Mobile, lossy or far.
- Users change networks.
- The CDN has a switch.
Watch out for
- Blocked UDP.
- Replays of 0-RTT.
- Long connections behind balancers.