AlgoViz

HTTP/2 and HTTP/3

medium 30 min

HTTP/2 cuts messages into small frames and mixes many requests on one TCP connection.

Video lesson · Chapter 1 of 5

Three colored files share the connection

0:00 / 3:46

HTTP/2 and HTTP/3 in 85 seconds

1:25Reel coming soon

Theory

Read or listen first. About 2 min.

What it is

HTTP has one meaning and three ways of travelling. Methods, status codes, headers and bodies are the same in HTTP/1.1, HTTP/2 and HTTP/3. What differs is how they are put on the wire.

HTTP/2 sends binary frames. Each frame belongs to a numbered stream, one stream per request and answer, and the frames of many streams are mixed on one TCP connection. HTTP/3 keeps the idea, but uses QUIC over UDP. QUIC knows the streams itself, includes TLS 1.3, and delivers each stream independently.

Why it exists

On an HTTP/1.1 connection answers come one after the other. Browsers therefore open up to six connections to a host, and sites invented tricks such as joining files and spreading them over host names.

HTTP/2 removed that limit, and with it showed the next one: TCP delivers bytes in order, so a single lost packet makes every stream wait. TCP lives in operating systems and network boxes and changes slowly. QUIC moves the job above UDP, where browsers and servers can improve it themselves.

A short history

  1. 1997

    HTTP/1.1 appears as RFC 2068, in January.

  2. November 2009

    Google announces SPDY, an experiment with mixed streams. HTTP/2 grows out of it.

  3. 2012

    Google first deploys its early QUIC, designed by Jim Roskind.

  4. May 2015

    HTTP/2 becomes RFC 7540, and its header compression HPACK RFC 7541.

  5. May 2021

    The IETF publishes QUIC as RFC 9000, with RFC 9001 for its use of TLS 1.3.

  6. June 2022

    HTTP/3 becomes RFC 9114. On the way, HTTP/2 is revised as RFC 9113.

Words to know

Frame
The smallest unit: a type, a length and a payload. In HTTP/2 also the number of its stream.
Stream
One request and its answer, inside a shared connection.
Multiplexing
Mixing the frames of many streams on one connection.
Head-of-line blocking
Something ready must wait for something in front of it.
RTT
Round-trip time: there and back once.
QUIC
A transport over UDP with streams, loss recovery, congestion control and TLS 1.3 built in.
Connection ID
The number that names a QUIC connection, independent of the IP address.
HPACK, QPACK
Header compression of HTTP/2 and of HTTP/3.
0-RTT
Sending a request in the first packet of a repeated visit.
ALPN
The part of TLS in which both sides agree on h2, h3 or http/1.1.

How it works

Step through the idea, or press Play.

Step 1 of 3

One at a time. HTTP/1.1: an answer blocks the next one on its connection

Understand it

Start here

Parcels, a belt and zip lines

Max sends three loads, one red, one yellow and one green, to Lee on the other side of the yard.

Three loads to the other side
One big load at a time

The yard is the same: total speed and lost parcels do not change. What changes is who has to wait.

What you get

Why it matters

  • Many requests at once

    One connection instead of six, and 100 or more streams on it.

  • Shorter start

    TCP and TLS 1.3: 2 round trips before the first request. QUIC: 1. A repeated visit: 0.

  • Loss hurts less

    HTTP/3 only. A lost packet stops its own streams.

  • Change of network

    HTTP/3 only. From Wi-Fi to mobile without a new connection.

  • Small headers

    Repeated headers become one or two bytes each.

  • Server push is over

    Once the sixth reason. Chrome removed it in version 106, nginx in 1.25.1. Use preload and early hints.

HTTP/2

Frames and streams

Frame types of HTTP/2

FrameJob
HEADERSRequest or answer headers, opens a stream
CONTINUATIONMore headers, when one frame is not enough
DATAA piece of the body
RST_STREAMEnds one stream, the connection stays
WINDOW_UPDATEThe receiver allows more bytes
SETTINGSLimits, sent first by both sides
PINGIs the connection alive, and how far away
GOAWAYNo new streams, with the last one handled
PUSH_PROMISEServer push, off in browsers
PRIORITYOld priorities, deprecated by RFC 9113
Each frame has 9 bytes of header and by default at most 16,384 bytes of payload. Streams opened by the client have odd numbers: 1, 3, 5. Stream 0 is the connection.

