Apache Performance Issue, server reached MaxClients

Solution Verified - Updated

Environment

  • Red Hat Enterprise Linux (RHEL)
  • httpd (Apache)
  • Red Hat JBoss Enterprise Web Server (EWS)

Issue

  • Apache is presenting a system Performance Issue where All the connections are consumed.
  • Connection pile up at webserver
  • The Apache httpd server is running out of MaxClients very quickly and following message can be seen in the Apache Logs.
  [error] server reached MaxClients setting, consider raising the MaxClients setting
  [error] server is within MinSpareThreads of MaxClients, consider raising the MaxClients setting
  • Server responding very slow and getting the above defined error.

Resolution

Increase the MaxClients to the desired value and also specify the ServerLimit to the same value as MaxClients (assuming prefork).

Keep in mind that MaxClients is optimal at 200 * (# of cpu cores) for the prefork mpm and around 300 * (# of cpu cores) for the worker mpm.

Example:

<IfModule prefork.c>
   StartServers       5
   MinSpareServers    5
   MaxSpareServers   10
   ServerLimit      200  
   MaxClients       200   
   MaxRequestsPerChild  400
</IfModule>

For a reference, see Content from httpd.apache.org is not included.Content from httpd.apache.org is not included.http://httpd.apache.org/docs/2.0/mod/mpm_common.html#serverlimit

Root Cause

  • The ServerLimit defaults to 256, so if the MaxClients is increased beyond that number, Apache will throw a warning message and truncate the MaxClients down to 250.

  • To properly calculate an optimal configuration you can use the following:

#!/bin/bash

MEM_LIMIT=$(free | grep -i mem | awk '{print $2}')
MAX_CLIENTS=$(if [ $(httpd -V | grep "Server MPM:" | awk '{ print $3 }') == 'Prefork' ]; then echo "$(grep -c proc /proc/cpuinfo) * 200" | bc; else echo "$(grep -c proc /proc/cpuinfo) * 300" | bc; fi)
THREAD_STACK_SIZE=$(ulimit -s)

echo "System Memory Limit: $MEM_LIMIT"
echo "System Thread Stack Size: $THREAD_STACK_SIZE"
echo "Max Clients Setting: $MAX_CLIENTS"
echo "Max Memory Needed for Optimal configuration: $(echo "$MAX_CLIENTS * $THREAD_STACK_SIZE" | bc) bytes"

if [ $MEM_LIMIT -gt $(echo "$MAX_CLIENTS * $THREAD_STACK_SIZE" | bc) ]; then
    echo "You have Enough RAM to reach the optimal configuration."
else
    echo "You can not reach the optimal configuration because of the amout of RAM your system has."
fi

Diagnostic Steps

You can reproduce with "ab" command:

$ ab -c <Number of client> -n <Number of request> http://hostname/path/to/file
Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.