HTTP keep-alive Mode

Source: Internet
Author: User

1. What is keep-alive mode?

We know that the HTTP protocol uses "request-answer" mode, when using normal mode, that is, non-keepalive mode, each request/reply client and server to create a new connection, immediately after the completion of the connection (HTTP protocol is a non-connected protocol) When using Keep-alive mode (also known as persistent connection, connection reuse), the Keep-alive feature keeps the client-to-server connection active, and the keep-alive feature avoids establishing or re-establishing a connection when a subsequent request to the server occurs.

HTTP 1.0 is closed by default, you need to add "connection:keep-alive" in the HTTP header to enable keep-alive;

Keep-alive is enabled by default in HTTP 1.1 and is closed if "connection:close" is added.

Most browsers now use the http1.1 protocol, which means that the Keep-alive connection request is initiated by default, so whether a full keep-alive connection can be completed depends on the server setup.


2. What is long connection, short connection?

In http/1.0, a short connection is used by default. That is, the browser and the server each HTTP operation, the connection is established, but the end of the task to disconnect. If a client browser accesses an HTML or other type of Web page that contains other Web resources, such as JavaScript files, image files, CSS files, and so on, when the browser encounters such a Web resource, an HTTP session is established.

However, from http/1.1, a long connection is used by default to maintain the connection characteristics. Using a long-connected HTTP protocol, this line of code is added to the response header:

Connection:keep-alive

In the case of a long connection, when a Web page opens, the TCP connection between the client and the server for transmitting HTTP data does not close, and if the client accesses the Web page on the server again, it will continue to use this established connection. Keep-alive does not permanently keep the connection, it has a hold time that can be set in different server software (such as Apache). Long connections are supported for both the client and the server.

The long and short connections of the HTTP protocol are essentially long connections and short connections to the TCP protocol.


2.1 TCP Short Connection

We simulate the case of a TCP short connection, the client initiates a connection request to the server, the server receives the request, and the two sides establish the connection. The client sends a message to the server, the server responds to the client, and then a read-write is completed, and either of the parties can initiate a close operation, but usually the client initiates the close operation. Why, the general server will not reply to the client immediately after closing the connection, of course, do not rule out a special situation. From the above description, short connections generally only pass a read and write operation between Client/server

The advantage of short connections is that they are simpler to manage, that existing connections are useful connections, and that no additional control is required

2.2 TCP Long Connection

Next we simulate the case of a long connection, the client initiates a connection to the server, the server accepts the client connection, and the two sides establish the connection. After the client and server have completed a read and write, the connection between them is not actively closed, and subsequent read and write operations continue to use the connection.

First of all, the TCP/IP feature described in the TCP/IP details, the KeepAlive function is mainly provided for the server application, the server application wants to know whether the customer host crashes, which can be used on behalf of the customer resources. If the customer has disappeared, leaving a semi-open connection on the server while the server is waiting for data from the client, the server should be waiting for the client's data, and the KeepAlive function is trying to detect this semi-open connection on the server side.

If a given connection does not have any action within two hours, the server sends a probe segment to the client and the client host must be in one of the following 4 states:

    1. The client host is still functioning and can be reached from the server. The client's TCP response is normal, and the server knows the other side is normal, the server after two hours will be keepalive timer reset.

    2. The client host has crashed and is shutting down or restarting. In either case, the client's TCP is not responding. The server will not receive a response to the probe and time out after 75 seconds. The server sends a total of 10 such probes at each interval of 75 seconds. If the server does not receive a response, it considers the client host to be closed and terminates the connection.

    3. The client host crashed and has restarted. The server receives a response to its keepalive probe, which is a reset that causes the server to terminate the connection.

    4. The client runs normally, but the server is unreachable, which is similar to 2, where TCP can find no response to the probe.




This article from the "777 Fast" blog, declined reprint!

HTTP keep-alive Mode

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.