Three requests on one connection

The server may answer in any order and mix the pieces. The number says where each belongs.

Limits that meet you in practice

SettingDefault of the standardAdvice
Streams at the same timeNo limit until the server sets oneAt least 100 recommended. nginx: 128
Window per stream65,535 bytesRaise for long distances
Window of the connection65,535 bytesRaise with WINDOW_UPDATE
Largest frame16,384 bytesRarely worth changing
HPACK table4,096 bytesLeave

Why the window matters

65,535 B0.1 s≈655 KBs≈5.2 Mbits\frac{65{,}535\ \text{B}}{0.1\ \text{s}} \approx 655\ \tfrac{\text{KB}}{\text{s}} \approx 5.2\ \tfrac{\text{Mbit}}{\text{s}}
With a round trip of 100 ms the sender may have 65,535 bytes on the way and must then wait. Browsers and good servers raise the window; home-made clients often forget it.

Headers

HPACK and QPACK

A request may carry several hundred bytes of headers, mostly the same each time. HPACK keeps a static table of 61 common entries and a dynamic table of headers already sent on this connection. A known header becomes an index of one byte or a little more; new text is shortened with a Huffman code.

An example

100 requests×600 B=60,000 B;→;600+99×60=6,540 B100\ \text{requests} \times 600\,\text{B} = 60{,}000\,\text{B} ;\to; 600 + 99 \times 60 = 6{,}540\,\text{B}
Assumption: after the first request 60 bytes of headers are new or indexed. The share you save depends on how alike your requests are.

HPACK needs everything in order. HTTP/3 streams may arrive in any order, so QPACK sends changes of the table on streams of their own, and a request may be held only if it uses an entry that has not arrived. Its static table has 99 entries, and its dynamic table is off until the receiver allows one. Both avoid the kind of compression that let attackers guess secrets in TLS-compressed headers.

The problem

Head-of-line blocking

WhereHTTP/1.1HTTP/2HTTP/3
Between answers on one connectionYesNoNo
Between streams, after a lost packetOne connection waits, others go onAll streams waitOnly streams with data in that packet
Inside one streamYesYesYes
On a very lossy network HTTP/2 can be slower than six HTTP/1.1 connections: all eggs are in one basket.

One lost packet

In both cases the lost bytes are sent again after about one round trip. The question is what may be handed to the browser meanwhile.

HTTP/3

QUIC

Two stacks

Two stacks side by side. HTTP/2 runs on TLS, on TCP, on IP: streams are known only to HTTP/2. HTTP/3 runs on QUIC, which contains streams, loss recovery and TLS 1.3, on UDP, on IP.
In HTTP/3 the transport itself knows the streams. That is the whole trick.
  • Reliable over UDP

    QUIC numbers packets, acknowledges, resends and controls congestion itself. UDP only carries it.

  • Always encrypted

    TLS 1.3 is part of the handshake, and most of each packet header is hidden too.

  • In the application

    Usually a library instead of the kernel, so it can be updated with the browser or the server.

  • No amplifier

    Before the client's address is proven, the server sends at most three times what it received.

The name was once proposed as short for Quick UDP Internet Connections. In the standard it is simply a name. QUIC is also used without HTTP, for example below WebTransport and for DNS.

Start

Handshakes

Round trips before the first request

TCPTLSSumFirst byte of the answer at
HTTP/1.1 or HTTP/2, TLS 1.21234 RTT
HTTP/1.1 or HTTP/2, TLS 1.31123 RTT
HTTP/1.1 or HTTP/2, TLS 1.3 early data1012 RTT
HTTP/3, first visit01, with QUIC12 RTT
HTTP/3, 0-RTT0001 RTT
Without DNS and server time. Early data exists for TLS over TCP too, but TCP still costs its round trip.

