HTTPhttp/2h2binary framingstreamsmultiplexing

HTTP/2: Streams on One Connection

HTTP/2 replaces text lines with binary frames tagged by stream id, so many requests and responses interleave on a single TCP connection with compressed headers — at the cost that one lost TCP segment now stalls every stream on that connection.

ConceptualBrowser
▶ InteractiveInterview question
Progress

You have not marked these as done yet. Read on anyway if they feel familiar; come back to them if something here does not land.

The problem

A page needs 80 resources from one origin. HTTP/1.1 gives six sequential connections, each paying its own handshake and slow start, and browsers spend more time queueing requests than transferring bytes. How do you carry 80 concurrent requests on one warm connection when responses have no way to say which request they belong to?

Frames and streams

HTTP/2 (RFC 9113, 2015 originally as RFC 7540) keeps HTTP’s semantics — the same methods, status codes and headers — and replaces the wire format. Everything is a frame: a 9-byte header carrying the payload length, a type, flags and a 31-bit stream identifier, followed by the payload. A request is a HEADERS frame (plus CONTINUATION if large) and optional DATA frames on a new stream; the response is HEADERS and DATA frames on the same stream, with the END_STREAM flag marking the end. Because every frame says which stream it belongs to, frames from different streams can be interleaved freely on one connection.

Streams are cheap: a client opens one per request by picking the next odd id (servers use even ids), and closes it with END_STREAM or aborts it with RST_STREAM — cancelling one download no longer means dropping a TCP connection. The server advertises SETTINGS_MAX_CONCURRENT_STREAMS (commonly 100–250); within that limit the browser sends every request the page needs the moment it knows about it, and the server answers in whatever order it produces results. Control frames (SETTINGS, PING, GOAWAY, WINDOW_UPDATE) travel on stream 0.

The connection begins with a fixed 24-byte preface (PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n) and a SETTINGS exchange; the protocol itself is chosen during the The TLS Handshake via ALPN (h2). Cleartext h2c with an Upgrade exists in the RFC and is used by some internal gRPC deployments, but browsers only speak HTTP/2 over TLS — in practice h2 requires TLS on the web.

Three requests, one connection: frames interleave by stream id
Browserstream 1: GET /index.htmlstream 3: GET /app.jsstream 5: GET /hero.jpgone TCP + TLS connection: HEADERS(1) HEADERS(3) HEADERS(5) DATA(3) DATA(1) DATA(5) DATA(3) …Server
UserLLMAgentToolDataDecisionHumanGuardrail

Header compression, prioritisation, push

Headers repeat: the same User-Agent, Accept, Cookie and Authorization go out on every request. HPACK (RFC 7541) compresses them with a 61-entry static table of common name/value pairs (:method GET, :status 200, accept-encoding gzip, deflate), a per-connection dynamic table where each side remembers headers it has seen, and Huffman coding for the rest. A repeated header becomes a one-byte index; a typical 700-byte request header block shrinks to tens of bytes. HPACK deliberately avoids general-purpose compression like gzip because CRIME showed that compressing attacker-controlled input next to a secret leaks the secret through the compressed size.

HTTP/2 also carries pseudo-headers (:method, :path, :scheme, :authority) instead of the request line, forces header names to lowercase and forbids Connection, Transfer-Encoding and other hop-by-hop headers — the framing handles what they used to.

Prioritisation in the original RFC let the client build a dependency tree with weights so a server could send CSS before images. Server support was inconsistent, some implementations ignored it, and RFC 9218 replaced the tree with a simpler priority header (urgency 0–7 plus an incremental flag) shared with HTTP/3. Server push — the server sending a PUSH_PROMISE and a resource the client had not asked for — is effectively dead: it was hard to use without wasting bandwidth on already-cached resources, Chrome removed support in 2022 and other browsers followed or never enabled it. 103 Early Hints with Link: rel=preload is the replacement: the server tells the client what to fetch and the client decides.

  • HPACK: static table + per-connection dynamic table + Huffman; a repeated header costs about one byte.
  • Pseudo-headers replace the request/status line; header names are lowercase; hop-by-hop headers are forbidden.
  • Prioritisation: RFC 7540 dependency tree superseded by RFC 9218 priority header; honoured to varying degrees.
  • Server push: specified, deprecated in practice — removed from Chrome; use 103 Early Hints + preload instead.

