Non-negligible keep alive

Source: Internet
Author: User
Tags keep alive

During webpage development, keep-alive is an important attribute in HTTP. We all know that HTTP is built on TCP. In the early implementation of HTTP, each HTTP request must open a socket connection. This is very inefficient because many HTTP requests on a web page point to the same server. For example, many requests initiated for images on Web pages direct to a common image server. The introduction of persistent connections solves the problem of low efficiency of socket connections caused by many requests to the server. It allows the browser to connect multiple requests separately. The browser and server use the connection header ilai to indicate the support for keep-alive.

 

The author encountered a problem with keep-alive last year:

 

Symptom:A JSP page takes more than 40 seconds. CSS with a large number of images in the webpage

 

Solution:The reason was also found for a long time. In the original Apache configuration, the keep-alive switch was disabled. This is a big problem. Why should the engineer shut it down? It was too simple for him to consider. We know that Apache is suitable for requests with short connections. The shorter the processing time, the higher the concurrency, it turned out that he thought so, but there is no way to do this. Let's turn on the keep-alive switch.

 

Of course, keepalive is not set to on in all cases. The following text is a better summary:

 

[In useApacheIn the process,KeepaliveI keep the attribute as the default value on. In fact, whether this attribute is set to ON or OFF is still subject to specific analysis. The impact in the production environment is still quite large.

KeepaliveWhat is the purpose of the option? If you have used MySQL, you should know that the connection attribute of MySQL isKeepaliveSimilar persistent connection, that is, persistent connection ). If this attribute is enabled, a TCP connection can be used as the request service for the same user multiple times, improving the response speed.

For example, images, CSS, JS, and HTML in many webpages are stored on one server. When a user accesses an HTML webpage, the images, CSS, and Js in the webpage constitute an access request.KeepaliveAttribute can effectively reduce the number of TCP handshakes (of course, the browser has a limit on the number of images simultaneously requested in the same domain, generally 2), reducing the number of httpd processes, this reduces memory usage (assuming the prefork mode ). The maxkeepaliverequests and keepalivetimeout attributes areKeepalive= On, which can control the survival time of persistent connections and the maximum number of service requests.

however, the above is only one case, that is, when there are many static webpages, and other requests on the webpage are on the same server as the webpage. When most of the Programs (such as PHP) in your application are dynamic, the dynamic program generates HTML content in real time during user access, keepalive = on reduces the performance of apache . Why?

As mentioned earlier, keepalive = on, open a TCP connection. apache will keep the connection for a period of time, so that the connection can continue to serve the same client. Before keepalivetimeout expires and maxkeepaliverequests has not reached the threshold, apache must have an httpd process to maintain the connection. The httpd process is not cheap and consumes memory and CPU time slices. Assume that the current apache responds to 100 user visits per second, keepalivetimeout = 5, at this time, the number of httpd processes is 100*5 = 500 (prefork mode). If an httpd process consumes 5 MB of memory, it is 500*5 m = 2500 m = 2.5 GB. Is that an exaggeration? Of course, apache and the client only perform 100 TCP connections. If your memory is large enough, the system load will not be too high. If your memory is less than 2.5 GB, swap will be used. Frequent swap switching will increase the CPU load.

Close nowKeepalive,ApacheThe system still responds to the access requests of 100 users per second, because we have separated the images, JS, CSS, and so on. Each access has only one request, at this time, the number of httpd processes is 100*1 = 100, and the memory is 100*5 m = 500 m.Apache100 TCP connections were also performed with the client. Performance has been improved too much.

Summary:

1. When your server has sufficient memory,Keepalive= On or off has little impact on system performance.

2. When there are many static webpages (HTML, images, CSS, and JS) on your server, we recommend that you openKeepalive.

3. When most of your servers are dynamic requests (because you connect to the database and have more access to the file system ),KeepaliveTurning it off will save a certain amount of memory. The saved memory can be used as the cache of the file system (one column in The vmstat command), reducing the I/O pressure.

PS: WhenKeepalive= On, keepalivetimeout settings are also a problem. Too short settings will causeApacheFrequent connection establishment puts pressure on the CPU. If the settings are too long, useless HTTP connections will be accumulated in the system, consuming a large amount of memory. You can adjust the specific settings continuously, because of your website browsing and ServerConfigurationHowever.

]

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.