HTTP/2 & HTTP/3 — Performance

On HTTP/1.1, a connection handles one request at a time, and the browser only opens 6 connections per host (server name), so files queue and block each other…

Your shop's page loads 14 files after the HTML: CSS, JavaScript and 12 small images. On HTTP/1.1, the 7th file onwards has to queue, because the browser only opens 6 connections to one server. Switch the HTTP version on the server, and the queue is gone without you touching the code.

HTTP is a protocol, a set of rules for how the browser and the server talk: the browser sends a request (GET /style.css), the server answers with a response. The content of requests and responses is the same in every version. What differs is how it's carried over the network. HTTP/1.1 (1997), HTTP/2 (2015) and HTTP/3 (2022) each make better use of one connection.

Picture a queue at a minimarket. HTTP/1.1 is six cashiers who each serve one customer to the end, so a customer with a full basket makes everyone queuing behind them wait. HTTP/2 is one cashier who scans every customer's items in turns, so nobody waits for their turn. HTTP/3 is the same, but if one item falls under the counter, only its owner waits. In this picture, a cashier is a connection, a customer is a requested file, and the item that falls is data lost on the network (explained below).

HTTP/1.1: one request per connection

HTTP/1.1 runs on TCP, a kind of connection that guarantees data arrives complete and in order. Before the first request, the browser needs two trips to the server and back. The time of one trip there and back is called the RTT (round trip time). The first trip opens the TCP connection, the second is for TLS (agreeing on HTTPS encryption). After that, these are the rules:

In the Lab (RTT 100 ms, 14 files: style.css, app.js and 12 small images), the HTML is done at 408 ms. The first connection, the one that brought the HTML, takes style.css right away. Five new connections need 200 ms for TCP and TLS, so they're only ready at 608 ms. The 14 files are handled in three waves, and the page is only complete at 875 ms.

HTTP/2: many requests on one connection

HTTP/2 still uses TCP, but it cuts every request and response into small pieces (frames). These are frames of data, not pictures of the screen. Each frame is tagged with a stream number, one number for each request and its response. Frames from many streams can be sent interleaved (mixed in turns) over one connection. This is called multiplexing: no request waits for its turn.

The problem left: lost packets on TCP

The network sends data in packets (small parts of the data). Sometimes one is lost, especially on a weak phone signal. TCP guarantees order, so when one packet is lost, all the data after it is held back until that packet is sent again, about one RTT. In HTTP/2 every stream sits on one TCP connection, so all of them have to wait, even though the lost packet belongs to just one file.

In the Lab, one packet of app.js is lost on HTTP/2. The other 13 files wait too, and every image that was done at 592 ms can only be used at 637 ms. So multiplexing fixes head-of-line blocking at the HTTP level, but not at the TCP level.

HTTP/3: QUIC on top of UDP

HTTP/3 doesn't use TCP anymore. It uses QUIC, a new protocol built on top of UDP. UDP only sends packets, with no promise of order. QUIC adds reliability (lost packets are sent again) and order itself, but per stream.

HTTP/1.1 tricks that now hurt

People used to use tricks to get past the 6-connection limit. On HTTP/2 and HTTP/3 those tricks aren't needed, and they even hurt:

🎯 Turning it on and checking

This is the setup in Nginx 1.25.1 and later:

server {
    listen 443 ssl;
    listen 443 quic reuseport;   # HTTP/3 over UDP
    http2 on;                    # HTTP/2
    http3 on;
    server_name myshop.com;
    ssl_certificate     /etc/ssl/myshop.crt;
    ssl_certificate_key /etc/ssl/myshop.key;
    # Tell the browser: HTTP/3 is available on port 443
    add_header Alt-Svc 'h3=":443"; ma=86400';
}

Open UDP port 443 in the firewall (the filter for network traffic on the server) too. A port is a numbered door into the server, and HTTPS uses 443. CDNs like Cloudflare usually have HTTP/2 and HTTP/3 on already. To check, open the Network tab in DevTools, right-click the column headers, then turn on Protocol: it shows http/1.1, h2 or h3.

⚠️ Old tricks and misconceptions about HTTP/2 and HTTP/3

TL;DR: On HTTP/1.1, a connection handles one request at a time, and the browser only opens 6 connections per host (server name), so files queue and block each other (head-of-line blocking). HTTP/2 uses multiplexing: many requests sent interleaved over one TCP connection, plus header compression (HPACK). In the Lab, 875 ms becomes 628 ms with no code changes. But on TCP, one lost packet (a small part of the data) makes every stream (every request and its response) wait. HTTP/3 uses QUIC on top of UDP (QUIC is a new protocol, and UDP sends packets with no promise of order). A lost packet only makes its own stream wait, and the handshake (the opening steps of a connection) takes 1 RTT, one trip to the server and back (628 becomes 528 ms). Server Push (sending files before they're requested) has been removed from browsers, and old tricks like domain sharding (spreading files over several domains) now slow things down.

⚡ HTTP/1.1: one request per connection

Meaning: On HTTP/1.1, one connection only handles one request at a time, and the browser opens at most 6 connections per host (server name). A slow file makes the queue behind it wait. This is called head-of-line blocking. In the Lab, the page is only complete at 875 ms.

⚡ HTTP/2: many requests on one connection

Guess first: HTTP/2 sends many requests over one connection. How does it do that, so that no request waits for its turn?

Requests and responses are cut into frames (small pieces) and sent interleaved (mixed in turns) over one connection. This is called multiplexing. In the Lab, 875 ms becomes 628 ms with no code changes.

Meaning: But HTTP/2 still runs on TCP, which guarantees the order of the data. One lost packet (a small part of the data) makes every stream wait, that is, every request and its response on that connection.

⚡ HTTP/3: QUIC on top of UDP

Write:

listen 443 quic reuseport;
http3 on;
add_header Alt-Svc 'h3=":443"; ma=86400';

Meaning: This is the setup for HTTP/3 in Nginx, a web server program. HTTP/3 uses QUIC (a new protocol) on top of UDP (a way of sending packets with no promise of order). A lost packet only makes its own stream wait, and the handshake (the opening steps of a connection) takes 1 RTT (one trip to the server and back). The browser only learns HTTP/3 is there from the Alt-Svc header. Old tricks like domain sharding (spreading files over several domains) now slow things down.

What you will learn