At 80 ms

HTTP/2: 3×80=240 msHTTP/3: 2×80=160 ms0-RTT: 80 ms\text{HTTP/2: } 3 \times 80 = 240\,\text{ms} \qquad \text{HTTP/3: } 2 \times 80 = 160\,\text{ms} \qquad \text{0-RTT: } 80\,\text{ms}
Time to the first byte with TLS 1.3 and a server that answers at once. The saving is one round trip per new connection.

One round trip

On the move

Connection migration

A TCP connection is named by two addresses and two ports. A phone that leaves the Wi-Fi gets a new address, and its TCP connections end. A QUIC connection is named by connection IDs, up to 20 bytes long, so packets from the new address still belong to it. The other side first checks that the new path works.

From Wi-Fi to mobile

Both ids belong to the same connection. A fresh one is used, so that watchers cannot link the two networks.

Discovery

How a browser finds HTTP/3

  1. 1

    First contact by TCP

    ALPN in TLS agrees on h2, or on http/1.1.

  2. 2

    The hint

    The answer carries Alt-Svc: h3=":443"; ma=86400: HTTP/3 is here, remember it for a day. A DNS record of type HTTPS (RFC 9460) can say so before the first connection.

  3. 3

    QUIC next time

    The browser tries UDP port 443, often racing it against TCP.

  4. 4

    Fallback

    If UDP is blocked, as in some company networks, the page loads over HTTP/2. So keep HTTP/2 on.

Each hop chooses for itself

A browser speaks HTTP/3 over QUIC to a CDN edge, and HTTP/2 over TCP as the fallback. The CDN edge, run by another company, speaks HTTP/2 to the load balancer in our system, which speaks HTTP/1.1 to the app server.
The long, lossy hop gains most from HTTP/3. Between machines in one building, HTTP/1.1 with kept connections is often fine.

Operate

Configure a server

