Workspace

HTTP/2: frames, streams and head-of-line blocking

3 day streakD

HTTP/2: frames, streams and head-of-line blocking

HTTP/2 sends many requests over one connection as interleaved binary frames. Learn the framing, HPACK compression and flow control, and the TCP-level blocking HTTP/2 could not fix.

DepthLesson 4 of 622 min

HTTP/1.1 sends one request at a time per connection. A slow response blocks everything behind it on that connection: application-level head-of-line blocking. Browsers opened six connections per origin, and sites sharded assets across domains, just to get parallelism.

HTTP/2 keeps HTTP's semantics but replaces the text format with binary frames. Every request and response becomes a stream with an ID, and frames from many streams interleave on one connection. A slow response no longer blocks others at the HTTP level.

FramePurpose
HEADERSStarts a stream; carries compressed headers
DATABody bytes for one stream
SETTINGSConnection parameters, such as max concurrent streams
WINDOW_UPDATEFlow control: permission to send more
RST_STREAMCancel one stream without closing the connection
GOAWAYGraceful shutdown: no new streams after this ID
PINGLiveness and RTT measurement

Each frame has a 9-byte header: 24-bit length, 8-bit type, 8-bit flags, and a 31-bit stream ID. Client-initiated streams use odd IDs. Because frames are length-prefixed, the receiver always knows where each one ends, the framing lesson from Module 4 applied.

HPACK header compression

Request headers repeat almost entirely between requests: the same host, user agent, cookies and accept headers. HPACK compresses them with three tools: a static table of 61 common header fields, a dynamic table of headers this connection has already sent, and Huffman coding for literal strings. A repeated header shrinks to a single byte index.

Worked exampleA repeated header in HPACK
First request sends
authorization: Bearer eyJ… (about 900 bytes), and adds it to the dynamic table at index 62
Second request sends
One byte: “indexed header field 62”
Saved per request
~899 bytes

Over 1,000 requests on one connection, nearly 1 MB of header bytes is never sent.

The block HTTP/2 could not remove

Figure 4.1

One lost packet, three streams

Stream A completes at time 4. Stream B completes at time 13. Stream C completes at time 13Stream AStream BStream CA1B1C1A2B2C2B3C3B4C4time slots → · B2 is lost and retransmitted 8 slots later · lines show packets held after arriving
Stream A complete at
slot 4
Stream B complete at
slot 13
Stream C complete at
slot 13

Streams interleave, but TCP delivers bytes in order: every packet after B2, from any stream, waits for the retransmission. C finishes late though none of its packets were lost.

Three responses share a connection and one packet carrying stream B is lost. Switch protocols: HTTP/1.1 serialises the responses; HTTP/2 interleaves them but TCP holds back every later byte until the loss is repaired; HTTP/3 stalls only stream B.

Streams are independent to HTTP/2, but TCP sees one byte stream. When a packet is lost, TCP holds back every later byte, whichever stream it belongs to, until the retransmission arrives. On a clean data-centre network that is rare and cheap. On a mobile link with 2% loss, a single HTTP/2 connection can perform worse than six HTTP/1.1 connections.

Flow control

HTTP/2 adds its own flow control on top of TCP's, per stream and per connection. A receiver grants credit with WINDOW_UPDATE frames. This lets one slow consumer, such as a stalled download, stop without starving every other stream on the connection.

Go deeperRapid Reset: a protocol feature as an attack

In October 2023, attackers exploited a simple pattern: open a stream with HEADERS, immediately cancel it with RST_STREAM, repeat. The client stays within its concurrent-stream limit because each stream is closed at once, but the server has already started work for every request. Record-breaking request floods followed (CVE-2023-44487).

Servers now count resets and close connections that cancel too many streams. The lesson generalises: any cheap-for-client, expensive-for-server operation in a protocol needs a budget. Agent tool servers face the same shape when a client can start and cancel expensive tool runs freely.

Where you will meet it

gRPC

gRPC maps each call to an HTTP/2 stream, uses trailers for status, and cancels calls with RST_STREAM.

Server push retired

HTTP/2 server push was removed from major browsers because it rarely helped. 103 Early Hints replaced it: the server suggests what to preload.

Domain sharding reversed

Under HTTP/2, splitting assets across domains hurts, because each domain needs its own connection and handshake.

Words to know

Stream
One request-response exchange inside an HTTP/2 or HTTP/3 connection.
Frame
HTTP/2's unit on the wire: 9-byte header plus payload.
HPACK
HTTP/2 header compression with static and dynamic tables.
Head-of-line blocking
A stalled item at the front of a queue delaying everything behind it.

Check yourself

Answer out loud before you open each one.

Head-of-line blocking exists at which layers?

HTTP/1.1 at the application layer, TCP at the transport layer under HTTP/2, and QPACK dynamic-table references under HTTP/3.

What does HPACK's dynamic table store?

Header fields already sent on this connection, so repeats can be sent as small indexes.

Why can HTTP/2 be slower than HTTP/1.1 on a lossy link?

One lost packet blocks every stream on the single TCP connection, while six HTTP/1.1 connections lose independently.

Try it

Run curl -sv --http2 https://example.com -o /dev/null 2>&1 | grep -i 'alpn\|http/2'. Then use nghttp -nv https://example.com if you have it, to see individual frames.

A good place to start

Explore a pattern

A little guidance

Start with a pattern, read through a problem, and mark it solved when the idea clicks. Save anything you want to revisit.

New tutorials are added one at a time. Your bookmarks and solved checkmarks stay in this browser.

Explore the roadmap