Rational use of keepalive configuration in Apache

Source: Internet
Author: User
Tags server memory

In the Apache server, keepalive is a Boolean value. On indicates opening, and off indicates disabling. This command exists in many other HTTPd servers.

The keepalive configuration command determines whether to immediately close the TCP connection after processing the HTTP request initiated by the user. If keepalive is set to on, the user will not immediately disconnect after one access, if there is still a request, it will continue to be completed in this TCP connection, instead of repeatedly establishing a new TCP connection and closing the TCP connection, which can improve the user access speed.

We will consider three situations:
1. When a user browses a webpage, in addition to the webpage itself, it also references multiple JavaScript files, multiple CSS files, and multiple image files, all of which are on the same HTTP server.
2. When a user browses a webpage, in addition to the webpage itself, a Javascript file and an image file are also referenced.
3. A user browses a dynamic web page, which is instantly generated by the program and does not reference other content.

In the above 3 cases, I think: 1 is the best choice to enable keepalive, 2 is random, and 3 is the best choice to disable keepalive.

Let me analyze the cause.

In Apache, what are the differences between enabling and disabling the keepalive function on the server?

Let's take a look at the theoretical analysis.

After keepalive is enabled, it means that every time a user completes all access, the TCP connection will be closed after a certain period of time. Before closing the connection, there must be an Apache process corresponding to this user and cannot process other users. Assume that the keepalive timeout time is 10 seconds, and the server processes Access from 50 independent users per second, in the system, the total number of Apache processes is 10*50 = 500. If a process occupies 4 MB of memory, a total of 2 GB of memory is consumed. Therefore, in this configuration, memory consumption, but the advantage is that the system only processes 50 TCP handshakes and closes the operation.

If keepalive is disabled and 50 users are allowed to access each second, if the number of consecutive requests is 3, the total number of Apache processes is 50*3 = 150, if each process still occupies 4 MB of memory, the total memory consumption is 600 mb. This configuration can save a lot of memory. However, the system processes 150 TCP handshakes and closes, therefore, it will consume more CPU resources.

Let's take a look at the practice.

In a group of servers that process a large number of dynamic web pages, I started the keepalive function and often observed that there were a large number of Apache processes when the user access volume was large, and the system frequently used swap memory, the system is unstable and sometimes the load fluctuates greatly. After the keepalive function is disabled, it is obvious that the number of Apache processes is reduced, the idle memory is increased, the memory used for file system cache is also increased, and the CPU overhead is increased, however, the service is more stable, and the system load is also relatively stable. There is little fluctuation in the load range, and the load is reduced to a certain extent. What is not obvious is: when the traffic volume is small, the average system load has not changed significantly.

Summary:
On servers with ample memory, no matter whether or not the keepalive function is disabled, the server performance will not change significantly;
If the server memory is small, or the server has a large number of file systems to access, or it mainly handles dynamic web services, it can save a lot of memory after keepalive is disabled, the saved memory is used for file system cache, which can improve the file system access performance and make the system more stable.

  Supplement 1:
I think the keepalive option should be disabled based on the following formula.

Under ideal network connection conditions, the number of Apache processes and memory usage in the system can be expressed using the following formula:
Httpdprocessnumber = keepalivetimeout * totalrequestpersecond/average (keepaliverequests)
Httpdusedmemory = httpdprocessnumber * memoryperhttpdprocess

Change to Chinese:
Total Apache processes = keepalivetimeout * Number of HTTP requests per second/average keepalive requests
Apache memory usage = total Apache processes * Average memory usage per Process

Note:
[Average keepalive requests] indicates the number of HTTP requests that are continuously sent after each user connects to the server. When keepalivetimeout and other 0 or keepalive are disabled, keepalivetimeout is not involved in multiplication. From the preceding formula, if there are more [user requests per second], the value of [keepalivetimeout] is large, if the value of the [average keepalive request] is small, there will be more [Apache processes] and [Memory]. However, when the value of the [average keepalive request] is larger, [Apache process count] and [Memory] tend to decrease.

Based on the above formula, we can calculate that when the average keepalive request is <= keepalivetimeout, it is cost-effective to disable the keepalive option. Otherwise, we can consider opening it.

Supplement 2:Keepalive this parameter controls whether Apache allows multiple requests in a connection. It is enabled by default. However, for most Forum sites, it is usually set to off to disable this support.

Supplement 3:If the application squid service or other layer-7 devices are running in front of the server, keepalive on is set to enable persistent connections.

Keepalive is critical when the front-end has squid. Remember on

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.