How to Tune MaxRequestWorkers in Apache?
Tuning the MaxRequestWorkers directive (formerly known
as MaxClients) is critical for optimizing Apache HTTP
Server performance, preventing server crashes, and managing memory usage
under high traffic. This guide covers how to calculate the optimal value
for your server based on available RAM, configure the directive within
your Multi-Processing Module (MPM), and apply the changes safely.
Understanding MaxRequestWorkers
The MaxRequestWorkers directive sets the limit on the
number of simultaneous client requests that Apache will serve. Any
connection attempts beyond this limit are queued up to the number
defined by the ListenBacklog directive.
- If the value is too low: Your server will underutilize its resources, and users may experience slow loading times or connection timeouts because they are stuck in the queue.
- If the value is too high: Apache will attempt to handle more concurrent requests than the server’s physical memory (RAM) can support, leading to heavy use of swap space, severe performance degradation, or an Out-Of-Memory (OOM) crash.
Step 1: Calculate Your Server’s Capacity
To find the right value, you need to know your total available RAM and the average memory size of a single Apache process.
- Find the average RAM used by an Apache process: You can run the following command in your terminal to see the average resident set size (RSS) of your Apache processes in megabytes:
ps -ylC apache2 --sort:rss | awk '{sum+=$8; ++n} END {print "Avg: "sum/n/1024" MB"}'(Note: Replace apache2 with httpd
depending on your Linux distribution, such as CentOS/RHEL). 2.
Determine available RAM for Apache: Deduct the memory
required for your operating system and other services (like MySQL,
PHP-FPM, or Redis) from your total RAM. If your server is a dedicated
web server, you can safely allocate roughly 80% of the total RAM to
Apache. 3. Use the Tuning Formula: Divide the available
RAM by the average Apache process size: \[MaxRequestWorkers = \frac{\text{Available RAM for
Apache}}{\text{Average Apache Process Size}}\]
For example, if you have 8 GB of total RAM, you might reserve 2 GB for the OS and other services, leaving 6 GB (6144 MB) for Apache. If your average Apache process size is 50 MB: \[6144 \text{ MB} / 50 \text{ MB} = 122.88\]
In this scenario, you would round down and set your
MaxRequestWorkers to 120.
Step 2: Configure the Directive in Apache
Apache handles concurrency via Multi-Processing Modules (MPMs), usually event, worker, or prefork. You must place the directive inside the configuration block for the specific MPM your server is running.
- Open your Apache configuration file (commonly found at
/etc/apache2/mods-enabled/mpm_event.conf,/etc/apache2/apache2.conf, or/etc/httpd/conf.modules.d/00-mpm.conf). - Locate or add the directive inside your active MPM block.
For the modern Event or Worker
MPMs, you must also ensure that ServerLimit is configured
properly, as MaxRequestWorkers cannot exceed
ServerLimit divided by ThreadsPerChild.
<IfModule mpm_event_module>
ServerLimit 16
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 5000
</IfModule>For the legacy Prefork MPM (where one process handles one connection), the configuration is simpler:
<IfModule mpm_prefork_module>
ServerLimit 120
MaxRequestWorkers 120
MaxConnectionsPerChild 3000
</IfModule>Step 3: Test and Restart Apache
Before applying the new limits, always test your Apache configuration syntax to ensure there are no typos or structural errors.
apachectl configtestor
httpd -tIf the output returns Syntax OK, restart the Apache
service to apply the changes:
sudo systemctl restart apache2or
sudo systemctl restart httpd