Flow control and the cost: one TCP stream underneath

Conceptual

Multiplexing forces HTTP/2 to have its own flow control: a fast stream must not starve a slow consumer of a different stream on the same connection. Each stream and the connection as a whole have a receive window (default 65,535 bytes) advanced with WINDOW_UPDATE frames — a layer of windows above TCP’s rwnd (Flow Control: The Receive Window). A client that forgets to grow its windows caps every download at 64 kB per RTT, which is a real and recurring bug in HTTP/2 client libraries.

The cost is that all streams share one TCP byte stream. TCP guarantees in-order delivery of the whole stream, so when one segment is lost, every byte after it — belonging to every stream — waits in the receiver’s buffer until the retransmission arrives. HTTP/2 removed the application-level head-of-line block of HTTP/1.1 and exposed the transport-level one underneath (Head-of-Line Blocking). On a clean network that is invisible; on a lossy mobile link measurements have shown HTTP/1.1 with six independent connections outperforming HTTP/2 with one, because a loss stalls one-sixth of the transfer instead of all of it. That observation is the motivation for HTTP/3 and QUIC.

A single long-lived connection has operational consequences too. A load balancer that balances *connections* sends all of one client’s requests to one backend, so Load Balancers: L4 vs L7 for h2 traffic must balance at the request (L7) level or accept imbalance. And cheap stream creation is an attack surface: the 2023 "Rapid Reset" attack (CVE-2023-44487) opened and immediately RST_STREAM-cancelled streams faster than servers could process them, producing record-size denial-of-service floods with modest client resources.

Key points

  • HTTP/2 keeps HTTP semantics and changes the wire format: binary frames with a stream id, so many requests interleave on one connection.
  • Streams are cheap and cancellable (RST_STREAM); the server caps concurrency with SETTINGS_MAX_CONCURRENT_STREAMS.
  • HPACK compresses headers with static and dynamic tables; a repeated header costs about a byte.
  • Prioritisation is now the RFC 9218 priority header; server push is deprecated in practice — use 103 Early Hints.
  • Browsers speak HTTP/2 only over TLS, negotiated with ALPN; h2c exists for internal use.
  • All streams share one TCP stream, so one lost segment stalls all of them: transport-level head-of-line blocking, which QUIC was built to address.
  • Per-stream and per-connection flow-control windows sit above TCP’s; forgetting to grow them throttles throughput.

Why does this exist?

Mechanisms are answers to constraints. Open each question before reading the answer.

▸Why binary framing rather than fixing text HTTP?

Interleaving needs an unambiguous, cheap-to-parse frame boundary and a stream id on every piece; text lines give neither. Fixed-size binary headers are parsed in a few instructions and have no precedence ambiguities.

▸Why a custom compression scheme instead of gzip?

CRIME and BREACH showed that a compressor sharing context between an attacker’s input and a secret leaks the secret by size. HPACK’s table-based scheme lets an attacker learn at most that a whole header matched, and implementations can refuse to index sensitive headers.

▸Why did server push fail?

The server cannot know what the client has cached, so it pushed bytes the client would discard; it competed with the client’s own priorities; and the win over preload hints was small. Telling the client what to fetch (103 Early Hints) keeps the decision where the cache is.

▸Why does HTTP/2 need its own flow control when TCP has one?

TCP’s window governs the whole connection; a single stream consumed slowly by the application would fill it and stall every other stream. Per-stream windows let the receiver throttle one stream without blocking the rest.

HTTP/2 streams

HTTP/2 streams on one connection
Four resources as interleaved HEADERS / DATA frames on a single TCP connection, versus HTTP/1.1 opening one connection per resource. One column = 10 ms.
Protocol
Simulated
stream 1 · index.html
stream 3 · app.css
stream 5 · app.js
stream 7 · hero.jpg
HEADERSDATA (darker = fuller frame)TCP + TLS setupstalled on a lost segment
t = 0 ms  (one connection)
HEADERS stream=1  len=180
HEADERS stream=3  len=40
HEADERS stream=5  len=40
HEADERS stream=7  len=40
Page complete (simulated)
HTTP/2 180 ms · HTTP/1.1 250 ms
Header bytes for 4 requests
HTTP/1.1 2080 B · HTTP/2 HPACK 300 B (86% saved)
HPACK sends a repeated header (Cookie, User-Agent, Accept…) once, then refers to it by a table index — a few bytes instead of a few hundred.
HTTP/1.1 connections 2–4 spend their first 80 ms on TCP + TLS and then start in slow start, so the big files arrive later than over the warm HTTP/2 connection. Press "Lose one TCP segment" to see the flip side of multiplexing.
1/26 · t = 0 ms

How it fails

What the failure looks like from inside real software.
Reference · show
  • Throughput capped around 64 kB per RTT: a client that never sends WINDOW_UPDATE beyond the default window; downloads are slow while the link is idle.
  • HTTP/1.1-era sharding across four hostnames under HTTP/2 now costs four TLS handshakes and four cold connections instead of one — the optimisation became a regression.
  • L4 load balancer plus HTTP/2: all requests from a busy client land on one backend; utilisation is lopsided and one backend saturates.
  • A lossy mobile link makes a single h2 connection slower than six h1 connections: transport head-of-line blocking dominates.
  • Rapid Reset (CVE-2023-44487): thousands of streams opened and cancelled per second overwhelm the server; mitigation is rate-limiting RST_STREAM and stream creation.
  • A proxy that speaks h2 to clients but h1 with a single connection to the origin serialises all streams behind it; the multiplexing is lost at the hop nobody looked at.

Check yourself

