Middle
What is the difference between HTTP/1 and HTTP/2?
sobes.tech AI
Answer from AI
Main differences:
- Multiplexing. HTTP/2 allows sending and receiving multiple requests and responses over a single TCP connection simultaneously. HTTP/1.1 required a separate connection for each request/response (except when pipelining was used, which had limitations).
- Header Compression. HTTP/2 uses HPACK compression to reduce header size and the amount of data transmitted. In HTTP/1.1, headers are sent in plain text.
- Stream Prioritization. In HTTP/2, the client can specify priorities for different streams (requests/responses), enabling the server to optimize data delivery order. HTTP/1.1 does not have this mechanism.
- Server Push. HTTP/2 allows the server to send resources to the client before the client requests them, based on assumptions about their necessity for page rendering. In HTTP/1.1, the client must explicitly request each resource.
- Frame Format. HTTP/2 transmits data in binary frames, which simplifies parsing and improves efficiency compared to the text format of HTTP/1.1.
Comparison table:
| Feature | HTTP/1.1 | HTTP/2 |
|---|---|---|
| Connection | One connection per request (no pipelining) | One connection for multiple streams |
| Multiplexing | No (or limited with pipelining) | Yes |
| Header Compression | No | HPACK (binary compression) |
| Stream Prioritization | No | Yes |
| Server Push | No | Yes |
| Data Format | Text | Binary (frames) |
| Head-of-Line Blocking | Present | Partially mitigated by multiplexing |
Example of an HTTP/1.1 request:
GET /index.html HTTP/1.1
Host: example.com
User-Agent: curl/7.68.0
Accept: */*
Example of partial data representation in HTTP/2 (frame exchange is more complex and not shown simply):
// In HTTP/2, communication occurs at the frame level (HEADERS, DATA, etc.),
// which are binary and hidden from the application layer.
// Representing them in plain text for illustration is difficult.
// Headers are compressed and sent in HEADERS frames.
// Response body is transmitted in DATA frames.