HTTP vs http2 details

Posted on

Disadvantages of http1

  • The end of the thread is blockedIn this way, several requests are queued and serialized for single thread processing, and the subsequent requests can only be executed when the previous request returns. Once a request times out, the subsequent requests can only be blocked, and there is no way, that is, the commonly known thread end blocking;
  • Not making full use of TCP links: in HTTP 1. X, if you want to concurrent multiple requests, you must use multiple TCP links, and in order to control resources, the browser also has 6-8 TCP link requests for a single domain name

Advantages of http2

  • Multiplexing: the most valuable advantage is that it solves the problem of line blocking and allows a single http2 connection to be sentmultipleMake full use of TCP. It makes the domain name, Sprite map and inline style no longer applicable.
  • Header compressionHttp2.0 can maintain static dictionary and dynamic dictionary on client and server, which can be used to compress and update HTTP header, greatly reducing the traffic caused by header transmission. The header in non two dictionaries can be compressed by Huffman compression.
  • New binary format: http1. X is the text format transmission, http2 is the binary format transmission.
  • Server push: the server can actively push resources to the client.

Binary framing

Let’s first understand a few concepts:

frame: minimum unit message of HTTP / 2 data communication: refers to the logical HTTP message in http / 2. For example, request and response, etc., the message consists of one or more frames.

flow: a virtual channel that exists in the connection. Flows can carry two-way messages, and each flow has a unique integer ID.

Http / 2 uses binary format to transmit data instead of text format of HTTP 1. X. binary protocol is more efficient in parsing. Http / 1 request and response messages are composed of starting line, header and entity body (optional), and each part is separated by text newline. Http / 2 divides the request and response data into smaller frames, and they are binary encoded.

In http / 2, all communications under the same domain name are completed on a single connection, which can carry any number of two-way data streams. Each data stream is sent in the form of a message, which in turn consists of one or more frames. Multiple frames can be sent out of order, and can be reassembled according to the flow identification at the beginning of the frame.

Multiplexing

Multiplexing, instead of the original sequence and blocking mechanism. All requests are completed concurrently through a TCP connection. In HTTP 1. X, if you want to concurrent multiple requests, you must use multiple TCP links, and in order to control resources, the browser will also limit 6-8 TCP link requests for a single domain name. As shown in the figure below, the request circled in red has been suspended for a period of time because the number of domain name links has exceeded the limit

HTTP vs http2 details

In http / 2, with binary framing, http / 2 no longer relies on TCP links to achieve multi stream parallelism

  • All communications under the same domain name are completed on a single connection.
  • A single connection can carry any number of bidirectional data streams.
  • The data stream is sent in the form of message, and the message is composed of one or more frames. Multiple frames can be sent out of order, because they can be reassembled according to the flow identification at the beginning of the frame.

This feature greatly improves the performance

  • The same domain name only needs one TCP connectionTo eliminate the delay and memory consumption caused by multiple TCP connections.
  • A single connection can interleave requests and responses in parallel without interference.
  • In http / 2, each request can carry a 31 bit priority value. 0 represents the highest priority. The higher the value, the lower the priority. With this priority value, the client and server can adopt different strategies when dealing with different streams and send streams, messages and frames in an optimal way.

Server push

The server can actively push other resources when sending page HTML, instead of waiting for the browser to resolve to the corresponding location and initiate a request to respond. For example, the server can actively push JS and CSS files to the client without sending these requests when the client parses HTML.

The server can actively push, and the client has the right to choose whether to receive or not. If the resources pushed by the server have been cached by the browser, the browser can send rst_ Stream frame. Active push also follows the same source policy, and the server will not push the third-party resources to the client.

Head compression

The size of HTTP 1.1 requests becomes larger and larger, sometimes even larger than the initial size of the TCP window, because they need to wait for the response with ack before they can continue to be sent. Http / 2 uses hpack (compression format specially designed for HTTP / 2 header) to compress the header, which can save the network traffic occupied by the header. Http / 1. X will carry a lot of redundant header information every request, which wastes a lot of bandwidth resources.

Each HTTP communication will carry a set of headers to describe the resources, browser properties, cookies, etc. of this communication, such as

HTTP vs http2 details

In order to reduce resource consumption and improve performance,Http / 2 adopts a compression strategy for these headers

  • Http / 2 uses “dictionary table” to track and store previously sent key value pairs in client and server. For the same data, it is no longer sent through each request and response;
  • The dictionary table always exists in the duration of HTTP / 2 connection, and is updated gradually by the client and server;
  • Each new header key value pair is either appended to the end of the current table or replaced with the previous value in the table.
  • Dictionary table includes dynamic dictionary table and static dictionary table

For example: in the two requests in the figure below, the first request sends all header fields, and the second request only needs to send differential data, which can reduce redundant data and reduce overhead.

HTTP vs http2 details

Let’s take a look at a practical example. Here’s a package captured by Wireshark to access Google’s home page:

HTTP vs http2 details

Here’s an interviewhttps://www.google.com/ I caught it, we can see the content of the header, which takes up 437 bytes in total. When we select the cookie of the header, we can see that the cookie takes up 118 bytes in total. Next, let’s look at the header of the second request:

HTTP vs http2 details

As can be seen from the figure above, thanks to the header compression, the cookie in the second request occupies only one byte. Let’s take a look at the changed accept field

HTTP vs http2 details

Because the accept field is different from the content in request 1 and needs to be sent to the server, it takes 29 bytes.

epilogue

  • Http / 2 reduces latency by supporting multiplexing of requests and responses
  • The protocol overhead is minimized by compressing the HTTP header field
  • Add support for request priority and server push.

When optimizing http / 2 website performance, it is very important toUse as few connections as possibleHowever, the header compression mentioned in this paper is one of the most important reasons: the more requests and responses generated on the same connection, the more complete the dynamic dictionary is, the better the header compression effect will be. Therefore, for HTTP / 2 websites, the best practice is not to merge resources and hash domain names (because under each different domain name, you need to open a single TCP link and maintain a header dictionary value)

By default, the browser uses the same connection for these situations:

  • Resources under the same domain name;
  • Resources under different domain names meet two conditions: 1. Resolve to the same IP; 2. Use the same certificate

HTTP vs http2 details

Leave a Reply

Your email address will not be published.