Apache MaxClients Issue

apache-2.2httpd.conf

In my error logs I see:

server reached MaxClients setting, consider raising the MaxClients setting

My MaxClients setting is 150. However when I calculate what seems to be an optimal MaxClients setting I come up with:

Mem used by 1 Apache process = 16
Mem available to Apache = 197

MaxClients = 12 (197/16)

So should I knock MaxClients way down to 12 because my calculations show that is all the RAM I have available or should I raise the limit as the error log suggests (or are my calculations worthless because I'm misunderstanding something)?

Best Answer

Consider setting KeepAlive Off in your httpd.conf if you are running a prefork Apache. Rather than each child holding an inactive connection open Apache will close each connection after fulfilling it. This will reduce the overall connections and child processes needed to serve. It does add a bit of extra time because each new request does have to renegotiate with your server rather than reusing an open connection.

(edit) Oh yeah the question. I'd leave MaxClients alone or set it based on the RAM you have. Assuming you have more than enough RAM, rule of thumb is 2-4x the number of CPU cores you have assuming your backend isn't very slow, else use RAM as the limiting factor. However I think turning off KeepAlive is going to drop the number of children you need to something fairly normal and you'll stop hitting the limit.

(edit2) Now here's where it gets tricky. If your Apache server is an application server which primarily runs PHP, Ruby, etc code and process data it gets from various backends or disk into rendered pages the 2-4x rule works. Generally you're going to be burning 250ms of CPU per page view if it's heavy uncached page so or 4 page views per core. If you have a standalone server you'll be able to serve more requests because lots of them will be small images, css, js, etc which would normally be served off another set of servers for an application server. I assume you have one server so leaving the whole thing alone is probably in your best bet.

At my last job we ran 8core/8GB frontend machines with 50 MaxClients and KeepAlive Off which worked pretty well. In some cases setting MaxClients to 100 got slightly better benchmarks, but overall latency of page generation went up because most of the time we were blocking on various backends or starting to starve for CPU.