Optimizing Apache in High-Traffic Servers

There are a few common things that can cause server load problems. One of the common problem is: too many processes (or runaway processes) using too much RAM

Solutions: Improve Apache’s RAM Usage

1. Reduce wasted processes by tweaking KeepAlive

KeepAliveTimeout is the amount of time a process sits around doing nothing but taking up space. Those seconds add up in a HUGE way. But using KeepAlive can increase speed for for you – disable KeepAlive and the serving of static files like images can be a lot slower. KeepAlive enable HTTP persistent connections to improve latency times and reduce server load significantly. I think it’s best to have KeepAlive on, and KeepAliveTimeout very low (like 1-2 seconds).

2. Limit total processes with MaxClients(for prefork MPM)

MaxClients sets a limit on the number of simultaneous connections/requests that will be served. I consider this directive to be the critical factor to a well functioning server. Set this number too low and resources will go to waste. Set this number too high and an influx of connections will bring the server to a stand still. Set this number just right and your server will fully utilize the available resources. An approximation of this number should be derived by dividing the amount of system memory (physical RAM) available by the maximum size of an apache/httpd process; with a generous amount spared for all other processes. MaxClients ? (RAM – size_all_other_processes)/(size_apache_process) Use ‘ps -ylC httpd –sort:rss’ to find process size. Divide number by 1024 to get megabytes. Also try ‘top’. Use ‘free -m’ for a general overview. The key figure to look at is the buffers/cache used value. Use ‘vmstat 2 5’ to display the number of runnable, blocked, and waiting processes; and swap in and swap out.

When calculating MaxClients, take into consideration that the reported size of a process and the effective size are two different values. In this setup, it might be safe to use 20 or more workers… Play with different values and check your system stats.

3. Force processes to reset with MaxRequestsPerChild

Directive MaxRequestsPerChild is used to recycle processes. When this directive is set to 0, an unlimited amount of requests are allowed per process. Setting this value to the amount of requests that a website generates per day, divided by the number of processes, will have the benefit of keeping memory leaks and process bloat to a minimum [both of which are a common problem]. The goal here is to recycle each process once per day, as apache threads gradually increase their memory allocation as they run.

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: