Part 03 · The Language of the Web
HTTP/1.1, HTTP/2 and HTTP/3
What each version fixed about the last one, from multiplexing to running over QUIC.
⏱ 1-2 hours · Station 33 of 153
Same methods, same status codes, three different engines
Everything in this module so far, methods, status codes, headers, cookies, URLs, is the meaning of HTTP. That meaning hasn't changed much since the 1990s. What has changed, twice, is how the bytes travel.
A modern web page needs dozens of files: HTML, CSS, scripts, fonts, images, API calls. Over a phone network in Dhaka with 80 ms of latency and a little packet loss, how those files share the network decides whether the page appears in half a second or three.
- HTTP/1.1 (1997): one request at a time per connection, so browsers open about six connections per site.
- HTTP/2 (2015): many requests at once over one connection.
- HTTP/3 (2022): the same idea, but over QUIC on UDP instead of TCP, so one lost packet doesn't freeze everything.
Your handler code is the same for all three. Your servers, proxies and performance aren't. This lesson explains what each version fixed about the last one.
The problem: many small files, one slow road
| Version | How requests share the connection | What hurts |
|---|---|---|
| HTTP/1.1 | One at a time; the next request waits for the previous response | Head-of-line blocking per connection; 6 connections = 6 handshakes |
| HTTP/2 | Many streams interleaved as frames on one TCP connection | TCP delivers bytes in order, so one lost packet stalls every stream |
| HTTP/3 | Many streams on one QUIC connection; each stream is ordered separately | UDP may be blocked on some networks; more CPU per byte |
Underneath it all is Module 2's formula (lesson 13): time = round trips × RTT + size ÷ bandwidth. Each version tries to cut round trips: fewer handshakes, fewer waits in line.
Think of a single bank counter
- HTTP/1.1 is a bank counter where the clerk serves one customer at a time. If one customer has a complicated problem, everyone behind waits. The bank's fix is to open six counters, but each one needs its own clerk and setup time.
- HTTP/2 is one very fast clerk who takes papers from everyone in the queue at once and works on all of them, handing back pages as they're ready. Much better, until the clerk drops one page of one customer's file: the rule says pages must be returned in order, so the clerk stops handing anything back until that page is found.
- HTTP/3 is the same fast clerk, but each customer's pages only need to be in order for that customer. If one page is lost, only that customer waits.
Where the analogy stops working. A clerk can be reasoned with; TCP can't. TCP's in-order delivery is a promise to the application, and it doesn't know HTTP/2 has independent streams inside. That's exactly why HTTP/3 had to replace TCP rather than tweak HTTP/2.
The building blocks
- HTTP/1.1: text, one exchange at a time. A request, then its response, then the next request on the same connection (keep-alive, lesson 10). Pipelining (sending several requests without waiting) existed but responses still had to come back in order, and it was so badly supported that browsers disabled it. Workaround: about 6 parallel connections per host, plus tricks like bundling files and spreading assets over several domains.
- HTTP/2: binary frames and streams. Each request/response pair is a stream with an ID (1, 3, 5... from the client). Messages are cut into frames tagged with the stream ID, and frames of different streams are interleaved on one connection. A slow response no longer blocks others at the HTTP level.
- HPACK header compression. Headers repeat on every request (cookies, user-agent). HTTP/2 keeps a table of headers already sent and sends small references instead, saving many bytes on chatty pages.
- HTTP/2 needs one connection, one handshake. TCP + TLS once, with ALPN choosing
h2during the TLS handshake (you sawALPN: server accepted h2in thecurl -vat the end of Module 2). - TCP head-of-line blocking. TCP guarantees in-order bytes for the whole connection. If one packet is lost, everything behind it waits for the retransmission (Module 2, lesson 10), even frames belonging to other streams. On lossy mobile networks, HTTP/2's single connection can be slower than HTTP/1.1's six.
- HTTP/3: QUIC over UDP. QUIC rebuilds reliability, ordering and congestion control on top of UDP, but per stream. A lost packet only delays the streams it carried. TLS 1.3 is built into QUIC, so connection + encryption setup is one round trip (and repeat visits can send data immediately with 0-RTT).
- Connection migration. A QUIC connection is identified by a connection ID, not by IP and port. When your phone switches from Wi-Fi to mobile data, the connection survives instead of starting over.
- Discovery. Browsers learn that a site supports HTTP/3 from an
alt-svc: h3=":443"header (or DNS records) on an HTTP/1.1 or HTTP/2 response, then try QUIC next time, falling back to TCP if UDP is blocked.
A page loads 40 small files from one site over a mobile network with 2% packet loss. The site switches from HTTP/2 to HTTP/3. What improves most?
Behind the scenes: one connection, three streams
An HTTP/2 connection carrying three requests
Step 1/5 One TCP + TLS handshake
Two round trips. During TLS, ALPN agrees on h2. This is the only handshake for the whole page.
- round_trips
- 2
The picture to keep in your head
Three versions, one meaning, three ways to share the road:
- HTTP/1.1: one lane per connection, so open six lanes and pay six handshakes.
- HTTP/2: one wide lane where many conversations interleave, but a single pothole (lost packet) stops all traffic.
- HTTP/3: one road with a separate lane per conversation; a pothole only slows its own lane, and the toll booth (handshake) is quicker.
Write your app for HTTP semantics; let the server, proxy or CDN pick the version.
Try it: which version helps here?
Does the version matter in this case?
HTTP/1.1: 6 connections, files queue up behind each other
Code from first principles
A small model, counted in round trips, of fetching 12 small files with each version. It accounts for the setup cost (TCP + TLS is 2 round trips; QUIC with TLS built in is 1), how many requests can be in flight, and what one lost packet does. It's a simplification (no bandwidth limits, instant server), but it shows the shape of each design.
package main
import "fmt"
// A simple model counted in round trips (RTT = 100 ms). A page needs 12 small files.
// Each file takes one round trip once its request is sent. One packet of file #3 is lost
// in the second scenario, and recovering it costs one extra round trip.
const rtt, files, lostFile = 100, 12, 3
type result struct{ total, delayed int }
// run returns when the last file arrives, and how many files were held up by the loss.
// setup: round trips before the first request; conns: parallel connections;
// multiplexed: all files share one connection at once; perStreamLoss: a loss only stalls its own file.
func run(setup, conns int, multiplexed, perStreamLoss, loss bool) result {
done := make([]int, files+1) // round trip in which each file arrives
for f := 1; f <= files; f++ {
if multiplexed {
done[f] = setup + 1 // every file is requested at once on the one connection
} else {
done[f] = setup + (f-1)/conns + 1 // each connection fetches its files one after another
}
}
delayed := 0
if loss {
for f := 1; f <= files; f++ {
stalled := false
switch {
case perStreamLoss: // QUIC: only the stream that lost a packet waits
stalled = f == lostFile
case multiplexed: // TCP: one lost packet blocks every stream on the connection
stalled = true
default: // separate connections: only files on the same connection, from #3 on
stalled = (f-1)%conns == (lostFile-1)%conns && f >= lostFile
}
if stalled {
done[f]++
delayed++
}
}
}
last := 0
for _, d := range done {
last = max(last, d)
}
return result{last * rtt, delayed}
}
func main() {
setups := []struct {
name string
setup, conns int
multiplexed, perStreamLoss bool
handshakes int
}{
{"HTTP/1.1, 1 connection ", 2, 1, false, false, 1},
{"HTTP/1.1, 6 connections", 2, 6, false, false, 6},
{"HTTP/2, 1 connection ", 2, 1, true, false, 1},
{"HTTP/3 (QUIC) ", 1, 1, true, true, 1},
}
for _, loss := range []bool{false, true} {
if loss {
fmt.Printf("one packet of file #%d lost:\n", lostFile)
} else {
fmt.Println("no loss:")
}
for _, s := range setups {
r := run(s.setup, s.conns, s.multiplexed, s.perStreamLoss, loss)
fmt.Printf(" %s last file at %4d ms, handshakes %d, files delayed by the loss %2d\n", s.name, r.total, s.handshakes, r.delayed)
}
}
}// A simple model counted in round trips (RTT = 100 ms). A page needs 12 small files.
// Each file takes one round trip once its request is sent. One packet of file #3 is lost
// in the second scenario, and recovering it costs one extra round trip.
const rtt = 100, files = 12, lostFile = 3;
// run returns when the last file arrives, and how many files were held up by the loss.
// setup: round trips before the first request; conns: parallel connections;
// multiplexed: all files share one connection at once; perStreamLoss: a loss only stalls its own file.
function run(setup, conns, multiplexed, perStreamLoss, loss) {
let last = 0, delayed = 0;
for (let f = 1; f <= files; f++) {
// multiplexed: every file is requested at once; otherwise each connection fetches its files one after another
let done = multiplexed ? setup + 1 : setup + Math.floor((f - 1) / conns) + 1;
if (loss) {
let stalled;
if (perStreamLoss) stalled = f === lostFile; // QUIC: only the stream that lost a packet waits
else if (multiplexed) stalled = true; // TCP: one lost packet blocks every stream on the connection
else stalled = (f - 1) % conns === (lostFile - 1) % conns && f >= lostFile; // same connection, from #3 on
if (stalled) {
done++;
delayed++;
}
}
last = Math.max(last, done);
}
return { total: last * rtt, delayed };
}
const setups = [
["HTTP/1.1, 1 connection ", 2, 1, false, false, 1],
["HTTP/1.1, 6 connections", 2, 6, false, false, 6],
["HTTP/2, 1 connection ", 2, 1, true, false, 1],
["HTTP/3 (QUIC) ", 1, 1, true, true, 1],
];
for (const loss of [false, true]) {
console.log(loss ? `one packet of file #${lostFile} lost:` : "no loss:");
for (const [name, setup, conns, multiplexed, perStreamLoss, handshakes] of setups) {
const r = run(setup, conns, multiplexed, perStreamLoss, loss);
console.log(` ${name} last file at ${String(r.total).padStart(4)} ms, handshakes ${handshakes}, files delayed by the loss ${String(r.delayed).padStart(2)}`);
}
}// A simple model counted in round trips (RTT = 100 ms). A page needs 12 small files.
// Each file takes one round trip once its request is sent. One packet of file #3 is lost
// in the second scenario, and recovering it costs one extra round trip.
const RTT: u32 = 100;
const FILES: u32 = 12;
const LOST_FILE: u32 = 3;
// run returns when the last file arrives, and how many files were held up by the loss.
// setup: round trips before the first request; conns: parallel connections;
// multiplexed: all files share one connection at once; per_stream_loss: a loss only stalls its own file.
fn run(setup: u32, conns: u32, multiplexed: bool, per_stream_loss: bool, loss: bool) -> (u32, u32) {
let (mut last, mut delayed) = (0, 0);
for f in 1..=FILES {
// multiplexed: every file is requested at once; otherwise each connection fetches its files one after another
let mut done = if multiplexed { setup + 1 } else { setup + (f - 1) / conns + 1 };
if loss {
let stalled = if per_stream_loss {
f == LOST_FILE // QUIC: only the stream that lost a packet waits
} else if multiplexed {
true // TCP: one lost packet blocks every stream on the connection
} else {
(f - 1) % conns == (LOST_FILE - 1) % conns && f >= LOST_FILE // same connection, from #3 on
};
if stalled {
done += 1;
delayed += 1;
}
}
last = last.max(done);
}
(last * RTT, delayed)
}
fn main() {
let setups = [
("HTTP/1.1, 1 connection ", 2, 1, false, false, 1),
("HTTP/1.1, 6 connections", 2, 6, false, false, 6),
("HTTP/2, 1 connection ", 2, 1, true, false, 1),
("HTTP/3 (QUIC) ", 1, 1, true, true, 1),
];
for loss in [false, true] {
if loss {
println!("one packet of file #{LOST_FILE} lost:");
} else {
println!("no loss:");
}
for (name, setup, conns, multiplexed, per_stream, handshakes) in setups {
let (total, delayed) = run(setup, conns, multiplexed, per_stream, loss);
println!(" {name} last file at {total:4} ms, handshakes {handshakes}, files delayed by the loss {delayed:2}");
}
}
}# A simple model counted in round trips (RTT = 100 ms). A page needs 12 small files.
# Each file takes one round trip once its request is sent. One packet of file #3 is lost
# in the second scenario, and recovering it costs one extra round trip.
RTT, FILES, LOST_FILE = 100, 12, 3
def run(setup, conns, multiplexed, per_stream_loss, loss):
"""When the last file arrives, and how many files were held up by the loss.
setup: round trips before the first request; conns: parallel connections;
multiplexed: all files share one connection at once; per_stream_loss: a loss only stalls its own file."""
last = delayed = 0
for f in range(1, FILES + 1):
# multiplexed: every file is requested at once; otherwise each connection fetches its files one after another
done = setup + 1 if multiplexed else setup + (f - 1) // conns + 1
if loss:
if per_stream_loss: # QUIC: only the stream that lost a packet waits
stalled = f == LOST_FILE
elif multiplexed: # TCP: one lost packet blocks every stream on the connection
stalled = True
else: # separate connections: only files on the same connection, from #3 on
stalled = (f - 1) % conns == (LOST_FILE - 1) % conns and f >= LOST_FILE
if stalled:
done += 1
delayed += 1
last = max(last, done)
return last * RTT, delayed
setups = [
("HTTP/1.1, 1 connection ", 2, 1, False, False, 1),
("HTTP/1.1, 6 connections", 2, 6, False, False, 6),
("HTTP/2, 1 connection ", 2, 1, True, False, 1),
("HTTP/3 (QUIC) ", 1, 1, True, True, 1),
]
for loss in (False, True):
print(f"one packet of file #{LOST_FILE} lost:" if loss else "no loss:")
for name, setup, conns, multiplexed, per_stream, handshakes in setups:
total, delayed = run(setup, conns, multiplexed, per_stream, loss)
print(f" {name} last file at {total:4d} ms, handshakes {handshakes}, files delayed by the loss {delayed:2d}")public class Main {
// A simple model counted in round trips (RTT = 100 ms). A page needs 12 small files.
// Each file takes one round trip once its request is sent. One packet of file #3 is lost
// in the second scenario, and recovering it costs one extra round trip.
static final int RTT = 100, FILES = 12, LOST_FILE = 3;
// run returns when the last file arrives, and how many files were held up by the loss.
// setup: round trips before the first request; conns: parallel connections;
// multiplexed: all files share one connection at once; perStreamLoss: a loss only stalls its own file.
static int[] run(int setup, int conns, boolean multiplexed, boolean perStreamLoss, boolean loss) {
int last = 0, delayed = 0;
for (int f = 1; f <= FILES; f++) {
// multiplexed: every file is requested at once; otherwise each connection fetches its files one after another
int done = multiplexed ? setup + 1 : setup + (f - 1) / conns + 1;
if (loss) {
boolean stalled;
if (perStreamLoss) stalled = f == LOST_FILE; // QUIC: only the stream that lost a packet waits
else if (multiplexed) stalled = true; // TCP: one lost packet blocks every stream on the connection
else stalled = (f - 1) % conns == (LOST_FILE - 1) % conns && f >= LOST_FILE; // same connection, from #3 on
if (stalled) {
done++;
delayed++;
}
}
last = Math.max(last, done);
}
return new int[] {last * RTT, delayed};
}
public static void main(String[] args) {
Object[][] setups = {
{"HTTP/1.1, 1 connection ", 2, 1, false, false, 1},
{"HTTP/1.1, 6 connections", 2, 6, false, false, 6},
{"HTTP/2, 1 connection ", 2, 1, true, false, 1},
{"HTTP/3 (QUIC) ", 1, 1, true, true, 1},
};
for (boolean loss : new boolean[] {false, true}) {
System.out.println(loss ? "one packet of file #" + LOST_FILE + " lost:" : "no loss:");
for (Object[] s : setups) {
int[] r = run((Integer) s[1], (Integer) s[2], (Boolean) s[3], (Boolean) s[4], loss);
System.out.printf(" %s last file at %4d ms, handshakes %d, files delayed by the loss %2d%n", s[0], r[0], s[5], r[1]);
}
}
}
}Output (the same in all five languages):
no loss:
HTTP/1.1, 1 connection last file at 1400 ms, handshakes 1, files delayed by the loss 0
HTTP/1.1, 6 connections last file at 400 ms, handshakes 6, files delayed by the loss 0
HTTP/2, 1 connection last file at 300 ms, handshakes 1, files delayed by the loss 0
HTTP/3 (QUIC) last file at 200 ms, handshakes 1, files delayed by the loss 0
one packet of file #3 lost:
HTTP/1.1, 1 connection last file at 1500 ms, handshakes 1, files delayed by the loss 10
HTTP/1.1, 6 connections last file at 500 ms, handshakes 6, files delayed by the loss 2
HTTP/2, 1 connection last file at 400 ms, handshakes 1, files delayed by the loss 12
HTTP/3 (QUIC) last file at 300 ms, handshakes 1, files delayed by the loss 1
Reading the output:
- HTTP/1.1 on one connection: 1400 ms. Two round trips of setup, then 12 files one after another. That's why nobody used a single HTTP/1.1 connection for a page.
- Six connections cut it to 400 ms, but cost six handshakes (six times the TLS work for the server, six slow-starting TCP connections).
- HTTP/2: 300 ms with one handshake. All 12 requests go out together on one connection.
- HTTP/3: 200 ms, because QUIC's setup is one round trip instead of two.
- With one lost packet, HTTP/2 delays all 12 files (TCP head-of-line blocking), while HTTP/3 delays only file #3. HTTP/1.1 with six connections delays the 2 files on the unlucky connection.
Real HTTP/2 multiplexing, seen from curl on this machine (it doesn't support HTTP/3, but the server announces it):
$ curl -sv --http2 --parallel -o /dev/null -o /dev/null -o /dev/null https://example.com/ https://example.com/ https://example.com/ 2>&1 | grep -iE "^\* (Connected|Re-using|Multiplex|using HTTP)|\[HTTP/2\] \[[0-9]+\] OPENED"
* using HTTP/2
* [HTTP/2] [1] OPENED stream for https://example.com/
* Multiplexed connection found
* [HTTP/2] [3] OPENED stream for https://example.com/
* Multiplexed connection found
* [HTTP/2] [5] OPENED stream for https://example.com/
$ curl -s -D - -o /dev/null https://www.cloudflare.com/ | tr -d '\r' | grep -i "^alt-svc"
alt-svc: h3=":443"; ma=86400
- Streams 1, 3 and 5 were opened on the same connection: "Multiplexed connection found" means the second and third requests reused it instead of handshaking again. Client-started streams always get odd numbers.
alt-svc: h3=":443"; ma=86400tells clients "HTTP/3 is available on UDP port 443; remember that for 86400 seconds (a day)". A browser would try QUIC on its next visit.
Real engineering
- Turn on HTTP/2 everywhere public, and HTTP/3 at the CDN or load balancer. It's usually a configuration switch; your application code doesn't change.
- Most apps speak HTTP/1.1 behind the proxy. Browsers use h2/h3 to the CDN or load balancer, which talks HTTP/1.1 (or h2) to your servers. That's fine: the slow, lossy part is the user's network.
- Undo HTTP/1.1 hacks. Domain sharding (assets on several hostnames) defeats HTTP/2's single connection; giant bundles and image sprites hurt caching. Keep files reasonably small and cacheable.
- Watch connection-level limits. HTTP/2 servers cap concurrent streams per connection (often 100-250). Clients that open thousands of streams on one connection get throttled.
- gRPC needs HTTP/2 end to end. Make sure every proxy in the path supports h2 to the backend, not just to the client.
- Measure, don't assume. Use real-user monitoring split by protocol. On good networks the difference is small; on lossy mobile networks HTTP/3 often wins clearly.
When it breaks
Pick a failure
assets on static1.shop.com ... static4.shop.com
Think before you read on
HTTP/3's 0-RTT lets a returning client send its first request together with the very first handshake packet, saving a whole round trip. Why is 0-RTT data only allowed for requests that are safe to repeat, like GET?
Reveal
0-RTT data is encrypted with keys from a previous session, before the server has proven anything fresh in this connection. An attacker who records that first packet can replay it to the server later, and the server can't tell it's a copy. If the request was GET /products, a replay is harmless. If it was POST /payments, a replay could charge the customer again. So servers accept 0-RTT only for idempotent, safe requests (lesson 4), and anything that changes state waits for the full handshake.
Check yourself
1. What problem did HTTP/2 solve, and how?
Answer
HTTP/1.1 handles one request at a time per connection, so browsers opened about six connections and requests still queued. HTTP/2 splits messages into frames tagged with stream IDs and interleaves many streams on one connection, with header compression.
2. What is TCP head-of-line blocking, and why does it hurt HTTP/2?
Answer
TCP delivers bytes strictly in order, so one lost packet holds back everything after it until it's retransmitted. HTTP/2 puts all streams on one TCP connection, so a single loss stalls every stream, even those whose data arrived.
3. What does QUIC change for HTTP/3?
Answer
QUIC runs over UDP and provides reliability and ordering per stream, so a loss only stalls its own stream. It builds TLS 1.3 in, making setup one round trip (0-RTT on repeat visits), and connections survive network changes via connection IDs.
4. Does your application code need to change to support HTTP/2 or HTTP/3?
Answer
Usually not. Methods, status codes and headers mean the same thing; the version is negotiated by the server, proxy or CDN. What changes is configuration, and old HTTP/1.1 performance hacks you may want to remove.
Your challenge: add bandwidth to the model
Make the model more realistic:
- Give each file a size (say 50 KB) and the link a bandwidth (say 10 Mbit/s). A file now takes one round trip plus its share of transfer time.
- On a single multiplexed connection, files share the bandwidth; on six HTTP/1.1 connections, they also share the same link.
- Add TCP slow start in its simplest form: a new connection can only send 14 KB in its first round trip, doubling each round trip after.
Before you run it, predict: does HTTP/1.1 with six connections ever beat HTTP/2 once slow start is included?
You've succeeded when the model prints times for all four setups with and without loss, you can point to the line where slow start makes six fresh connections expensive, and HTTP/3 still delays only one file when a packet is lost.
The mental model
HTTP's meaning (methods, statuses, headers) stayed the same; the transport changed. HTTP/1.1 does one exchange at a time per connection, so browsers open six. HTTP/2 multiplexes many streams as frames over one TCP connection with header compression, but TCP's in-order delivery lets one lost packet stall them all. HTTP/3 moves to QUIC over UDP: per-stream recovery, TLS built in, one round trip to start, and connections that survive network changes. Write for the semantics; let your edge pick the version.
Station stamp
Finish the chapter
Done reading? Press Complete to save your progress. XP is Pro-only — on Pro this chapter is worth +10 XP.
Go Pro to earn XP →