The keepalive difference between HTTP keep-alive and TCP

Source: Internet
Author: User
Tags ack

Original address: http://blog.csdn.net/oceanperfect/article/details/51064574

1. HTTP keep-alive
In the early days of HTTP, each HTTP request required an TPC socket connection to open, and the TCP connection was disconnected after use once.
Using keep-alive can improve this state by continuously sending multiple copies of data in a single TCP connection without disconnecting. 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 the close () call.
However, Keep-alive is not a free lunch, and long-time TCP connections can lead to invalid system resource usage. 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.
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.
2. TCP KEEPALIVE
After the link is established, if the application or the upper layer protocol has not sent data, or a long time to send the data, when the link is long no data message transfer how to determine the other side is online, in the end is dropped or really no data transmission, links need not be maintained, This is a scenario that needs to be considered in the TCP protocol design.
TCP protocol through a clever way to solve this problem, when more than a period of time, TCP automatically send a message to the other side, if the other party responded to this message, indicating that the other side is also online, the link can continue to maintain, if the other party did not return the message, and retry the link is considered to be lost, There is no need to keep the link.
3, HTTP keep-alive and 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 attempt to send a detection packet to the client to determine the TCP connection status ( It is possible that the client crashes, the app 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. Diagram of HTTP keep-alive

Reference link Address: http://www.bubuko.com/infodetail-260176.html

http://www.nowamagic.net/academy/detail/23350305

The keepalive difference between HTTP keep-alive and TCP

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.