Highlights

Contents

Part V
05

HTTP/1.1, HTTP/2 and HTTP/3

We follow how the same request travels in each HTTP version and why each version was needed. The semantics never changed, but the cost of carrying them did, and that cost decides latency and connection counts in production. We will go from short-lived HTTP/1.0 connections through keep-alive and head-of-line blocking to HTTP/2 multiplexing and HTTP/3 on QUIC.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1HTTP messages2HTTP methods andidempotency3HTTP status codes4HTTP caching headers5HTTP/1.1, HTTP/2 andHTTP/36TLS and the handshake7Certificates and trust8DNS for backend engineers9The HTTPS request end toend

#Section 16HTTP/1.0 and HTTP/1.1 keep-alive

In HTTP/1.0 (1996) each request got its own TCP connection, and the server closed it after the response. Opening a connection costs time. TCP needs one round trip for its handshake, and TLS needs at least one more. At Wren’s 40 ms round-trip time, a phone paid 80 ms of handshakes before each 30 ms request. Three requests in a row cost three times that overhead.

HTTP/1.1 (1997) made connections persistent by default. After a response the connection stays open for the next request, which is called keep-alive. Three requests now share one handshake, and each skipped TCP plus TLS 1.3 setup saves 80 ms. A sequence of three requests drops from 450 ms to 290 ms. Clients keep a pool of open connections per host and reuse them. Servers close idle ones after a timeout. Nginx waits 75 seconds by default.

HTTP/1.0 PAYS A HANDSHAKE PER REQUEST; HTTP/1.1 KEEPS THE CONNECTIONHTTP/1.0 PAYS A HANDSHAKE PER REQUEST; HTTP/1.1 KEEPS THE CONNECTIONHTTP/1.0: open, ask, close, repeathandshakereqhandshakereqhandshakereqHTTP/1.1 keep-alive: one handshake, three requestshandshakewith a 40 ms round trip, each skipped TCP + TLS 1.3 handshake saves 80 ms
Figure 1.22Keep-alive spreads one handshake over many requests. Each request after the first starts immediately.

Keep-alive has a cost on the server side. Each open connection holds a socket and a file descriptor even when idle, plus some memory for buffers. A server with 50,000 idle keep-alive connections needs a file descriptor limit well above that, and Unit XI shows what happens when it runs out. The idle timeout is a trade-off. Too short, and clients pay new handshakes. Too long, and the server holds sockets for clients that left. Behind a load balancer there is one more rule. The backend’s idle timeout must be longer than the balancer’s, otherwise the backend may close a connection at the exact moment the balancer sends a request on it, and the user sees a 502.

#Section 17Pipelining and head-of-line blocking

Keep-alive still allows one outstanding request per connection. The client sends a request, waits for the whole response, and only then sends the next. HTTP/1.1 defined pipelining to remove the wait. The client sends several requests back to back, and the server answers them in the same order. If the first response is a slow report taking 900 ms, the three quick responses queued behind it also wait 900 ms, even though the server finished them long ago. That is head-of-line blocking, where one slow item at the front of a queue delays everything behind it.

HTTP/1.1: ONE RESPONSE AT A TIME PER CONNECTION, SO A SLOW ONE BLOCKS THE LINEHTTP/1.1: ONE RESPONSE AT A TIME PER CONNECTION, SO A SLOW ONE BLOCKS THE LINEconn 1conn 2conn 3conn 4conn 5conn 6/report: slow, 900 msbrowsers open about six connections per host; 30 resources need 5 rounds
Figure 1.23Six connections, one response at a time each. The slow request on connection 1 holds up the line behind it.

Pipelining was also buggy in many proxies, so browsers never turned it on by default. Their workaround was to open about six parallel connections per host. A page with 30 resources then needs ⌈30 / 6⌉ = 5 rounds instead of 30, and at 40 ms per round that is 200 ms instead of 1,200 ms. But six connections cost six handshakes, and each has its own TCP congestion window that starts small and grows independently. Sites invented tricks to get more parallelism. They spread assets across several host names, a trick called domain sharding, and glued many small images into one large one called a sprite. HTTP/2 made those tricks unnecessary, and in some cases harmful.

#Section 18HTTP/2: framing, streams, multiplexing and HPACK

HTTP/2 (2015, grown out of Google’s SPDY) kept the methods, headers and status codes and replaced the wire format. Messages are cut into binary frames. Each frame carries a stream id, and a stream is one request and its response. Frames from many streams interleave on a single connection, and the receiver reassembles each stream from its own frames. A slow response on stream 1 no longer blocks stream 3. This is multiplexing. One connection can carry a hundred concurrent requests, so a browser needs one connection per origin instead of six. Client-started streams use odd numbers, 1, 3, 5, and the server limits how many may be open at once with a setting that defaults to around 100 in most servers.

