HTTP2.0 features


1. Binary framing

  • Without changing HTTP1.xthe semantics, methods, status codes, URLand header fields, HTTP2.0how to break through HTTP1.1the 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.0The smallest unit of communication. All frames share a one- 8byte 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 as HTTPheaders, payloads, etc.
  • Message: A communication unit larger than a frame, which refers to a logical HTTPmessage, such as a request and a response. Consists of one or more frames
  • Stream: A unit of communication larger than a message. It is TCPa 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.0all transmitted information is divided into smaller messages and frames, and they are encoded in a binary format to encapsulate them. Among them, HTTP1.Xthe header information in headeris encapsulated into Headersthe frame, and request bodywill be encapsulated into Datathe frame.

1.3 How binary framing works

  • HTTP2.0The communication is all done on a TCPconnection, 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, TCPthe 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.1Does not support HTTPheader compression, for this reason SPDYand HTTP2.0appeared.
  • SPDYIt is DEFLATEan algorithm.
  • HTTP2.0An algorithm specifically designed for header compression is used HPACK.

2.1 What is header compression

  • HTTP1.xEach communication (request or response) carries header information describing resource attributes. Instead, HTTP2.0a 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 headers HTTP2.0basically 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.0always 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.0uses 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.0Provides a simple mechanism for streaming data streams and connections:

  • Traffic HTTPis 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 DATAframes 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. And HTTP2.0multiplexing in optimizes this performance.

4.1 What is multiplexing

  • Based on a binary framing layer, requests and responses HTTP2.0can TCPbe sent simultaneously on a shared link.
  • HTTPThe 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.XSee how it works by comparing with .

  • HTTP1.X
    insert image description here

  • HTTP2.0
    insert image description here

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.xmore work to get around restrictions

5. Request priority

  • Once HTTPthe 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 31bitpriority value: 0indicates the highest priority; 2的31次方-1indicates 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.0A 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 server stream 1pushes stream 2(/script.js)andstream 4(/style.css)
    insert image description here

6.2 How server push works

  • PUSH_PROMISEA frame is a signal that the server intends to push resources to the client.
  • PUSH_PROMISEOnly the headers of the pre-pushed resources are included in the frame. If the client PUSH_PROMISEhas no comment on the frame, the server PUSH_PROMISEsends a response DATAframe after the frame. If the client has already cached the resource and does not need to push it, it can reject PUSH_PROMISEthe frame.
  • PUSH-PROMISEThe request-response principle must be followed, and resources can only be pushed in response to requests.
  • PUSH_PROMISEThe 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.0After connecting, the client and the server exchange SETTINGSframes, thereby limiting the maximum number of two-way concurrency. 0Therefore, 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, logoand 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.

Guess you like

Origin blog.csdn.net/FlyingKuiKui/article/details/129167865