Heavy for light root, quiet for manic king. Lao tzu

Http series (-) History of Http

Http series ii: Multiplexing in Http2

Http/Tcp Three-way handshake and four-way wave

Http series (4) Http Get/Post differences

Introduction to the


Multiplexing replaces the original sequence and blocking mechanism, and all requests are done concurrently over a TCP connection. It also solves the problem of browsers limiting the number of requests under the same domain name.

In HTTP/2, with binary framing, HTTP/2 no longer relies on TCP links for multi-stream parallelism:

  • All communication under the same domain name is completed on a single connection. The same domain name occupies only one TCP connection, and multiple requests and responses are sent in parallel using the same connection.
  • A single connection can host any number of two-way data streams, and requests and responses can be interleaved in parallel on a single connection without interference.
  • The data stream is sent as a message, which in turn consists of one or more frames that can be sent out of order because they can be reassembled according to the stream identifier at the head of the frame. Each request can have a priority value of 31 bits. 0 indicates the highest priority. A higher value indicates a lower priority.

Frames and streams


In HTTP/2, there are two very important concepts: frames and streams.

The frame (frame)

The smallest unit of data transfer in HTTP/2, so frames not only subdivide the parts of HTTP/1.x, but also optimize the areas where HTTP/1.x is poorly expressed, and add ways in which HTTP/1.x cannot be expressed. Each frame contains several fields, such as Length, Type, Flags, Stream Identifier, frame Playload, etc. Type represents the type of frame. There are 10 different types defined in HTTP/2. Include HEADERS frames and DATA frames mentioned above. In addition: PRIORITY RST_STREAM SETTINGS PUSH_PROMISE PING RTT GOAWAY WINDOW_UPDATE (flow control) CONTINUATION (CONTINUATION of header data)

In HTTP 2.0, it divides the two parts of a datagram into header frames and data frames. That is, header frames and data body frames.

Stream (stream)

Flow: A virtual channel that exists in a connection. Streams can carry two-way messages, and each stream has a unique integer ID. Packets in HTTP/2 long connections are not sent in request-response order, and a complete request or response (called a stream, with each stream having a unique number) may be sent in discrete batches. It has the following characteristics:

  • Bidirectional: Data can be sent and received simultaneously within the same stream.
  • Orderliness: The data being transmitted in a stream is binary frames. Frames are sent and received sequentially on a stream.
  • Parallelism: Binary frames in a stream are transmitted in parallel without waiting in sequence.
  • Creation of streams: Streams can be created, consumed, or shared unilaterally by clients or servers.
  • Stream closing: A stream can also be closed by either party.
  • The HEADERS frame precedes the DATA frame.
  • If the stream ID is odd, it means that the stream is initiated by the client, which is the standard, then the stream ID is even.

The development course


To send multiple requests from Http/0.9 to Http/2, from multiple Tcp connections =>keep-alive=> pipelining => multiplexing continuously reduces the performance cost of creating Tcp multiple times and so on.

Multiple Tcp connections

In the early days without keep-alive, only multiple Tcp connections could be created to make multiple requests. The effect of multiple HTTP requests is shown below:

Once a request is completed, the Tcp connection will be closed, and the next request must be re-established Tcp connection to complete data transmission and then closed, resulting in a large performance loss.

Keep-Alive

The core problem that Keep-Alive solves is that only one HTTP request is established for the same domain name within a certain period of time. Other requests can reuse the connection channel established each time to improve the request efficiency. The specified time is configurable regardless of whether you are using Apache or Nginx. In the past, the browser determined whether the response data was received by checking whether the connection was closed. This is not the case with persistent connections, which requires that the server’s response header to a persistent connection must return the content-Length identifier that identifies the length of the body for the browser to determine the bounds. Sometimes, the Content-Length method is not very accurate, and transfer-encoding can also be used: the chunked header sends strings of data, ending with a chunked flag of length 0. The effect of multiple HTTP requests is shown below:

Above: Set Connection: keep-alive to Keep the Connection open for a period of time.

Keep-alive still has the following problems:

  • Serial file transfer.
  • Block (6-8) due to concurrent request restrictions in the same domain

pipelines

HTTP pipelines can be overcome with the domain of the disruption caused by parallel requests limit, it is based on a persistent connection, is to put all the requests were sent to the server, but the server need to follow a sequence by a response, rather than wait until a response is returned to the next request, thus saves a lot of time to request to the server. However, HTTP pipelining still has the problem of blocking. If the previous response is delayed, subsequent responses will be blocked.

Above: HTTPpipelining: Build multiple connections

multiplexing

Multiplexing replaces the original sequence and blocking mechanisms. All requests are made concurrently over a TCP connection. Because before multiplexing all transmission is based on basic text, in multiplexing is based on binary data frame transmission, message, stream, so it can be done out of order transmission. Multiplexing is stream-based for all requests under the same domain name, so there is no concurrent blocking in the same domain. Multiple requests are shown below:

Image above: multiplexing

conclusion

In HTTP/2, there are two very important concepts: frame and stream.

A frame represents the smallest unit of data, and each frame identifies which stream it belongs to. A stream is a data stream composed of multiple frames.

HTTP2 uses binary data frames instead of http1.x’s text format, which is more efficient for parsing.

Multiplexing replaces the sequence and blocking mechanism of HTTP1.x, where all requests for the same domain name are completed concurrently over the same TCP connection. Multiple requests can be sent in the same Tcp, and the peer end can know which request belongs to by the identifier in the frame. By using this technique, the queue header blocking problem in older VERSIONS of HTTP can be avoided and the transmission performance can be greatly improved.

reference

multiplexing

Read HTTP/2 and HTTP/3 features

Read HTTP/2 features

Analysis of HTTP/2 multiplexing