Turn: What is the difference between Connection:close and connection:keep-alive?

Source: Internet
Author: User

Original: http://www.cnblogs.com/TinyMing/p/4597136.html

One, the problem phenomenon: a JSP page, incredibly takes more than 40 seconds. CSS with a large number of images in the Web page
Problem solving: The reason also looked for half a day, originally Apache configuration inside, put keep-alive switch off. This is a big problem, why the engineer to close it, the original he considered too simple, we know that Apache is suitable for short-connection requests, processing time is shorter, concurrency can go up, the original he is so considered, but there is no way, can only do so, or open the keep-alive switch bar.
Of course, not all cases are set keepalive to ON, the following text summarizes the better:
"In the process of using Apache, the KeepAlive attribute I kept as the default value on, in fact, whether the property is set to on or off or to specific problems specific analysis, in the production environment, the impact is quite large.
What is the use of the keepalive option? If you've ever used MySQL, you should know that there is a persistent Connection similar to keepalive in the connection properties of MySQL: Long connection (pconnect). If this property is turned on, a TCP connection can be made more than once for the same user, increasing the response speed.
For example, many Web pages in the image, CSS, JS, HTML are on a server, when the user access to the HTML page, the page image, CSS, JS all constitute the access request, open the KeepAlive property can effectively reduce the number of TCP handshake ( Of course, the browser has a limit on the number of simultaneous requests in the same domain, generally 2 see the following article section to reduce the cost of domain name interpretation, reduce the number of httpd processes, thereby reducing the use of memory (assuming prefork mode). The Maxkeepaliverequests and KeepAliveTimeout two properties work at KeepAlive =on, which controls the lifetime and maximum number of service requests for a persistent connection.
However, the above is only a situation, that is, static Web pages are mostly in the case, and the Web pages of other requests and Web pages on the same server. When your application dynamic program (for example: PHP) is mostly, the user access by the dynamic program to generate HTML content, HTML content in the image material and CSS, JS, etc. less or hashed on other servers, KeepAlive =on will degrade Apache performance. Why is it?
As mentioned earlier, KeepAlive =on, each time a user accesses a TCP connection, Apache will keep the connection for a period of time so that the connection can serve the same client continuously. Before KeepAliveTimeout expires and maxkeepaliverequests is not yet up to the threshold, Apache must have a httpd process to maintain the connection, and the httpd process is not cheap and consumes memory and CPU time slices. If the current Apache response 100 users per second access, keepalivetimeout=5, at this time httpd process is 100*5=500 (prefork mode), a httpd process consumes 5 m of memory, is 500*5m=2500m= 2.5G, is it exaggerated? Of course, Apache has only 100 TCP connections to the client. If your memory is large enough, the system load will not be too high, if your memory is less than 2.5G, you will use swap, frequent swap switching will increase the load on the CPU.
Now we turn off keepalive, Apache still responds to 100 users per second, because we separate the picture, JS, CSS, etc., each access only 1 request, at this time the number of httpd process is 100*1=100, using memory 100*5m= 500M, this time the Apache and the client also made 100 TCP connections. Performance has increased too much.
Second, summary
1, when your server memory is sufficient, KeepAlive =on or off has little effect on system performance.
2, when the static Web page on your server (Html, graphics, CSS, Js) is mostly, it is recommended to open keepalive.
3, when your server is a dynamic request (because connected to the database, more access to the file system), KeepAlive off, will save a certain amount of memory, save memory just can as the file system cache (Vmstat command in the cache column), reduce I/O pressure.
PS: When KeepAlive =on, keepalivetimeout settings is actually a problem, set too short, will lead to Apache frequent connection, to the CPU pressure, set too long, the system will accumulate useless HTTP connection, consumes a lot of memory, The number of settings can be adjusted constantly, depending on your site browsing and server configuration.
Third, reduce the cost of domain name interpretation
For http/1.0, can take full advantage of the browser default maximum number of concurrent connections more than http/1.1 benefits, to achieve the cost of not adding new domain name and higher parallel download, reduce the cost of Domain name interpretation (Note: IE 6,7 in http/1.0 the default maximum number of concurrent connections is 4, in http/ 1.1 The default maximum number of concurrent connections for 2,IE8 is 6,firefox2 in http/1.0, the default maximum number of concurrent connections is 2 in http/1.1, the default maximum number of concurrent connections is 8,firefox 3 default is 6), According to the July 10 Google index 4.2 billion pages of the statistical report, each page contains 29.39 images, 7.09 external scripts, 3.22 external CSS style sheets, if the keep-alive and reasonable control keep-alive Timeout This parameter can save a lot of connection cost, improve the corresponding speed. If the settings are not good, in the case of large concurrency, because of the maintenance of a large number of connections to the server resources exhausted, and for most of the current domestic users are still ie6,7 in the case of the shutdown keep-alive can take full advantage of the browser default maximum number of concurrent connections to achieve the benefits of no additional overhead page display quickly.

Turn: What is the difference between Connection:close and connection:keep-alive?

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.