Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between keep-alive and HTTP/2 multiplexing in HTTP/1.x?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces "what is the difference between HTTP/1.x 's keep-alive and HTTP/2 multiplexing?" The relevant knowledge, in the actual case operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

Introduction

This paper is divided into the following three parts: step by step into keep-alive and HTTP/2 multiplexing of HTTP/1.x:

What is HTTP/1.x keep-alive?

HTTP/2 multiplexing

Difference between HTTP/1.x keep-alive and HTTP/2 Multiplexing

Let's get started.

What is HTTP/1.x keep-alive?

In an article into TCP and HTTP, we have introduced that HTTP protocol is an application layer protocol based on TCP protocol. HTTP protocol was originally a very simple protocol, and the communication mode is also a simple request-reply mode, that is, every request between the client and the server needs to create a TCP connection, and the server disconnects the TCP connection after the response, and then requests to be disconnected.

In HTTP/0.9 and early HTTP/1.0, this is the default, but this frequent creation and disconnection is undoubtedly a great consumption of performance.

The cost of building a TCP connection is high because the client and server need a "three-way handshake" to establish a connection, send three packets, need one RTT; to close the connection is "four waves", four packets need two RTT, and the initial sending rate is slow (slow start). As more and more external resources are loaded on the web page, this problem becomes more and more prominent.

Therefore, HTTP/1.0 introduces keep-alive persistent connection, which is turned off by default in HTTP/1.0 and can be enabled by Connection: keep-alive;. HTTP/1.1 is enabled by default, with or without Connection: keep-alive.

The so-called persistent connection means that when HTTP requests to establish a TCP connection, the request ends, the TCP connection continues to open, and continues to remain for a period of time (timeout). During this period, the same client sends a request to the server to reuse the TCP connection, and resets the timeout time counter, and can continue to reuse TCP during the next timeout time. This undoubtedly omits the wear and tear of repeatedly creating and destroying TCP connections.

Will TCP disconnect immediately after the timeout time is up?

If no data is received from the customer in two hours (timeout), the server sends a probe message segment, which is then sent every 75 seconds. If there is still no response from the client after sending 10 probe segments in a row, the server thinks that the client has failed and then closes the connection.

-- extracted from Xie Xiren's "computer Network"

HTTP/2 multiplexing

Why does HTTP/2 introduce multiplexing?

This is because:

Although HTTP/1.x introduces keep-alive persistent connection, each request must wait for the last response before it can be initiated.

Therefore, a pipeline mechanism is proposed in HTTP/1.1 (not enabled by default). The next request does not need to wait for the last response before it is sent, but this requires the server to return the response in the order in which the request is sent. When multiple files are requested sequentially, when one of the requests is blocked for some reason, all the requests queued later are also blocked, which is called Head-Of-Line Blocking.

People have taken many methods to solve the problem, such as using multiple domain names, introducing sprite images, inlining small images, etc., but they have not solved the problem fundamentally.

How does HTTP/2 do it?

First of all, it introduces frame (frame) and stream (stream), because HTTP/1.x is based on text, because it is text, it must be a whole, it is uncut in transmission, and can only be transmitted as a whole.

Since HTTP/2 is based on a binary stream, it can decompose HTTP messages into separate frames, interleave them, and then reassemble them at the other end by identification in the frame, which is called multiplexing.

This realizes that in the same TCP connection, multiple requests and responses can be sent at the same time, and there is no need to correspond one by one in order, even if a request task is time-consuming, it will not affect the normal execution of other connections.

Difference between HTTP/1.x keep-alive and HTTP/2 Multiplexing

To sum up, the difference between HTTP/1.x keep-alive and HTTP/2 multiplexing:

HTTP/1.x is text-based and can only be transmitted as a whole; HTTP/2 is based on binary stream and can be decomposed into independent frames and interleaved

HTTP/1.x keep-alive must return the response in the order in which the request is sent; HTTP/2 multiplexing does not respond in order

In order to solve the blocking at the head of the line, HTTP/1.x keep-alive distributes the resources of the same page to different domain names and opens multiple TCP connections. All communication between HTTP/2 and the domain name is completed on a single connection.

HTTP/1.x keep-alive a single TCP connection can only process one request at a time (the lifecycle of two requests cannot overlap); HTTP/2 a single TCP can send multiple requests and responses at the same time

"what is the difference between HTTP/1.x 's keep-alive and HTTP/2 multiplexing?" This is the end of the introduction, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report