HTTP persistent connection

Source: Internet
Author: User
Talk about persistent links-Understanding HTTP original excellent Guo Shiyu with baozou comics 2 from: NS testing department time 637

Persistent connection

What is persistent connection? As the name implies, it is a "persistent" connection. As mentioned before, in order to complete an HTTP transaction, a TCP connection must be established between the server and the client to transmit packets. After the transaction ends, it is usually directly closed, which is a normal mode. HoweverThis will reduce the network usage efficiency.Why?There are several reasons:

  1. Every time a connection is established, it must go through three handshakes and other necessary programs. If we have a connection that can be used all the time, it means that we only need to establish one connection, this saves the time required to establish a connection.

  2. As mentioned in the previous article, connections used will be faster than new connections. This is due to the slow start of TCP connections. Each time a new connection is established, of course, the connection speed is not as fast as it has been transferred.

  3. Each connection is a burden on the server and client, and can be opened as little as possible without affecting functions and experience.

Nowadays, many solutions will adoptPersistent connection + new connection combinationThis method reduces the waste of new connections as much as possible. At the same time, when the existing connection cannot meet the requirements, it can be set up to meet the needs, more flexible. There are two types of persistent connections:HTTP/1.0 + keep-alive and HTTP/1.1 persistent.

  • Keep-alive

Let's take a look at keep-alive, first:

This is an HTTP transaction on the Baidu homepage. We can see that there is a connection: keep-alive, which is the first persistent connection. Next let's take a look at how this persistent connection is established:

 

This is the request process:The client sends a request to the server in the form of connection: keep-alive. If the Server accepts the request, the response will contain connection: keep-alive.

When connection: keep-alive is used, you can use the keep-alive header to pass some parameters about persistent connections:Timeout indicates the duration, and Max indicates the number of HTTP services that you want to transmit over this persistent connection.But none of themCommitment value, that is, you can repent at any time..

Next, let's talk about some notes for the keep-alive persistent connection:

  1. If a persistent connection is used, the Content-Length header that describes the length of the subject must be correct, because the persistent connection continuously transmits HTTP transactions, the demarcation points between consecutive HTTP tasks are determined by the length of the subject indicated by Content-Length. If an error occurs or the length of the subject is not indicated, therefore, we cannot know where the transaction ends.

  2. The connection: keep-alive header must be deleted before forwarding by the proxy and network administrator. This involves the problem of dumb proxy, which will be discussed later.

  3. The persistent connection can be closed at any time, so you must be prepared to disconnect the persistent connection when the request is sent and the response is not sent back, that is, in some cases, you may need to send a new request.

Dumb proxy and smart proxy

Here, we will first describe the difference between a dumb proxy and a smart Proxy: a dumb proxy simply forwards requests and cannot parse or maintain persistent connections, A smart proxy can parse received packets and maintain persistent connections.

For example, if there is a proxy that does not parse direct forwarding between the client and the server, the connection: keep-alive header is directly forwarded to the server. After the server receives the request, A response with connection: keep-alive will be sent to the client. Similarly, the blind proxy will not parse the response and directly forward all the response back to the client. Because the client receives this header, it thinks that the persistent connection has been established successfully, but the "Stupid proxy" in the middle does not know these things,The stupid proxy has only one behavior mode: after the request is forwarded and the return server responds to the request, the transaction is deemed to have ended and the connection is closed.Because the connection: keep-alive header has been sent to the server and client, both parties believe that the persistent connection has been established,In this way, the two sides think that the persistent connection is OK and the middle of the dumb proxy is waiting for the connection to be disconnected.In this case, if the client sends a request to the connection again, the request will be stopped at the sub-proxy,Because the dumb proxy is waiting for the connection to close. This status will cause the browser to remain suspended until a connection in the client or server times out and closes the connection, A wonderful combination of hands is gone (the dumb proxy is to forward the content intact to the proxy ).

To avoid this situation, the modern proxy will not forward the connection: keep-alive header.

To prevent this problem, Netscape proposes a solution by inserting proxy-connection ,:

Let's take a look at how this solution solves the problem:

  1. First, the client sends the proxy-connection first request, which is a non-standard request. That is to say, even if the server receives this header, it does not know what he is doing, in the eyes of the server, it only knows that the header of the client applying for a persistent connection is connection: keep-alive.

  2. Dumb proxy Mode: When the report comes to the proxy location, the dumb proxy will directly forward the request without parsing, then the proxy-connection header will be directly sent to the server because the server does not recognize it, therefore, the header is directly ignored, so that the server does not carry the connection: keep-alive header when returning the response. When the response arrives at the client, the client finds that there is no connection in the response: the keep-alive header indicates that the server rejects the persistent connection request. In other words, the client determines whether the server accepts the persistent connection request by responding to the connection header.

  3. Smart proxy Mode: A smart proxy will parse the request and replace the header with connection: keep-alive when the proxy-connection header is found,Because the server can only recognize the connection HeaderWhen it finds this header, it knows that the client has made a persistent connection request and adds the connection header to the response and sends it back to the client. When the client receives a response with the connection header, it deems that the persistent connection is successfully established, and the smart routing in the middle can also maintain the persistent connection, in this way, the entire connection is in the OK proxy OK server OK State on the client. You can continue to use this persistent connection to send packets.

As mentioned above, I think this solution is equivalent to making a judgment on the type of intermediate proxy.

However, this solution can only solve the problem of only one proxy in the middle. If either side of the smart side still has a dumb proxy, the initial problem of the dumb proxy will still occur..

  • Persistent

The persistent connection of HTTP/1.1 is enabled by default. The connection is closed only when the header contains connection: close. Of course, the server and client can still close the persistent connection at any time.

After the connection: Close header is sent, the client cannot send more requests on the connection. Of course, according to the persistent connection feature, the correct Content-Length must be transmitted.

In addition, according to the characteristics of HTTP/1.1, it is not necessary to establish a persistent connection with the HTTP/1.0 client. Finally, we must prepare for the re-release.

Pipeline Connection

HTTP/1.1 allows the use of pipelines on persistent connections, so that the second request is sent directly on the pipeline without waiting for the response of the previous request. This improves performance with high latency.

Restrictions on pipe connection:

  • Pipelines cannot be used if they are not persistent connections.

  • The response must be returned in the same sending order, because the packets do not have tags, and the order may be messy.

  • Because persistent connections can be disabled at any time, you need to prepare for re-sending at any time.

  • You should not use the pipeline to send repeated requests with side effects (such as post and repeated submission ).

That's it. It's so tired...

HTTP persistent connection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.