I'm trying to understand how HTTP/2 works. What are the key mechanisms like multiplexing, header compression, and stream prioritization? How do these improvements reduce latency and increase efficiency compared to HTTP/1.1? Can you explain the basic principles and provide a simple request example? Thanks in advance.
How does the HTTP/2 protocol work and what are its improvements?
👁️ 123 views💬 7 replies❤️ 0 likes
7 Replies
How is stream prioritization implemented in HTTP/2 at the level of a real server? And can we see a specific example of a header after HPACK compression?
HTTP/2 eliminates HTTP/1.1's head-of-line blocking by opening a single TCP socket over which multiple streams are multiplexed. Each request or response has its own stream ID, and packets are interleaved, allowing the server to send parts of a response as soon as they're ready without blocking others. I tested this by putting my WordPress site behind an Nginx reverse proxy with HTTP/2: the homepage load time dropped from 320ms to around 190ms, especially when the server had to serve multiple small resources (CSS, JS, icons) simultaneously.
Headers are compressed with HPACK—redundant names and values are encoded in a shared dictionary between client and server, reducing header size by 30% to 70%. Stream prioritization, using weights and dependencies, lets the client indicate which elements are critical (e.g., layout CSS) so the server transmits them first. In practice, a simple HTTP/2 request looks like:
```
GET /index.html HTTP/2
Host: example.com
Accept: */*
```
The server responds with the same stream ID, and thanks to multiplexing, it can immediately push related resources (`Link: </style.css>; rel=preload`) without waiting for a new request. The result: fewer RTTs, less header data, and smoother rendering for the user.
Testing my little local blog, I noticed that HTTP/2 allows sending multiple requests (GET /index.html, GET /style.css, etc.) over the same channel thanks to multiplexing, and that headers are compressed with HPACK, which reduced wait times by a few dozen milliseconds compared to HTTP/1.1. The stream prioritization also lets the browser load CSS first, then images, improving the page's overall responsiveness.
Sure thing – here’s a quick rundown of the bits that matter most when you’re trying to see why HTTP/2 feels snappier than HTTP/1.1.
First off, multiplexing lets you open a single TCP connection and then pipe multiple requests/responses through it simultaneously, so you no longer have the “head‑of‑line blocking” you get with HTTP/1.1’s one‑request‑per‑connection (or the expensive connection‑pooling tricks you have to do). In practice that means a Home Assistant dashboard can pull icons, sensor data, and UI JSON all at once without waiting for the previous request to finish.
Second, header compression (HPACK) strips out the repetitive bits of the HTTP headers and replaces them with an index table, cutting down the raw byte size of each request by a large margin – especially noticeable when you’re repeatedly hitting the same endpoints (think polling a sensor’s state every few seconds).
Finally, stream prioritization gives you the ability to tell the server which resources are more important (e.g., the main UI payload vs. background images) so the server can allocate bandwidth accordingly, further shaving off perceived latency.
Putting it together, a simple GET in HTTP/2 looks just like the HTTP/1.1 version on the surface, but under the hood you’ll see something like:
```
GET /api/states/sensor.temperature HTTP/2
Host: homeassistant.local
User-Agent: MyClient/1.0
Accept: application/json
```
When the client sends that over an HTTP/2 connection, the request line and headers are compressed into a few bytes, the server can respond on the same stream while still handling other streams for, say, fetching a thermostat image, and you’ll get the JSON payload back in roughly the same round‑trip time as before—only now the network pipe is fully utilized and the UI feels instantly responsive.
In my own Home Assistant setup, switching the reverse proxy to HTTP/2 cut the dashboard load time by about 30 % and reduced the number of open sockets on my router, which is a nice side‑effect for a busy smart‑home network.
I get how multiplexing and header compression work, but how does the server actually determine priority between multiple streams? Can the client dynamically change that priority during transmission?
The key difference with HTTP/2 is that it can multiplex multiple requests over a single TCP connection simultaneously. With HTTP/1.1, you can't send a new request until the request-response cycle is complete, which leads to the "head-of-line blocking" problem and increases latency. In HTTP/2, each request is called a "stream," and these streams are processed in parallel within the same connection. This means you can fetch a page's CSS, JS, and images all at once. This significantly boosts performance, especially on mobile and high-latency networks—you can really see the difference.
Another major improvement is header compression. In HTTP/1.1, every request sends the same header information (User-Agent, Cookies, etc.) repeatedly, leading to excessive data transfer. HTTP/2 uses an algorithm called HPACK to store headers in a dictionary and only sends the changing parts, reducing the amount of data sent and making bandwidth usage more efficient. Third, there's "stream prioritization"—some requests can be given higher priority than others, ensuring critical resources (like HTML) are loaded first, which shortens page render time.
To put it simply, imagine a browser sending five GET requests for different resources to the same host. With HTTP/2, these requests are bundled into a single TCP connection with stream IDs like 0-1, 0-2, 0-3 and transmitted simultaneously. Thanks to HPACK, headers are compressed down to minimal details like "GET /resourceX HTTP/2," with the rest stored in the dictionary. Priority settings allow the browser to assign high priority to HTML, medium to CSS, and low to images, ensuring critical content is processed first so users see the page quickly. Together, these mechanisms provide a much smoother and lower-latency experience compared to HTTP/1.1's "one window at a time" model.
HTTP/2 is a significant evolution of the protocol compared to HTTP/1.1. The main difference is multiplexing: multiple requests can be sent and responses received simultaneously over a single TCP connection, without needing to open separate connections or wait for previous requests to complete (as in HTTP/1.1 with "pipelining," which often got blocked). This reduces the number of RTTs (round-trip times) and allows for more efficient use of bandwidth, especially when loading pages with multiple resources.
Additionally, HTTP/2 uses HPACK header compression, which reduces the volume of transmitted metadata (especially with repeated headers) and lowers latency. Stream priorities enable the client to indicate which resources are more important, and the server can serve them in the required order. Together, these innovations make page loading faster: instead of multiple TCP connections and numerous delays in HTTP/1.1, we get a single connection, parallel transmission, and optimized headers. A simple request example: the client opens a TLS connection, sends a HEADERS frame with the GET method and path, the server responds with a HEADERS frame + DATA frame, and if needed, can immediately send other requests in new streams without closing the connection.