HTTP / 2 study
My understanding of HTTP /2 is still at the rote level of the interview, so I recently made a summary of some questions in my mind and my understanding, which may be wrong. If a bigwig thinks there’s a problem, just say so.
The full introduction to HTTP /2 is numerous. HTTP /2 introduction will not be repeated.
Question 1: Why does HTTP /2 use the binary framing layer
Binary protocol understanding:
The first thing we need to understand is the difference between http1.1’s text protocol and HTT/P2’s binary protocol. In fact, as a brick remover with a shaky foundation, WHEN I saw this binary, I thought: Isn’t everything in the computer world binary? Why are there binary and text protocols?
With this question in mind, I found some responses online, so I’ll just move on
Binary protocol versus text protocol isn’t really about how binary blobs are encoded. The difference is really whether the protocol is oriented around data structures or around text strings. Let me give an example: HTTP. HTTP is a text protocol, even though when it sends a jpeg image, it just sends the raw bytes, not a text encoding of them.
Grade 4 low pass level
The difference between binary and text protocols is not about how binary BLObs are encoded. The key is whether the protocol is oriented toward data structures or text strings.
Text protocol request: All content is text
Binary request:
struct request {
int requestType;
int protocolVersion;
char path[1024];
char user_agent[1024];
char host[1024];
long int accept_bitmask;
long int language_bitmask;
long int charset_bitmask;
};
Copy the code
When we look at the HTTP text protocol and the structure of binary requests:
- Binary protocols are more compact than text protocols.
- Text protocol parsing requires special characters, such as \r\n, which on the one hand makes the text protocol more verbose. On the other hand, using this special character to parse a paragraph of text is not as convenient and fast as binary parsing.
- A binary number takes up less space than a text number: for example, the HTTP status code 200 is “2”, “0”, and “0” in the text protocol, and these three digits are encoded separately. However, in the binary protocol, the binary number is not encoded in the text format, but can be directly represented in the binary number: 11001000.
The binary framing layer in HTTP /2
Refer to the article
At the core of all performance enhancements of HTTP/2 is the new binary framing layer, which dictates how the HTTP messages are encapsulated and transferred between the client and server.
In many articles, the binary framing layer in HTTP /2 has been described as the key to HTTP /2 performance, as it makes it possible for a SINGLE TCP connection to send multiple requests simultaneously.
HTTP/1.1 and HTTP/2 are both tcp-based protocols. The TCP model is a two-way data flow, and any protocol that processes more than one request on a TCP connection needs to solve two problems:
- Sharding – How do I split multiple requests and responses in a flow into separate messages
- Correspondence – How do requests and responses correspond
In http1.1, a TCP request can only be completed at the same time a request response process, so the request and response naturally one-to-one correspondence, there is no response and request can not match the problem. However, when we open the packet capture tool to view the corresponding HTTP packets, we will find that there is no field corresponding to the request. This creates a problem for HTTP /2 multiplexing. Multiple requests, multiple responses in a TCP connection. How do you map those responses to the requests? Fields can only be added at the binary frame layer, so one of the fields in the HTTP /2 frame format is the Stream Identifier. This Stream ID can be used to map the response to the request. This fundamentally solves the problem of multiplexing, request and response matching.
So to sum up the question:
- Binary protocols are more compact than text protocols and take less space.
- The frame-splitting layer is equivalent to HTTP shred, with more flexibility, such as the ability to do separate special processing for header frames.
- The frame layer has its own header, and the Stream Identifier in it enables the operating system to match multiple responses and requests one by one, which is key to HTTP /2 performance, namely multiplexing.
- The binary framing layer exists for the purpose of splitting requests or responses, allowing for more flexible processing. The message format defined by the frame layer, such as Stream Identifier, is directly related to the implementation of various optimization schemes of HTTP /2.
Problem 2: the relationship between TCP and HTTP /2 frames
In HTTP /2 frame is the minimum unit of communication, my question is HTTP /2 frame and the smallest TCP packet is what relation, one to one correspondence? How does TCP split and assemble messages in HTTP /2 requests?
The interpretation of multiplexing in many interview questions
The explanation is as follows:
- All communication under the same domain name is completed on a single connection, eliminating the delay and memory consumption caused by multiple TCP connections.
- Requests and responses can be interleaved in parallel on a single connection, with no interference between them
In the explanation below, request and response are interleaved in parallel. In fact, the parallel request should be a parallel HTTP request, so can TCP split the request into parallel requests?
This starts with how TCP is assembled to split HTTP packets,
TCP packets are in the IP packet payload. It also requires a minimum of 20 bytes of header information, so the maximum payload of TCP packets is 1480-20 = 1460 bytes. Because IP and TCP protocols tend to have additional headers, the ACTUAL TCP payload is about 1400 bytes. Thus, a single 1500 byte message requires two TCP packets
Let’s assume two POST requests are made in parallel:
If THE TCP protocol layer parallel requests, and the order is out of order, is that ok?
In my opinion, this is not possible, because TCP assembles packets entirely sequentially, and if parallel requests are made and packets are thrown out of order, the computer will not know how to assemble them.
So the above parallel request is not accurate, in the computer must be serial request, send a request packet, send the next request packet, so as to ensure that the packet combination is correct.
So frames in HTTP /2 are requested sequentially, without which the data would not be assembled, one-to-one.
To sum up:
- Frames in HTTP /2 are the minimum unit of communication, but only at the HTTP /2 protocol layer. A frame in HTTP /2, if too informative, may consist of multiple TCP splits, so the concept of a frame in HTTP /2 does not correspond to a packet in a network protocol.
- One or more TCP packets constitute a frame, and one or more frames constitute an HTTP /2 request or response.
- HTTP /2 A TCP connection does not have parallel transmission, but is a serial transmission. After sending a packet of one frame, the packet of the next frame is sent.
Please feel free to comment if you don’t understand