One window, two jobs
Picture a museum's ticket window. A clerk sits behind it. In front, a visitor has asked for two things: a map printed on four sheets, and the opening hours printed on two. The window is a narrow slot. Only one sheet fits through at a time.
The clerk could pass all four map sheets first. The visitor would then wait until the sixth sheet for the hours. Instead, the clerk slips a sheet of hours between two sheets of map. A heading on each sheet says which pile it belongs to, so the visitor can sort them. Watch when the hours are complete.
Opening a museum website works the same way. Your browser is the visitor. The computer that holds the site, called a server, is the clerk. The window is the one connection between them. HTTP/2 is the set of rules that lets the two replies take turns on it.
One difference: no paper crosses a real connection, only a long row of bytes. So the heading has to be bytes too. The rest of this article follows the map and the hours through that connection, one step at a time.
A request and a reply
Start with a single exchange. To show you the opening hours, your browser
sends the server a short message called a request. It
names the page it wants, /hours, and the site it is talking
to.
The server answers with a response. Its first line is a
status: 200 OK means “here it is”. Then come
headers, lines of the form name: value that
describe the reply, such as its type and its size. After an empty line
comes the body: the page itself.
In HTTP/1.1, the version before HTTP/2, all of this is plain text. You
could type the request by hand. In this example the hours page is 32,768
bytes long, the number in its Content-Length header. The map
is twice that.
One reply at a time
Now the browser wants both: first the map, then the hours. HTTP/1.1 lets it send both requests on the same connection. But the server must send the responses back in the order the requests arrived. Every byte of the map has to cross before the first byte of the hours.
Count time in turns. In one turn the connection carries one equal piece of data. The map is four pieces, the hours two. When are the hours complete?
The short reply waits four turns behind the long one. Engineers call this head-of-line blocking: whatever is first in line holds up everything behind it. Browsers work around it by opening several connections to the same site, one reply on each. Every extra connection costs time to set up and competes with the others for the same network.
Cut into frames
HTTP/2 keeps a single connection and changes what travels on it. A reply is no longer sent as one long piece of text. It is cut into small pieces called frames, the sheets of the museum.
Every frame starts with the same 9-byte header, the heading on the sheet. It has four fields: the length of what follows, the frame's type, a few flags, and a stream identifier, the number of the reply the frame belongs to. One bit, marked R, is reserved and always 0. Watch the hours body become frames.
A frame may carry at most 16,384 bytes unless the browser allows larger
ones, so the 32,768-byte body becomes two DATA frames. The
status and headers travel first, in a frame of type
HEADERS. The last DATA frame sets the
END_STREAM flag: nothing more is coming for this reply.
Reading the nine bytes
The last hours frame starts 00 40 00 00 01 00 00 00 03.
The first three bytes are the length: hexadecimal 4000, which is
4 × 4,096 = 16,384. Then the type, 00 for DATA
(01 would be HEADERS). Then the flags, 01 for
END_STREAM. The last four bytes hold the reserved bit, 0, and the stream
identifier, 3.
Sorting by stream
A request and its response share one stream number. The browser picks it when it sends the request, counting up through the odd numbers. Here the map is stream 1 and the hours stream 3. Even numbers are kept for streams the server starts, and 0 for the connection itself.
The frames of both replies arrive mixed together on the one connection. The browser reads the stream identifier in each header and adds the payload to the right reply. Predict where each frame goes.
Within one stream, frames arrive in the order they were sent. The browser only ever adds a payload to the end of its stream. It never has to put the pieces of the map back in order.
Taking turns
Now the server chooses the order. It could still send the whole map first, exactly like HTTP/1.1. Or it can alternate between the two streams. Both schedules use the same six turns of the same connection. Which one finishes the hours sooner?
Taking turns gets the hours to you at turn 4 instead of turn 6. The map finishes two turns later, at turn 6 instead of turn 4. Nothing got faster: the connection carried the same six frames. Sharing one connection between several replies this way is called multiplexing.
Headers sent once
Frames fix the waiting. HTTP/2 also stops repeating itself. Every request carries headers, and most are the same each time: the site's name, the browser's name.
In HTTP/2 the request line becomes headers too, with names that start with
a colon: :method, :scheme,
:authority for the site and :path for the page.
A method called HPACK then packs the list. Common pairs
such as :method GET already have a number in a fixed table of
61 entries. New pairs can be added to a second table that the browser and
the server both keep, numbered from 62.
The first request sends the site and browser names in full and adds them
to the table. The second request points at them with one byte each. Its
headers shrink from 39 bytes to 12. Only the path, /hours, is
new.
The bytes of the second request
82 is entry 2, :method GET, and
87 is entry 7, :scheme https: a first bit of 1
means “take this entry”, the other seven bits give its number.
bf is entry 63, the site, and be is entry 62,
the browser: the newest entry always gets 62. The path is written out:
04 (name from entry 4, value not stored), 06
for six characters, then /hours. Real browsers also
compress written-out text with a Huffman code, so their bytes are fewer
still.
One lost piece
One problem remains, underneath HTTP/2. The connection itself is run by TCP, the delivery service that carries the bytes. TCP gives the browser one ordered strip of bytes. If part of the strip goes missing on the way, TCP asks for it again and holds back everything after it until the gap is filled.
In the drawing, some bytes of the second map frame are lost. The second hours frame arrives after them, whole. Can the browser use it?
It can't. TCP knows nothing about streams; it only sees bytes in order. The hours wait for map bytes they don't need. This is head-of-line blocking again, one layer down: HTTP/2 removed it between replies, but not inside TCP.
How we got to HTTP/2
HTTP stands for Hypertext Transfer Protocol. Tim Berners-Lee wrote the first version at CERN in 1991 so computers could fetch linked documents. It asked for one file per connection, which was fine for a page of text.
Pages grew to dozens of images, scripts and styles. HTTP/1.1, from 1997, kept a connection open for several requests, but the replies still came back in order, so browsers opened a handful of connections side by side. In 2009 Google announced SPDY, an experiment that let many replies share one connection and compressed their headers. The IETF, the group that maintains internet standards, built HTTP/2 and HPACK from it and published both in 2015.
The lost-piece problem led to the next step. QUIC gives every stream its
own ordered strip, so a lost piece only holds up its own reply. HTTP/3 runs
on QUIC and was published in 2022. Your browser now speaks HTTP/2 or HTTP/3
with most large sites. To see it, open the developer tools in Chrome or
Firefox, go to the Network panel and look at the Protocol column: Chrome
shows h2 or h3 for each file.
Filed under Networking