Sub topic 14
HTTP/1.1, HTTP/2, and HTTP/3
This chapter assumes you can read an HTTP request and response, know what a header is, and understand that HTTP runs on top of TCP and TLS. It covers how the three major versions of HTTP differ on the wire, why those differences exist, and how to tell which version a connection uses.
What stays the same
All three versions carry the same ideas: methods, URLs, status codes, headers, and bodies. A GET that returns 404 means the same thing in each. Server code written against a framework rarely changes between versions, because the web server or proxy usually handles the version-specific parts.
What changes is how those messages are encoded and how they travel between client and server. The versions differ in three areas:
- Encoding: text in HTTP/1.1, binary frames in HTTP/2 and HTTP/3.
- Concurrency: how many requests can be in flight on one connection.
- Transport: TCP for HTTP/1.1 and HTTP/2, QUIC (built on UDP) for HTTP/3.
HTTP/1.1: text messages, one response at a time
HTTP/1.1 messages are plain text, as in the earlier chapters. Its main improvement over HTTP/1.0 is the persistent connection. The TCP connection stays open after a response, so the browser skips a new handshake for each request. A required Host header lets many sites share one IP address.
The limit is that responses on one connection must come back in the order the requests were sent. If a slow response is first, everything behind it waits. This is called head-of-line blocking. HTTP/1.1 defined pipelining, which lets a client send several requests without waiting. Browsers disabled it because broken proxies and the in-order rule made it unreliable.
Browsers worked around the limit by opening several connections to the same host, usually up to six. Developers added their own workarounds:
- Spreading assets across several hostnames, called domain sharding.
- Bundling files together.
- Combining small images into one sprite sheet.
Each extra connection costs a TCP handshake, a TLS handshake, and a separate slow-start phase for congestion control.
HTTP/1.1 also repeats headers in full on every request. Cookies and User-Agent can add hundreds of bytes to each one.
HTTP/2: many streams on one connection
HTTP/2 keeps the semantics and changes the encoding. A message is split into binary frames. Each frame carries a stream identifier, and a stream is one request and response pair. Frames from many streams are interleaved on a single TCP connection. This is called multiplexing.
Several things follow from this:
- A slow response no longer blocks a fast one at the HTTP level. Frames from other streams continue to flow.
- One connection per origin is enough. Domain sharding becomes counterproductive, because it forces extra connections and splits prioritization.
- HPACK compresses headers. Client and server keep a shared table of headers already sent, so a repeated
Cookieheader costs a few bytes. - Header names are lowercase, and the request line becomes pseudo-headers:
:method,:scheme,:authority(replacingHost), and:path. The response carries:status. There is no reason phrase such as "OK".
Browsers only use HTTP/2 over TLS. The client and server agree on it during the TLS handshake with an extension called ALPN (Application-Layer Protocol Negotiation). The client offers h2 and http/1.1, and the server picks one. No separate upgrade request is needed.
HTTP/2 also defined server push, where the server sends resources before the client asks. It proved hard to use well, and Chrome removed support. Do not design around it.
The remaining problem: TCP head-of-line blocking
HTTP/2 removed blocking at the HTTP layer, but TCP delivers one ordered byte stream. If one packet is lost, TCP holds back every later byte until the missing packet is retransmitted, including bytes belonging to unrelated streams. On a lossy network, such as mobile or weak Wi-Fi, one HTTP/2 connection can perform worse than several HTTP/1.1 connections.
HTTP/3: HTTP over QUIC
HTTP/3 fixes this by replacing TCP. It runs on QUIC, a transport protocol built on UDP. QUIC implements its own reliability, congestion control, and streams, and it builds TLS 1.3 into the handshake.
- Independent streams: QUIC tracks loss per stream. A lost packet stalls only the stream it belongs to.
- Faster setup: transport and encryption are negotiated together, so a new connection needs one round trip. A client that has connected before can send data in the first flight (0-RTT). 0-RTT data can be replayed by an attacker, so servers should only accept it for safe requests.
- Connection migration: a QUIC connection is identified by a connection ID, not by an IP address and port pair. A phone that moves from Wi-Fi to cellular can keep the same connection.
- QPACK replaces HPACK. It has the same goal but is designed so that header compression does not reintroduce blocking between streams.
QUIC runs in user space, not in the operating system kernel, so it is easier to update. It is also more CPU-intensive than TCP, and some networks block or throttle UDP on port 443.
How a browser chooses a version
A browser does not know in advance that a server speaks HTTP/3. The usual sequence is:
- The browser connects over TCP and TLS, negotiating
h2orhttp/1.1with ALPN. - The server announces HTTP/3 support in a response header.
- The browser remembers this and tries QUIC on later visits, typically racing it against TCP.
- If QUIC fails, the browser falls back to HTTP/2 without the user noticing.
The announcement is the Alt-Svc header:
HTTP/2 200
content-type: text/html; charset=utf-8
alt-svc: h3=":443"; ma=86400This says that the same host offers HTTP/3 on UDP port 443, and the browser may cache that for 86400 seconds. Servers can also publish this information in DNS records of type HTTPS, which lets a browser try HTTP/3 on the first visit.
Comparing the versions
| Feature | HTTP/1.1 | HTTP/2 | HTTP/3 |
|---|---|---|---|
| Encoding | Text | Binary frames | Binary frames |
| Transport | TCP | TCP | QUIC over UDP |
| Concurrent requests per connection | One at a time | Many streams | Many streams |
| Header compression | None | HPACK | QPACK |
| Blocking from packet loss | Whole connection | Whole connection | Single stream |
| Encryption | Optional | Required in browsers | Always (TLS 1.3) |
Checking which version you get
curl can force a version and report the one it used. The %{http_version} variable is printed after the transfer.
curl -s -o /dev/null -w "%{http_version}\n" --http1.1 https://example.com/
curl -s -o /dev/null -w "%{http_version}\n" --http2 https://example.com/The first command prints 1.1. The second prints 2 if the server supports HTTP/2. HTTP/3 requires a curl build with QUIC support, and you can test it with --http3 if yours has it. In browser DevTools, the Network panel has a Protocol column that shows http/1.1, h2, or h3. Right-click the column headers to enable it.
You can also speak HTTP/2 from code. This Node.js client uses the built-in node:http2 module and sends the pseudo-headers directly:
import { connect } from "node:http2";
const client = connect("https://example.com");
const request = client.request({ ":method": "GET", ":path": "/" });
request.on("response", (headers) => {
console.log("status:", headers[":status"]);
console.log("content-type:", headers["content-type"]);
});
request.setEncoding("utf8");
let body = "";
request.on("data", (chunk) => {
body += chunk;
});
request.on("end", () => {
console.log("body length:", body.length);
client.close();
});
request.end();Common errors and gotchas
- Assuming HTTP/2 is faster everywhere. It helps most with pages that load many small resources. A single large download gains little, and a lossy network can make HTTP/2 slower than HTTP/1.1.
- Keeping HTTP/1.1 optimizations. Domain sharding and heavy bundling can hurt under HTTP/2 and HTTP/3. Test before removing bundling, since many small files still carry per-request overhead.
- Checking only the origin server. A CDN or load balancer often terminates the client connection. The browser may speak HTTP/2 to the proxy while the proxy speaks HTTP/1.1 to your application. Your app's logs may show a different version than the browser's.
- Case-sensitive header handling. HTTP/2 and HTTP/3 require lowercase header names. HTTP/1.1 header names are case-insensitive, but code that compares them exactly can break when a proxy converts between versions.
- Blocked UDP. If HTTP/3 never appears, check whether a firewall blocks UDP port 443. Browsers fall back silently, so the failure shows up only as the protocol staying at
h2.
Exercise
A page loads 80 small images from one origin. Under HTTP/1.1 the browser opens six connections. Explain why HTTP/2 can load the page with one connection, and name the problem that remains on a lossy network.
Show answer
HTTP/2 splits each request and response into frames tagged with a stream ID and interleaves them on one TCP connection, so many requests are in flight at once without waiting for earlier responses. The remaining problem is TCP head-of-line blocking. A single lost packet stalls all streams on that connection until it is retransmitted, because TCP delivers one ordered byte stream. HTTP/3 avoids this by running on QUIC, which tracks loss per stream.
Exercise
Your server sends Alt-Svc: h3=":443"; ma=86400, but DevTools still shows h2 for every request on a corporate network. Give two possible causes.
Show answer
The network may block or drop UDP on port 443, so QUIC fails and the browser falls back to HTTP/2. Alternatively, a proxy or CDN in the path may not support HTTP/3, or the browser has not yet visited the site since receiving the header, because it only tries QUIC on later connections.