Keep-alive mode for HTTP

Source: Internet
Author: User
Tags ack

First, keep-alive mode

1. 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 (the HTTP protocol is a non-connected protocol); Keep-alive mode (also known as persistent connection, connection reuse), the Keep-alive feature makes the client-to-server connection continuous, and when a subsequent request to the server occurs, the Keep-alive function avoids establishing or re-establishing the connection . That is, you can continuously send multiple copies of data in a single TCP connection without disconnecting.

2,

    by using the keep-alive mechanism, you can reduce the number of TCP connection builds and also mean that you can reduce the TIME_WAIT state connection to improve performance and increase the throughput of the HTTPD server (fewer TCP connections mean fewer system kernel calls, socket accept () and close () calls).

However, long-time TCP connections can easily cause system resources to become invalid. Improperly configured keep-alive can sometimes be more costly than reusing a connection. Therefore, it is important to set the Keep-alive timeout time correctly.

3. Keepalvie Timeout

HTTPD daemon, generally provides keep-alive timeout time setting parameters. Like Nginx's keepalive_timeout, and Apache's keepalivetimeout. This keepalive_timout time value means that an HTTP-generated TCP connection will need to hold keepalive_timeout seconds after the last response has been delivered before the connection is closed.

When the httpd daemon sends a response, it should actively close the corresponding TCP connection immediately, after setting up keepalive_timeout, the httpd daemon would like to say: "Wait, see if the browser has asked to come over", this is keepalive_ Timeout time. If the daemon has not received a browse send HTTP request during this waiting time, the HTTP connection is closed.

1). The life cycle of a TCP connection when keepalive_timeout time is 0 o'clock, that is, when keep-alive is not enabled:

In the absence of a keepalive_timeout setting, a socket resource must elapse from the time it was established to real release: Establish TCP connection + transmit HTTP request + PHP script execute + transmit HTTP response + Close TCP connection + 2MSL. (Note: The time here can only be referenced, the specific time is mainly by the network bandwidth, and response size depends on)

2). The life cycle of a TCP connection when keepalive_timeout time is greater than 0 o'clock, that is, when Keep-alive is enabled.

Set the keepalive_timout time situation, a socket set up to release the time required is more than keepalive_timeout time.

3). When the keepalive_timeout time is greater than 0, and multiple HTTP responses are sent on the same TCP connection.

Keepalive_timeout is the timing from the end of the last response.

When Keepalive_timeout is set, a socket is set up to release, which takes time: TCP establishes + (last response time – First request time) + TCP off + 2MSL. Red Bold indicates the time each request was sent, the execution time of each request script, the time each response was sent, and the time between 22 requests. Further testing, a TCP connection that is shutting down or time_wait state, cannot transmit HTTP requests and responses. That is, when a connection ends Keepalive_timeout timing, the server daemon sends the first FIN flag IP packet, and the connection is no longer available.

4). http keep-alive with TCP keep-alive

HTTP keep-alive with TCP keep-alive, not the same thing, the intention is not the same. The HTTP keep-alive is designed to allow TCP to live longer to transmit multiple HTTP on the same connection, increasing the efficiency of the socket. TCP Keep-alive is a fresh-keeping mechanism for TCP to detect TCP connection condition. TCP keep-alive freshness Timer, supports three system kernel configuration parameters:

1 echo 1800 >/proc/sys/net/ipv4/tcp_keepalive_time 2 echo >/PROC/SYS/NET/IPV4/TCP_KEEPALIVE_INTVL 3 echo 5 >/proc/sys/net/ipv4/tcp_keepalive_probes

KeepAlive is a TCP freshness timer , when a TCP connection is established on both ends of the network, idle idle (without any traffic between the two sides) Tcp_keepalive_time, the server kernel will try to send a detection packet to the client, To determine the status of the TCP connection (it is possible that the client crashes, the application is forcibly closed, the host is unreachable, and so on). If you do not receive an answer (ACK packet), you will try to send the detection packet again after TCP_KEEPALIVE_INTVL, until you receive an ACK to the other side, if you have not received the other side of the ACK, will try to tcp_keepalive_probes times, Each time interval is 15s, 30s, 45s, 60s, 75s, respectively. If you try to tcp_keepalive_probes and you still do not receive an ACK packet from the other, the TCP connection is discarded. The default idle time for TCP connections is 2 hours and is generally set to 30 minutes enough.

That is, only if the keepalive_timeout value of Nginx is set above Tcp_keepalive_time, and the last HTTP response transferred from this TCP connection, after Tcp_keepalive_time time, The operating system sends a reconnaissance packet to determine whether to discard the TCP connection. This is generally not the case unless you need to do so.

4, Keep-alive and time_wait

With HTTP Keep-alvie, you can reduce the number of server time_wait (because the connection is actively closed by the server-side httpd daemon). The simple truth is that, in contrast, with keep-alive enabled, fewer TCP connections are made, and the TCP connections that naturally need to be shut down are correspondingly less.

5. Use the different keepalive enabled. In addition, HTTP keepalive is the result of collaboration between the client browser and the server-side httpd daemon.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/82/9F/wKiom1ddBNGh7EVvAACYb0qKglg277.png "title=" 2013_ 12_20_02.png "alt=" Wkiom1ddbngh7evvaacyb0qkglg277.png "/>

Second, the application

HTTP is a typical example of a request <-> response pattern in which a client sends a request message to the server to respond to this information. In earlier versions of HTTP, each request was created with a new client-server connection, sending a request on the connection, and then receiving the request. One of the great advantages of such a model is that it is simple, easy to understand and programmed to implement; it also has a big drawback that it is inefficient, so keep-alive is proposed to solve the problem of low efficiency .

The keep-alive feature makes the client-to-server connection persistent, and when a subsequent request to the server occurs, the Keep-alive feature avoids establishing or re-establishing the connection. Most Web servers on the market, including Iplanet, IIS, and Apache, support HTTP keep-alive. This feature is often useful for websites that provide static content. However, there is another problem with heavier sites: 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 are still occupied. When the Web server and application server are running on the same machine, the Keep-alive feature has a particularly significant impact on resource utilization.

This feature is a feature of HTTP 1.1 presets, and HTTP 1.0 plus Keep-aliveheader can also provide the persistent function of HTTP.
Keep-alive:timeout=5, max=100
Timeout: Expires 5 seconds (the parameter in the corresponding httpd.conf is: keepalivetimeout), Max is a maximum of 100 requests, forced to disconnect, that is, in timeout time and a new connection, and Max will automatically minus 1, until 0, forced to break off.




This article is from the "Flower Open Shore" blog, please be sure to keep this source http://zxtong.blog.51cto.com/10697148/1788252

Keep-alive mode for HTTP

Related Article

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.