HTTP2.0 features
1. Binary framing
- Without changing
HTTP1.x
the semantics, methods, status codes,URL
and header fields,HTTP2.0
how to break throughHTTP1.1
the performance limitations, improve transmission performance, and achieve low latency and high throughput? One of the keys is to add a binary framing layer between the application layer (HTTP
) and the transport layer ( ).TCP
1.1 Knowledge about frames
- Frame:
HTTP2.0
The smallest unit of communication. All frames share a one-8
byte header, which contains the length, type, flag, and a reserved bit of the frame, and at least has an identifier that identifies the stream to which the current frame belongs. The frame carries Certain types of data, such asHTTP
headers, payloads, etc. - Message: A communication unit larger than a frame, which refers to a logical
HTTP
message, such as a request and a response. Consists of one or more frames - Stream: A unit of communication larger than a message. It is
TCP
a virtual channel in the connection that can carry two-way messages. Each stream has a unique integer identifier
1.2 What is binary framing
- At the binary framing layer,
HTTP2.0
all transmitted information is divided into smaller messages and frames, and they are encoded in a binary format to encapsulate them. Among them,HTTP1.X
the header information inheader
is encapsulated intoHeaders
the frame, andrequest body
will be encapsulated intoData
the frame.
1.3 How binary framing works
HTTP2.0
The communication is all done on aTCP
connection, which can carry any number of bidirectional data streams, and each corresponding data stream is sent in the form of a message. A message, on the other hand, consists of one or more frames, which can be sent out of order and then reassembled based on the stream identifier in the header of each frame.
1.4 Contribution of binary framing to performance optimization work
- Binary framing mainly provides the basis for various features below. It can encapsulate a data partition into smaller and more convenient data.
- First of all, in the single-link multi-resource mode, the link pressure on the server is reduced, the memory usage is less, and the link throughput is greater. This can be experienced in conjunction with the multiplexing below.
- On the other hand,
TCP
the network congestion state is improved due to the reduction of links, and the slow start time is reduced at the same time. Make congestion and packet loss recovery faster.
2. Header compression
HTTP1.1
Does not supportHTTP
header compression, for this reasonSPDY
andHTTP2.0
appeared.SPDY
It isDEFLATE
an algorithm.HTTP2.0
An algorithm specifically designed for header compression is usedHPACK
.
2.1 What is header compression
HTTP1.x
Each communication (request or response) carries header information describing resource attributes. Instead,HTTP2.0
a header table is used between the client and the server to track and store previously sent key-value pairs. The definition of request and response headersHTTP2.0
basically remains unchanged in , except that all header keys must be all lowercase, and the rows are required to be independently:method:
,:scheme:
,:host:
,:path:
these key-value pairs
2.2 How header compression works
- The same data is no longer resent through each request and response. Each new header key-value pair either appends to the end of the current table or replaces the previous value in the table.
- The header table
HTTP2.0
always exists during the duration of the link, and is updated gradually by both the client and the server.
2.3 The contribution of the first compression performance optimization work
- The header table
HTTP2.0
uses header compression technology. Make the header more compact and transmit faster, which is beneficial to the mobile network environment. Reduce the amount of data in each communication, so that the network congestion can be improved.
3. Flow control
HTTP2.0
Provides a simple mechanism for streaming data streams and connections:
- Traffic
HTTP
is based on each hop of the link, rather than end-to-end control - Flow control is based on window update frames, that is, the receiver broadcasts how many bytes it is ready to receive for a certain data stream, and how many bytes it wants to receive for the entire link.
- Flow control is directional, that is, the receiver may set any window size for each flow or even the entire link according to its own situation
- Flow control can be disabled by the receiver, both for individual streams and for the entire link.
- The type of frame determines whether flow control applies to the frame. Currently, only
DATA
frames are subject to flow control. All other types of frames do not consume space in the flow control window. This ensures that important control frames are not blocked by flow control
4. Multiplexing
- In
HTTP1.1
, the browser client has a certain number of requests for the same domain name at the same time. Requests exceeding the limit will be blocked. AndHTTP2.0
multiplexing in optimizes this performance.
4.1 What is multiplexing
- Based on a binary framing layer, requests and responses
HTTP2.0
canTCP
be sent simultaneously on a shared link. HTTP
The message is decomposed into independent frames without destroying the semantics of the message itself, sent out interleaved, and reassembled at the other end according to the stream identifier and header.
4.2 How multiplexing works
HTTP1.X
See how it works by comparing with .
-
HTTP1.X
-
HTTP2.0
4.3 Contribution of Multiplexing to Performance Optimization Efforts
- Requests and responses can be sent in parallel and interleaved, and these requests and responses do not affect each other
- Send multiple requests and responses in parallel using only one connection
- Reduces page load time by eliminating unnecessary delays
- No
HTTP1.x
more work to get around restrictions
5. Request priority
- Once
HTTP
the message is divided into many individual frames, performance can be further optimized by optimizing the interleaving and transmission order of these frames.
5.1 What is request priority
- Each flow can have a
31bit
priority value:0
indicates the highest priority;2的31次方-1
indicates the lowest priority.
5.2 How request prioritization works
- The client clearly specifies the priority, and the server can use this priority as the basis for interactive data. For example, the client priority is set to
.css>.js>.jpg
. The server returns results in this order, which is more conducive to efficient use of underlying connections and improved user experience. However, when using request priority, you should pay attention to whether the server supports request priority and whether it will cause head-of-line blocking problems. For example, high-priority slow response requests will block the interaction of other resources.
5.3 Contribution of request priority to performance optimization work
- The server can control resource allocation (, memory, bandwidth) according to the priority of the flow
CPU
, and after the response data is ready, the highest priority frame is sent to the client first. Browsers can dispatch requests as soon as resources are found, specifying the priority of each stream, and letting the server determine the optimal response order. In this way, requests do not need to be queued, which not only saves time, but also maximizes the use of each connection.
6. Server push
HTTP2.0
A powerful new feature added is that the server can send multiple responses to a client request. The server pushes resources to the client without the client's explicit request.
6.1 What is server push (in HTTP2.0)
- According to the client's request, the server returns multiple responses in advance and pushes additional resources to the client. As shown in the figure below, the client requests
stream 1(/page.html)
. The serverstream 1
pushesstream 2(/script.js)
andstream 4(/style.css)
6.2 How server push works
PUSH_PROMISE
A frame is a signal that the server intends to push resources to the client.PUSH_PROMISE
Only the headers of the pre-pushed resources are included in the frame. If the clientPUSH_PROMISE
has no comment on the frame, the serverPUSH_PROMISE
sends a responseDATA
frame after the frame. If the client has already cached the resource and does not need to push it, it can rejectPUSH_PROMISE
the frame.PUSH-PROMISE
The request-response principle must be followed, and resources can only be pushed in response to requests.PUSH_PROMISE
The frame must be sent before the response is returned to avoid race conditions on the client side (race conditions mean that in the case of multi-threading, different execution sequences will cause the computer to execute different results with different correctness)HTTP2.0
After connecting, the client and the server exchangeSETTINGS
frames, thereby limiting the maximum number of two-way concurrency.0
Therefore, the client can limit the number of push streams, or completely disable server push by setting this to only .
All pushed resources must obey the same-origin policy. In other words, the server cannot push third-party resources to the client casually, but must be confirmed by both parties.
6.3 Contribution of server push to performance optimization
- Server push is a mechanism for sending data ahead of a client request. In
HTTP2.0
, a server can send multiple responses to a client's request. If a request is made by your home page, the server may respond with home page content,logo
and style sheets, because it knows that the client will use these things. This not only reduces the redundant steps of data transmission, but also speeds up the response speed of the page and improves the user experience.