One page is many files
Loading a web page is not one request. The HTML arrives and refers to stylesheets, scripts, images and fonts, and each of those is a separate request. A typical page needs dozens.
HTTP is the protocol for asking for those files. The requests themselves have barely changed in thirty years: GET /style.css meant the same thing in 1997. What has changed, twice, is how the requests are carried over the network. Each new version exists to fix the main bottleneck of the one before.
index.htmlstyle.cssapp.jslogo.svghero.jpgfont.woff- waiting for the answer to start
- data arriving
- held up by a lost packet
Start. One request at a time. The next file cannot be requested until the previous answer has fully arrived. Pale means waiting for the request to cross the network and the answer to start.
- Time
- 0 ticks
- Files finished
- 0 / 6
HTTP/1.1: one at a time
On a single connection, HTTP/1.1 is strictly a conversation: ask, wait for the complete answer, then ask for the next thing.
index.htmlstyle.cssapp.jslogo.svghero.jpgfont.woff- waiting for the answer to start
- data arriving
- held up by a lost packet
Start. One request at a time. The next file cannot be requested until the previous answer has fully arrived. Pale means waiting for the request to cross the network and the answer to start.
- Time
- 0 ticks
- Files finished
- 0 / 6
Look at how much of each row is pale. Most of the time is not spent transferring data. It is spent waiting for a request to travel there and the answer to start coming back, six separate times, one after another.
Browsers work around this by opening several connections to the same site at once, usually up to six. That helps, and each extra connection costs its own setup and competes with the others. Developers added tricks of their own: gluing many scripts into one file, or many images into one large image, purely to make fewer requests.
HTTP/2: everything at once, on one connection
HTTP/2 keeps a single connection and lets many requests share it. Each request gets a numbered stream, and the pieces of different answers are interleaved on the wire. This is called multiplexing.
index.htmlstyle.cssapp.jslogo.svghero.jpgfont.woff- waiting for the answer to start
- data arriving
- held up by a lost packet
Start. All six requests are sent at once over a single connection, and the answers are interleaved. Pale means waiting for the request to cross the network and the answer to start.
- Time
- 0 ticks
- Files finished
- 0 / 6
All six requests leave together, so the waiting happens once instead of six times. In the model, the page finishes in 5 ticks where HTTP/1.1 took 18.
HTTP/2 also sends headers in a compact binary form and avoids repeating identical headers on every request.
The problem HTTP/2 could not fix
HTTP/2 runs on top of TCP, and TCP guarantees that bytes are delivered in order. That guarantee applies to the connection as a whole. TCP has no idea that the bytes belong to six unrelated files.
So what happens when one packet is lost?
index.htmlstyle.cssapp.jslogo.svghero.jpgfont.woff- waiting for the answer to start
- data arriving
- held up by a lost packet
Start. As before, but one packet belonging to app.js is lost on the way. Pale means waiting for the request to cross the network and the answer to start.
- Time
- 0 ticks
- Files finished
- 0 / 6
A single packet of app.js goes missing. TCP refuses to deliver anything that came after it until the missing piece is resent. The images and the font are stuck, although every one of their packets arrived safely.
One loss stalls every stream. This is head-of-line blocking, and on a poor mobile connection it can make HTTP/2 worse than several HTTP/1.1 connections, where a loss only stalls one of them.
HTTP/3: independent streams
The fix could not be made inside TCP. So HTTP/3 replaces it. It runs on QUIC, a newer transport built on top of UDP, which understands streams.
In QUIC, each stream is delivered in order separately. A lost packet holds up only the stream it belongs to.
index.htmlstyle.cssapp.jslogo.svghero.jpgfont.woff- waiting for the answer to start
- data arriving
- held up by a lost packet
Start. The same lost packet, on a connection where each file is an independent stream. Pale means waiting for the request to cross the network and the answer to start.
- Time
- 0 ticks
- Files finished
- 0 / 6
The same packet is lost. app.js waits for the resend. The other five files are not affected and finish on time.
QUIC brings two more improvements:
- Faster setup. TCP followed by TLS needs separate round trips before the first request. QUIC combines connection setup and encryption into one, and can skip it almost entirely when reconnecting to a site it has visited before.
- Connections survive a change of network. Walk out of Wi-Fi range onto mobile data and a TCP connection dies, because your address changed. A QUIC connection has its own identifier and simply continues.
Side by side
| HTTP/1.1 | HTTP/2 | HTTP/3 | |
|---|---|---|---|
| Runs on | TCP | TCP | QUIC, over UDP |
| Requests per connection | One at a time | Many, interleaved | Many, interleaved |
| Format | Text | Binary | Binary |
| A lost packet stalls | That connection | Every stream on the connection | Only its own stream |
| Encryption | Optional | Required in practice | Always, built in |
| Setup before the first request | TCP, then TLS | TCP, then TLS | One combined step |
What this means for you
You rarely choose a version by hand. Browsers and servers negotiate the best one both support, and fall back automatically. What changes is the advice that follows from it:
- With HTTP/2 or 3, many small files are fine. The old habit of bundling everything into one giant file to save requests matters much less.
- Splitting a site across many domain names was a trick to get more HTTP/1.1 connections. Now it works against you, because each domain needs its own connection.
- Enabling HTTP/2 and HTTP/3 is usually a server or CDN setting, with no change to your application.
Key takeaways
- A page is many requests, and the versions of HTTP differ in how they are carried.
- HTTP/1.1 handles one request at a time per connection, so the waiting adds up.
- HTTP/2 sends many requests at once over one connection.
- Because HTTP/2 runs on TCP, one lost packet stalls every stream.
- HTTP/3 runs on QUIC, where a lost packet only delays the stream it belongs to.