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:
- One connection only handles one request at a time. The next request waits for the previous response to finish.
- The browser opens at most 6 connections per host (one server name, for example
myshop.com). The 7th file onwards queues. - A slow file makes everything queuing behind it on that connection wait. This is called head-of-line blocking (the one at the front blocks the queue).
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.
- In the Lab, all 14 files are requested at once at 408 ms, on the same connection as the HTML. The page is complete at 628 ms, not 875 ms.
- Header compression (HPACK): some headers repeat on every request, such as cookies (small pieces of data a site keeps in the browser) and the user-agent (which browser is used). Those headers aren't sent again in full.
- Browsers only use HTTP/2 over HTTPS. Turning it on is the server's job, not the page's code.
- Server Push, an HTTP/2 feature for sending files before they're requested, has been removed from browsers (Chrome turned it off in 2022, Firefox in 2024). Instead, use
<link rel="preload">or a 103 Early Hints response (an early answer from the server that names files to start downloading).
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.
- A lost packet only makes the stream it belongs to wait. In the Lab, one packet of
app.jsis lost on HTTP/3: onlyapp.jswaits, and the images are still done on time, at 492 ms. - The handshake (the opening steps before data can be sent) is faster. QUIC combines opening the connection and TLS 1.3 into 1 RTT, not 2. In the Lab, the page is complete at 528 ms, not 628 ms. A returning visitor can even send data right away (0-RTT).
- Connection migration: the connection keeps working when the phone moves from Wi-Fi to mobile data, without reconnecting.
- The browser only learns that a server supports HTTP/3 from the
Alt-Svcheader the server sends (or from a record in DNS). So the first visit is usually still HTTP/2, and later visits use HTTP/3.
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:
- Domain sharding: spreading files over
img1.myshop.comandimg2.myshop.comto get more connections. Every new domain needs its own DNS and handshake. In the Lab, HTTP/2 with sharding takes 880 ms, up from 628 ms. Even on HTTP/1.1, sharding doesn't help on this page (875 becomes 880 ms). - Sprites: joining all icons into one image. This isn't needed anymore, and when one icon changes, the whole sprite has to be downloaded again.
- Joining all the JavaScript into one giant file. A few medium-sized chunks (from code splitting) are fine on HTTP/2 and friendlier to the cache. But thousands of tiny files are still worse, because every request has its own cost.
🎯 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
- Still domain sharding on HTTP/2. The extra connections need DNS and handshakes, and the files are slower in the end.
- Relying on Server Push. It has been removed from Chrome and Firefox. Use preload or 103 Early Hints.
- Thinking HTTP/2 is free of head-of-line blocking. At the TCP level it's still there: one lost packet makes every stream wait. That's what HTTP/3 fixes.
- Turning on HTTP/3 but forgetting
Alt-Svcor the UDP port. The browser never learns that HTTP/3 is there, so it keeps using HTTP/2.
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.