Everything I didn’t know about HTTP2 (Part 1)
What Got You Here Won’t Get You There#
Before we talk about HTTP2 we need first to know what was the problem in HTTP/1.1 that makes us need to think we need to introduce HTTP2.
When HTTP first introduced more than 20 years ago there was no intention to handle this type of rich application, AJAX…etc, and all cool stuff we have today. The most notable limitation was head-of-line blocking: a connection handles one request at a time, so a slow response holds up every request queued behind it. Browsers open more connections to get around it, but no more than 6 per origin, it was 2 connections only in the beginning. to address this and limitation comes with it we started introducing workarounds that we’ll later name it “Best Practices™️”.

Here’s a page with 20 files loading over HTTP/1.1. Watch the files queue up for a free connection, and how each request waits a full round trip before its first byte:
Animated waterfall: Over HTTP/1.1 with one domain, the browser opens up to 6 connections. Each one carries one request at a time, so the 20 files the page needs queue up behind each other, and each request waits a full round trip before its first byte. The page loads in about 1,050 ms. Watch it on the page.
Loaded in
connections
files waiting for one
- DNS lookup and TCP+TLS setup
- Waiting for the first byte
- Downloading
Concatenation, minification, and sprites#
We have to do this as opening a new connection to download resources is a costly process and already limited (the aforementioned 6 parallel connection per origin limitation) so we will concatenate all of our assets (js, CSS and images as sprites) together and minify them to decrease the number of requests needed.
The problem with this approach is: change is costly, if you made any change for example to the minified CSS file like you change the color of the header background color, for example, you will invalidate all the CSS cache and the user will need to download this CSS file again although the change is very small compared to the change in download size. you change 6 characters and invalidate kilobytes in return for example.
Figure: The header's background color changes from #1a1a2e to #16213e: six characters in header.css. With 5 separate CSS files, a returning visitor downloads only header.css again: 2 KB, and the other files stay cached. With all of them concatenated into one bundle, the whole 40 KB bundle is downloaded again.
The change, in header.css:background: #1a1a2e16213e
Separate files5 requests
header.css2 KB, download againlayout.css12 KB, still cachedbuttons.css6 KB, still cachedforms.css10 KB, still cachedtheme.css10 KB, still cached2 KB to download
One bundle1 request
bundle.min.css40 KB, download again40 KB to download
Domain sharding#
An interesting thing about the 6 connections limit is: it is per origin, this means if we introduce more origins (domains) we can increase the number of connections available 💡 so we started to host our assets on domains like assets1.example.com, assets2.example.com, etc. This paid off most back when browsers allowed only 2 connections per domain: going from 2 to 4 is a big jump, going from 6 to 12 much less so.
There was a bonus too: a separate asset domain could be cookie-free, so image and script requests skipped the cookie header entirely, as long as your cookies weren’t set for the whole parent domain. More on why that matters in the next section.
The problem with this approach: every additional domain needs a new DNS lookup that the user has to wait for before the browser can download files. Every connection to it also needs its own TCP+TLS setup, which costs more round trips. And more connections don’t make the network any faster: they all share the same bandwidth.
Animated waterfall: Sharded across two asset domains, the browser opens 12 connections and fewer files queue up. But both new domains need their own DNS lookup and connection setup first, so the files start later. The page loads in about 1,030 ms, barely faster. Over HTTP/1.1 with one domain, the browser opens up to 6 connections. Each one carries one request at a time, so the 20 files the page needs queue up behind each other, and each request waits a full round trip before its first byte. The page loads in about 1,050 ms. Watch it on the page.
Loaded in
connections
files waiting for one
- DNS lookup and TCP+TLS setup
- Waiting for the first byte
- Downloading
Metadata#
HTTP is a stateless protocol this means we can’t know if 2 requests are by the same user or not. so we have to start using cookies; a special header that’s sent with every request we use to identify the user and may add other useful data as well. this will add additional bytes or even kilobytes to every request adding to this user agent too. the problem with this metadata is it’s static (most of the time they will be rarely changed) and not compressed data sent over the wire.
Avoidable Roundtrips#
Imagine this scenario we’ve page.html hosted in our server with some assets
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Page</title>
</head>
<body>
<img src="./Picaso.png" alt="Marawan Pablo" />
<script src="script.js"></script>
</body>
</html>
Although the server knows that this page.html page will request style.css, script.js, and Picaso.png files. you have no way to give a hint to the server: “when the client asks for page.html send these 3 files along with it.” although all files are on the server in the same place and it knows this HTML page will request these files.
Again we introduce a new workaround we started to inline resources, this means concatenate content of these files minify them (encode them in Base64 in case of images) so we can send them all together in the same response by writing them directly to the HTML file, this removed the need to make additional roundtrip we already know about beforehand.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
/**
* initial inlined CSS goes here
*/
</style>
<title>Page</title>
</head>
<body>
<img alt="Marawan Pablo" src="data:image/png;base64,....==" />
<script>
/**
* initial inlined javascript goes here
*/
</script>
</body>
</html>
But again this comes with a problem, we are sending the same data of assets over the wire again, as every time the browser requests HTML pages that uses some of the assets it has no way to utilize the idea it already receives this data before, we lost what we could gain by caching these assets. also in HTTP/1.1, we’ve no way other than cache mechanism to tell the server “we already received this data don’t send it again, please!”.
Animated sequence diagram: HTTP/1.1: the browser asks for page.html, waits for the server to build and send it, parses it, and only then asks for style.css and script.js: a second round trip. Everything is there at 840 ms. Inlined: the server writes the CSS and JS into page.html, so one round trip brings everything, at 640 ms. But it sends them every time, even when the browser already has them cached: 120 KB for nothing. Watch it on the page.
Everything there at
Sent for nothing
In part 2 I talk about what HTTP2 actually fixed with multiplexing, header compression, and binary framing, which of these old “Best Practices™️” became unnecessary, and what happened to Server Push, the feature that was supposed to solve the roundtrip problem.
I wrote this in 2020, before I used AI tools. When I moved it here, Claude reviewed it and fixed some parts. How I use AI →