Notes

Inside HTTP/2

How can a short download get past a long one?

You open a museum website for its hours. A big map is downloading too. Follow the two replies through one connection and see when the hours become readable.

Overview: a browser and a server joined by one connection. Frames of the map (stream 1) and the hours (stream 3) take turns on it.BrowserYOUServerMUSEUM.EXAMPLEONE CONNECTIONFRAMES OF BOTH REPLIES, TAKING TURNS1M13H11M23H21M31M4M = /MAP · STREAM 1H = /HOURS · STREAM 3

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.

A ticket window that fits one sheet. 0 of 6 sheets have passed.CLERK · NEXT SHEET ON THE RIGHTWINDOWONE SHEET AT A TIMEVISITORM1H1M2H2M3M4MAP PILE0 / 4M1M2M3M4HOURS PILE0 / 2H1H2EACH SHEET HAS A HEADING: M FOR MAP, H FOR HOURS
The clerk passes six sheets in this order: M1 H1 M2 H2 M3 M4. Only one fits through the window at a time.

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.

The browser sends GET /hours. The server answers 200 OK with two headers and a body of 32,768 bytes.BrowserServerREQUESTRESPONSEREQUEST · BROWSER TO SERVERGET /hours HTTP/1.1Host: museum.exampleUser-Agent: Mozilla/5.0(empty line: no body)REQUEST LINEHEADERSRESPONSE · SERVER TO BROWSERHTTP/1.1 200 OKContent-Type: text/htmlContent-Length: 32768<!doctype html><h1>Opening hours</h1>…… 32,768 bytes in allSTATUS LINEHEADERSBODY
The request is three lines of text. The reply adds a status, headers and the 32,768-byte page.

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?

HTTP/1.1 sends the replies in request order. The map takes turns 1 to 4, so the hours are complete at turn 6.REQUESTS, IN ORDER1 · GET /map2 · GET /hoursTURN123456RepliesONE CONNECTIONM1M2M3M4H1H2HOURS WAIT 4 TURNSHOURS · WAITING
Each box is one turn of the connection, one equal piece. The map is four pieces, the hours two.

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.

The 32,768-byte hours body is cut into 2 DATA frames of 16,384 bytes, each behind a 9-byte header. Shown: frame 2, 00 40 00 · 00 · 01 · 00 00 00 03.HOURS RESPONSE BODY · /HOURSHTML page32,768 BYTESDATA FRAME 1HEADERpayload · part 19 bytes16,384 BYTESDATA FRAME 2 · LASTHEADERpayload · part 29 bytes16,384 BYTESLENGTH16,38424 bitspayload bytesTYPE08 bitsDATAFLAGS18 bitsEND_STREAMR01STREAM IDENTIFIER331 bitsthe hoursOn the wire: 00 40 00 · 00 · 01 · 00 00 00 039 BYTES · 72 BITS
Frame 2 is the last: its flags byte is 01, END_STREAM. Length 16,384, type 0 for DATA, stream 3.

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.

Frames arrive mixed on one connection. The browser reads the stream number in each header and appends the payload to stream 1 (map) or stream 3 (hours). 0 of 6 sorted.ARRIVING ON THE CONNECTION, IN ORDER1M13H11M23H21M31M4STREAM IDStream 1/MAP · 4 FRAMESStream 3/HOURS · 2 FRAMES
The first frame's header says stream 1. Predict where it goes, then step.

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?

Two schedules for the same six frames. Map first: hours complete at turn 6. Taking turns: hours complete at turn 4.TURN123456Map firstHTTP/1.1 ORDERM1M2M3M4H1H2HOURS · WAITINGMAP · WAITINGTake turnsHTTP/2M1H1M2H2M3M4HOURS · WAITINGMAP · WAITING
Same connection, same six frames, two orders. Which one finishes the hours first?

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.

Request 1 sends its headers in 37 bytes and adds two entries to the table. Request 2 points at them with one byte each: 12 bytes instead of 39.REQUEST 1 · STREAM 1 · /MAP:method GET1:scheme https1:authority museum.example16:path /map6user-agent Mozilla/5.013HEADER BLOCK37 bytesREQUEST 2 · STREAM 3 · /HOURS:method GET1:scheme https1:authority museum.example1:path /hours8user-agent Mozilla/5.01HEADER BLOCK12 bytes39 without the tableTABLE BOTH SIDES KEEPFIXED · ENTRIES 1–611:authority (name only)2:method GET4:path /7:scheme https58user-agent (name only)ADDED · FROM 6262user-agent Mozilla/5.063:authority museum.examplewritten by request 1,read by request 2
Request 2 repeats the site and browser names. Each becomes a one-byte pointer into the table: 12 bytes instead of 39.

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?

Four frames are on their way, one after the other on the same TCP strip: M1, H1, M2, H2.TCP · ONE ORDERED STRIP OF BYTESM3, M4 FOLLOWM1STREAM 1ON ITS WAYH1STREAM 3ON ITS WAYM2STREAM 1ON ITS WAYH2STREAM 3ON ITS WAYSTREAM 1 · MAPwaitingSTREAM 3 · HOURSwaiting
Four frames are on their way, one after the other on the same TCP strip: M1, H1, M2, H2.

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.

How HTTP got here+6 y1991HTTP at CERNTim Berners-Lee, one file per connection+12 y1997HTTP/1.1connections stay open, replies come back in order+6 y2009SPDYGoogle shares one connection between replies+6 y2015HTTP/2 and HPACKframes, streams and shared header tables2021QUICa lost piece only holds up its own stream2022HTTP/3HTTP over QUICTodayh2 and h3 on most large sitessee the Protocol column in your browser's tools
Each version answers the bottleneck of the one before: one file at a time, then replies in order, then one lost piece holding up everything.

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