AlgoViz

HTTP/2 and HTTP/3

medium 30 min

HTTP/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

123456
redr1r2
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

HTTP/3HTTP/2HTTP/1.1Browserfar awayCDN edgenear the userLoad balancerour regionApp servermakes pages

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

    critical

    A recorded first packet can be sent again. An order or payment would be made twice.

    Wrong
    if request.early_data:    handle(request)
    Right
    if request.early_data and not is_safe(request):    return 425          # Too Early: ask againhandle(request)
  • HTTP/3 without a fallback

    critical

    Some networks block UDP. For them the site is gone.

    Wrong
    listen = ["udp/443"]
    Right
    listen = ["udp/443", "tcp/443"]# ALPN: h2, http/1.1
  • Keeping the old tricks

    minor

    Several host names mean several handshakes and connections that do not know each other's priorities.

    Wrong
    hosts = ["img1", "img2", "img3", "img4"]
    Right
    host = "www"# long cache times, sensible bundles
  • Balancing per connection

    major

    One HTTP/2 connection carries all calls of a client, so one server gets them all.

    Wrong
    server = pick(client.ip, client.port)
    Right
    for stream in connection.streams:    server = fewest_open_requests()
  • Promising a percentage

    minor

    The gain depends on loss, distance and server. On clean networks it may be nothing.

    Wrong
    "HTTP/3 is 30% faster"
    Right
    compare(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.