Answer out loud or on paper first, then compare. These come from the interview questions linked to this lesson.

  1. What did HTTP/2 fix over HTTP/1.1, what problem did it leave behind, and how does HTTP/3 address it? When would you choose not to deploy HTTP/3?

    Show a strong answer

    HTTP/1.1: Persistent Connections and Their Limits serialises: one request in flight per connection (pipelining never worked in practice), so browsers open ~6 connections per host and sites resorted to domain sharding, sprites and inlining. Headers are repeated as text on every request — hundreds of bytes of cookies per image. HTTP/2 (HTTP/2: Streams on One Connection) keeps HTTP’s semantics and changes the wire: a binary framing layer, many streams multiplexed over one TCP connection, HPACK header compression with a shared dynamic table, stream priorities, and (rarely useful, since removed from Chrome) server push. One connection means one handshake, one slow start and one congestion window instead of six.

    What it leaves behind is everything below it. All streams share a single ordered TCP byte stream, so one lost packet stalls every stream until retransmission — HTTP/2 concentrates the loss that HTTP/1.1 spread across six connections (Head-of-Line Blocking). The connection still costs a TCP round trip plus one or two for TLS. It is bound to a four-tuple, so a phone moving from Wi-Fi to cellular loses every in-flight stream. And TCP is implemented in kernels and inspected by middleboxes, so changing it (new options, new loss recovery) takes years to reach users — the protocol has *ossified*.

    HTTP/3 (HTTP/3 and QUIC) runs over QUIC, a transport in user space over UDP with TLS 1.3 built in. Streams are the unit of loss recovery, so a lost packet blocks only the streams it carried — cross-stream HOL is gone, intra-stream HOL is not. The transport and crypto handshakes merge into one round trip, 0-RTT on resumption. Connections are identified by a connection ID rather than the four-tuple, so they survive an address change. Packet numbers never repeat, which makes loss detection unambiguous, and being in user space means Chrome or a CDN can ship a congestion-control change in weeks. Header compression becomes QPACK, redesigned so a lost header packet does not necessarily block other streams.

    When *not* to: when UDP is blocked or throttled — some corporate networks and ISPs do exactly that — so every deployment must keep a TCP fallback and discovery is via Alt-Svc or the HTTPS DNS record, meaning the *first* visit is still TCP. When server CPU per byte matters: user-space packet handling without the offloads TCP enjoys (segmentation, checksum, kTLS) costs measurably more CPU, though UDP GSO has narrowed the gap. When observability and tooling matter — L4 load balancers must route on connection ID rather than four-tuple, firewalls and packet captures see opaque UDP, and existing dashboards are TCP-shaped. And when the network is already good: inside a datacenter or on a wired LAN with ~0 loss and sub-millisecond RTT, the wins are near zero; the gains are at the tail on lossy, high-latency, mobile paths. gRPC between services stays on HTTP/2 for good reason.

    Full interview question, with follow-ups and red flags →
  2. What is head-of-line blocking? Where does it occur in TCP, in HTTP/1.1 and in HTTP/2, and what does HTTP/3 actually change about it?

    Show a strong answer

    It is a queueing pattern: a shared, ordered queue in which the first element cannot be delivered, so nothing behind it is either — even if those later elements are complete and unrelated. The question is always *what is the queue, what does "delivered" mean, and why must it be in order*. Networking has at least three instances stacked on top of each other, and each was introduced to fix the one above it.

    TCP. TCP promises an ordered byte stream. If segment 5 is lost and 6–20 arrive, the kernel keeps 6–20 in the receive buffer and gives the application nothing until 5 is retransmitted — at best one RTT later (fast retransmit after duplicate ACKs), at worst a retransmission timeout, which on Linux is at least 200 ms. The application sees a stall, not a loss. That is the price of in-order delivery, and it is paid by every byte behind the hole regardless of what those bytes mean. See Head-of-Line Blocking and Packet Loss: Duplicate ACKs, Fast Retransmit and the RTO.

    HTTP/1.1. One request is in flight per connection; pipelining was specified but browsers abandoned it because responses had to come back in order, so a slow first response blocked the rest — application-level HOL. Browsers work around it with ~6 parallel connections per host, which is why HTTP/1.1: Persistent Connections and Their Limits sites sharded domains. HTTP/2 multiplexes many streams over *one* TCP connection with framing, so a slow response no longer blocks the others at the HTTP layer — but now all streams share a single TCP byte stream, so one lost packet stalls every stream until it is retransmitted. Under a few percent of loss HTTP/2 over one connection can be *slower* than HTTP/1.1 over six, because the six connections lose independently. HTTP/2 moved HOL from the HTTP layer down to TCP and made the blast radius larger.

    HTTP/3. QUIC makes streams the unit of loss recovery: each stream has its own ordered byte space, so a lost packet blocks only the streams whose data it carried; the others keep delivering. That removes cross-stream HOL. It does not remove HOL *within* a stream — a lost packet in the middle of one large response still stalls the rest of that response — and QPACK header compression can reintroduce a small cross-stream dependency if a header block references a dynamic-table entry that has not arrived yet. The trade-off is that QUIC does all of this in user space over UDP: more CPU per byte, less hardware offload, and a fallback to TCP whenever UDP is blocked. See HTTP/3 and QUIC.

    Full interview question, with follow-ups and red flags →
  3. A browser visits your site for the first time. Which protocol does the first request use, and why?

    Show a strong answer

    TCP + HTTP/2 (or 1.1): the browser learns HTTP/3 is available from the Alt-Svc header or an HTTPS DNS record and switches on later requests; it also races or falls back to TCP if QUIC stalls.

    Full interview question, with follow-ups and red flags →