2bits has a great article explaining the theory behind tuning your Apache server's MaxClients directive. It explains how to optimize this setting for your server's available memory based on this simple equation:
The key here is to avoid thrashing (memory swapping to disk, which is extremely slow and sometimes unavailable), while making good use of the memory you have. This becomes particularly important for those hosting using VPS products where memory is precious and expensive.
The following test will quickly provide a real-world reference point for tuning the MaxClients directive:
Start with a reasonable
MaxClientsvalue based on the math used in the 2bits article:
sudo vim /etc/httpd/conf/httpd.conf
sudo /etc/init.d/httpd restart
Start your benchmark tool (ab, JMeter, etc). Concurrency should be equal or higher than the value in step 1.
ab -n 1000 -c 50 http://example.com/
While your benchmark tool is running, observe your server's memory consumption:
watch -n 1 free -m
- Verify available memory use is stable and does not go below 15% of total memory.
- Rinse and repeat. Find the sweet spot.
- Always back up your conf files before you edit them.
- You may notice slower page load times from your benchmark tool while its concurrency is greater than your MaxClients setting.
- If your server is a VPS product running without Swap support (on MediaTemple, for example), running out of memory can prove fatal to your VPS. Be careful when running this test against a live production web server.