Keep-alive detailed

Source: Internet
Author: User

As we all know, the HTTP protocol uses "request-answer" mode, when using normal mode, that is, non-keep-alive 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 keep-alive avoids re-establishing the connection when subsequent requests to the server occur.

In the old HTTP version, each request is created with a new client-to-server connection, a request is sent on the connection, and then the request is accepted. This model has a great advantage, it is very simple, easy to understand and programming implementation, it also has a big drawback, it is inefficient, keep-alive is used to solve the problem.

Keep-alive makes the client-to-server connection persistent, and when subsequent requests to the server occur, the Keep-alive feature avoids establishing or re-establishing the connection. Most Web servers now support keep-alive. This feature is often useful for websites that provide static content. However, for heavier web sites, there is another problem: while retaining open connections for customers has some benefits, it also affects performance because the resources that could have been freed during the processing pause were made obsolete. This feature is HTTP1.1 preset, but on HTTP1.0 we add Keep-aliveheader to use this feature as well.

Let's give an example:

keep-alive:timeout=5,max=100

The expiration time is 5 seconds, Max is a maximum of 100 requests, forcing the connection to disconnect, that is, every new request in timeout time, Max will automatically subtract 1 until 0, forcing the connection to be broken.

In the HTTP1.0 version there is no official standard to specify how keep-alive works, so in fact he is attached to the HTTP1.0 protocol, if the client browser supports keep-alive, then add a field to the HTTP request: Connection:keep-alive, when When the server receives a request with a connection:keep-alive attached, it also adds the same field to the response header to use keep-alive. As a result, the HTTP connection between the client and the server is maintained and is not broken (unless the time specified by timeout is exceeded), and the established connection is used when the client sends another request.

In the HTTP1.1 version, the official keep-alive uses a somewhat different version of the standard and http/1.0, and by default all connections in http/1.1 are persisted, unless indicated in the request header or response header to be closed: connection:close.

HTTP is a stateless protocol, which means that each request is independent and keep-alive failed to change the result. In addition, Keep-alive does not guarantee that the connection between the client and the server must be active, as is the case in http/1.1. The only guarantee is that when the connection is closed you can get a notification, so we should not let the program rely on keep-alive to maintain connectivity, otherwise it will have some unintended consequences.

By using the keep-alive mechanism, you can reduce the number of times a TCP connection is established, and this can reduce the TIME_WAIT state connection to improve performance and increase the throughput of the HTTP server (fewer TCP connections mean fewer system kernel calls, The socket's accept () and close () call). However, long-time TCP connections can lead to invalid system resource occupancy, and improper configuration of keep-alive is more costly than reusing connections. So it is important to set keep-alive timeout time correctly.

Let's say this keep-alive timeout parameter:

HTTPD Daemon, generally provides the keep-alivetimeout time setting parameters. This time value means that an HTTP-generated TCP connection will need to remain keep-alive timeout seconds after the last response has been delivered before the connection is closed.

When the httpd daemon sends a response, it should immediately actively shut down the corresponding TCP connection, set Keepalive_timeout, the httpd daemon will say: Wait, see if the browser has requested to come over, this waiting time is keepalive_ Timeout time, if the daemon has not received an HTTP request from the browser during this waiting time, close the HTTP connection.

In short, we should better use this mechanism to bring us some benefits.


















Keep-alive detailed

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.