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 a TCP connection. It covers how the three major HTTP versions move the same messages across the network in different ways.
What stays the same
The meaning of HTTP does not change between versions. A request still has a method, a target, headers, and an optional body. A response still has a status code, headers, and an optional body. Your server code and your fetch calls look the same in all three versions. What changes is how those messages are encoded and carried between client and server.
HTTP/1.1: text over one request at a time
HTTP/1.1 sends messages as plain text, as in the earlier examples in this tutorial. It has persistent connections: the browser keeps a TCP connection open and reuses it for several requests instead of reconnecting each time.
The limit is that responses on one connection come back in order. If a slow response is first, everything behind it waits. This is called head-of-line blocking. Browsers work around it by opening several connections to the same host, commonly up to six. Developers also bundled files together to reduce the number of requests.
HTTP/2: many requests on one connection
HTTP/2 keeps the same semantics but changes the encoding in three ways:
- Binary framing. Messages are split into small binary frames instead of text lines.
- Multiplexing. Frames from many requests and responses are interleaved on one TCP connection. Each request gets its own numbered stream, so a slow response no longer blocks the others at the HTTP level.
- Header compression (HPACK). Headers repeated across requests, such as
CookieandUser-Agent, are sent in a compact form.
HTTP/2 is used in practice over TLS. Browsers negotiate it during the TLS handshake using ALPN (Application-Layer Protocol Negotiation).
HTTP/2 still has a problem at the TCP level. TCP delivers bytes in order, so one lost packet stalls every stream on the connection until it is retransmitted.
HTTP/3: HTTP over QUIC
HTTP/3 replaces TCP with QUIC, a transport built on UDP. QUIC includes TLS 1.3 and handles streams itself, so a lost packet delays only the stream it belongs to. Connection setup is faster because the transport and encryption handshakes are combined. QUIC connections are identified by a connection ID, so they can survive a change of network, such as moving from Wi-Fi to mobile data.
Seeing the version in use
Ask curl which version it used:
curl -sI --http2 -o /dev/null -w "%{http_version}\n" https://example.com/It prints 2 if the server negotiated HTTP/2. In browser DevTools, the Network panel can show a Protocol column with http/1.1, h2, or h3.
Common mistakes
- Assuming bundling is always good. Under HTTP/2, many small files are cheap, and bundling can hurt caching because one change invalidates the whole bundle.
- Expecting HTTP/3 to work everywhere. Some networks block UDP, so clients fall back to HTTP/2 or HTTP/1.1. Servers announce HTTP/3 support with the
Alt-Svcheader. - Looking for plain text in packet captures. HTTP/2 and HTTP/3 are binary, so use curl
-vor DevTools.
Exercise
A page loads 80 small files from one host. Why is this slow on HTTP/1.1 and faster on HTTP/2? Why can HTTP/2 still stall when a packet is lost?
Show answer
HTTP/1.1 returns responses in order on each connection and browsers cap parallel connections per host, so requests queue. HTTP/2 multiplexes all requests as streams on one connection. It still runs on TCP, which delivers bytes in order, so a lost packet pauses every stream until it is retransmitted. HTTP/3 avoids this by using QUIC, where loss affects only one stream.