nginx
server {    listen 443 ssl;    listen 443 quic reuseport;     # UDP, HTTP/3    listen [::]:443 ssl;    listen [::]:443 quic reuseport;    http2 on;                      # since 1.25.1, replaces "listen ... http2"    server_name example.com;    ssl_certificate     /etc/ssl/example.com.pem;    ssl_certificate_key /etc/ssl/example.com.key;    ssl_protocols TLSv1.2 TLSv1.3; # QUIC uses 1.3, others may need 1.2    http2_max_concurrent_streams 128;   # the default    quic_retry on;                      # check the client's address first    ssl_early_data off;                 # 0-RTT: see the warning above    add_header Alt-Svc 'h3=":443"; ma=86400' always;    add_header Strict-Transport-Security "max-age=63072000" always;    location / { root /var/www/html; }}# Open UDP 443 in the firewall too:  sudo ufw allow 443/udp# Log the variable $http3 to see who uses it.
HTTP/3 in nginx (module since 1.25.0) is still marked experimental in its documentation. One reuseport per address and port in the whole configuration. quic_retry on costs a round trip.
Shell
curl -sI --http2 https://example.com            # HTTP/2 200 ?curl -sI --http3-only https://example.com       # needs a curl built with HTTP/3curl -sI https://example.com | grep -i alt-svc  # is HTTP/3 announced?curl -v --http2 https://example.com 2>&1 | grep -i alpncurl --http2-prior-knowledge http://localhost:8080   # HTTP/2 without TLS, only if knowncurl --http2 -Z -O https://example.com/a -O https://example.com/b   # parallel

In the browser, performance.getEntriesByType("resource") gives nextHopProtocol for each file: http/1.1, h2 or h3. Group your timings by it.

What to watch

MeasureWhy
Share of h3, h2, http/1.1Whether discovery works and where UDP is blocked
Time to first byte and page timings, per protocol and networkThe reason for doing it
CPU per gigabyteQUIC encrypts and counts in the application
Streams reset per connectionCancelled pages, or abuse
Connections per machine after a deploymentLong connections do not move by themselves

Performance

Habits to drop, and to keep

Now

  • One host name for your own files
  • Sensible bundles: tens of files, not one and not a thousand. Larger files compress better
  • fetchpriority="high" for the main image, low for what can wait
  • rel="preload" for fonts, rel="preconnect" and rel="dns-prefetch" for other hosts
  • 103 Early Hints while the page is being made, over HTTP/2 or later
  • Long cache times with a hash in the file name

From the old days

  • Spreading files over host names (sharding)
  • Joining everything into one file
  • Putting images into the CSS as text
  • Server push
  • Headers of many kilobytes
  • Forgetting that defer and order still matter

Priorities were rewritten too: the header Priority: u=1, i (RFC 9218) gives an urgency from 0 (first) to 7, with 3 as the default, and i for answers that are useful piece by piece. Browsers may also use one connection for several host names, if the certificate covers them and they lead to the same server.

Behind the edge

Load balancing and gRPC

One connection, one server

This surprises teams that start with gRPC, which runs on HTTP/2. See L4 and L7 load balancers and gRPC.
TopicAdvice
Shutting downSend GOAWAY, finish open streams, then close
Cancelled requestsRST_STREAM ends one stream. Limit how fast one client may open and cancel streams, and keep servers patched (CVE-2023-44487)
RetriesGOAWAY names the last stream handled. Later ones are safe to send again
Without TLSh2 in clear text exists, between machines that know of each other. Browsers use TLS only
WebSocketBy extended CONNECT on one stream: WebSockets
Server-sent eventsThe six-connection limit is gone: server-sent events
Chunked encodingOnly HTTP/1.1. DATA frames replace it, and proxies translate

Decide

Which one, where

HTTP/1.1HTTP/2HTTP/3
Year, standard1997, now RFC 91122015, now RFC 91132022, RFC 9114
FormatTextBinary framesBinary frames
TransportTCPTCPQUIC over UDP
EncryptionOptionalIn browsers alwaysAlways
Requests per connectionOne at a timeMany, mixedMany, independent
Header compressionNoneHPACKQPACK
Start with TLS 1.32 RTT2 RTT1 RTT
New network addressBreaksBreaksContinues
Reading it on the wireEasyTools neededTools and keys needed

Turn it on when

  • You serve a web site or app: HTTP/2 always
  • People are far away, or on mobile or poor Wi-Fi: HTTP/3
  • Pages load many files, or APIs get many calls at once
  • Phones change networks while they use you
  • A CDN offers HTTP/3 with a switch

Be careful when

  • UDP is blocked: keep HTTP/2 as the fallback
  • Old proxies or tools know only HTTP/1.1
  • CPU is your limit: test QUIC under load
  • You must read traffic to find a fault: plan for logs and key export
  • The other end is in the same building: little to gain

Other tools for other jobs: gRPC for calls between services, WebSockets for two-way messages, real-time APIs to choose among them, and CDN for being near the user, which saves more than any protocol.

Summary

The whole topic in one minute.

HTTP/2 gives many streams on one TCP connection, binary frames and compressed headers. TCP still blocks all streams when a packet is lost. HTTP/3 over QUIC blocks only the streams that lost data, needs one round trip to start, and keeps its connection when the address changes.

Offer both and HTTP/1.1 too: browsers start with TCP, learn about HTTP/3 from Alt-Svc or a DNS HTTPS record, and fall back when UDP is blocked. Measure before and after, especially for people on poor networks.

  • The meaning of HTTP is the same in all three versions. The transport differs.
  • HTTP/2: many streams as frames on one TCP connection, HPACK for headers.
  • One lost TCP packet stops all HTTP/2 streams. QUIC stops only the streams it carried.
  • With TLS 1.3 HTTP/2 needs 2 round trips before a request, HTTP/3 needs 1, or 0 on a repeated visit, for safe requests.
  • Connection IDs let a QUIC connection survive a new address.
  • Announce HTTP/3 by Alt-Svc or a DNS HTTPS record, open UDP 443, and keep HTTP/2 as the fallback.
  • Server push is gone: preload and early hints instead.

Sources