HTTP/2: STREAMS CUT INTO FRAMES AND INTERLEAVED ON ONE CONNECTIONHTTP/2: STREAMS CUT INTO FRAMES AND INTERLEAVED ON ONE CONNECTIONs1s3s5s1s3s1s5s3s3s1s5s5s3one TCP + TLS connectioneach frame carries a stream id; the receiver reassembles stream 3 on its ownclient streams use odd ids: 1, 3, 5, …
Figure 1.24Many streams, one connection. The receiver sorts frames back into streams by id.

Headers get their own compression, called HPACK. API requests repeat the same headers constantly. An authorization header might be 600 bytes, and it goes on every call. HPACK keeps a table of header fields on both sides. A static table of 61 common entries, such as :method: GET, is built in. New fields are added to a dynamic table as they are sent. The next request can then send a one-byte index instead of the full 600-byte header. Across 100 requests on one connection, that header costs 600 + 99 = 699 bytes instead of 60,000. HPACK avoids general-purpose compression on purpose. In 2012 the CRIME attack showed that compressing secrets alongside attacker-chosen text leaks those secrets through the compressed size.

HPACK: REPEATED HEADERS BECOME SMALL TABLE INDEXESHPACK: REPEATED HEADERS BECOME SMALL TABLE INDEXES:method: GET:path: /orders/123authorization: Bearer eyJ…user-agent: WrenApp/5.2first request: literal, then rememberedtable both sides keep62authorization: Bearer …63user-agent: WrenApp/5.22:method GET5:path /2/orders/1246263second request: mostly indexestokens sent once
Figure 1.25After the first request, repeated headers shrink to indexes into a shared table.

HTTP/2 also let clients assign stream priorities, so a browser could say the stylesheet matters more than an image. The original tree-based scheme was so complex that most servers ignored it. RFC 9218 replaced it with a simple urgency header that works for HTTP/2 and HTTP/3 alike.

ONE LOST TCP PACKET STALLS EVERY HTTP/2 STREAM BEHIND ITONE LOST TCP PACKET STALLS EVERY HTTP/2 STREAM BEHIND ITs1s3s5s1s3s5s1s3held until the retransmit arrives, because TCP delivers in orderstreams 1 and 3 wait for stream 5's lost packet
Figure 1.26TCP’s single ordered byte stream makes one lost packet everyone’s problem.

#Section 19HTTP/3 and QUIC

HTTP/3 (RFC 9114, 2022) runs on QUIC, a transport built on UDP. QUIC handles reliability, congestion control and streams itself, and it orders bytes per stream instead of per connection. A lost packet carrying stream 5’s data delays only stream 5. Streams 1 and 3 keep delivering. That removes the transport-level head-of-line blocking that HTTP/2 could not escape. QUIC needed its own header compression too, QPACK, because HPACK assumed in-order delivery.

HTTP/3 ON QUIC: LOSS ON STREAM 5 DELAYS ONLY STREAM 5HTTP/3 ON QUIC: LOSS ON STREAM 5 DELAYS ONLY STREAM 5stream 1delivered nowstream 3delivered nowstream 5waits for its retransmitQUIC runs over UDP and orders bytes per stream, not per connection
Figure 1.27Loss stays inside the stream it hit. That is the main reason HTTP/3 exists.

QUIC builds TLS 1.3 into its own handshake, so transport setup and encryption setup happen in the same round trip. A fresh HTTPS connection over TCP spends one round trip on TCP and one on TLS 1.3. QUIC spends one in total, which saves 40 ms at Wren’s round-trip time. With a previous session, QUIC can carry the request in its very first packet, called 0-RTT. Part VI explains the replay risk that comes with it.

QUIC also names each connection with a connection ID instead of the pair of IP addresses and ports. When a phone moves from Wi-Fi to cellular, its IP address changes, and a TCP connection dies, so the app needs new handshakes and loses any in-flight requests. A QUIC connection survives the move, because the server recognises the connection ID arriving from the new address. This is connection migration.

CONNECTION MIGRATION: A QUIC CONNECTION ID SURVIVES A NEW IP ADDRESSCONNECTION MIGRATION: A QUIC CONNECTION ID SURVIVES A NEW IP ADDRESSwifi 192.0.2.7Wren edgeconn id c9f2 from 192.0.2.7conn id c9f2 from 198.51.100.4 (cellular)TCP identifies a connection by addresses and ports, so the same switch kills it
Figure 1.28The connection ID outlives the network change. TCP would need a full new handshake.

Deployment has some rough edges. Servers advertise HTTP/3 with an Alt-Svc response header or a DNS HTTPS record, so the first visit usually happens over HTTP/2. QUIC encrypts almost all of its transport headers, so corporate firewalls and load balancers that inspect TCP cannot see into it, and some networks block UDP port 443. Clients race the two and fall back to TCP when QUIC fails.