Most of us just write applications to be deployed on a webserver without thinking about what to do when the server becomes overloaded. What happens in most cases is that the application would go trying to run itself on every request, and on a heavily loaded system, it just goes on the aggravate the problem, making increasing the load on the server, till finally the server becomes unreachable.
What if you could actually check the server load in your PHP application? Would you think about checking the server load before doing some heavy computational task or database accesses? There’s function in PHP which will allow you to check the load averages on a server.
The sys_getloadavg() in PHP gives you the load averages for your server. You can use this to check the load on your server before processing a request.
$serverload = sys_getloadavg(); print_r($serverload);
The code above gives the output:
Array
(
[0] => 1.07
[1] => 0.89
[2] => 1
)
The output from the code shows the system load averages where [0] is the load averages for the past 1 minute, [1] is for the past 5 minutes and [2] is for the past 15 minutes.
On an ideal server, the load averages on the server shouldn’t go above 3. A load average of more than 15 would mean that the server is already running much lower than normal, and you may not want to add more load to this.
You can use this output to decide whether to serve a process intensive page depending on the server load. Here’s a pseudo-code on how you’d do something like this:
$serverload = sys_getloadavg();
if ($serverload[0]<10)
{
// process loads of data now
some_big_process();
} else
{ // Send a 503 header stating that the server is overloaded.
header('HTTP/1.1 503 Too busy, try again later');
die('The server is busy at present and cannot process your request.');
}
You can also run the application in such a way that you can wait till the load on the server decreases to run your process. This is useful if you are running a cron for processing data:
set_time_limit(0);
//set time limit to 0, so PHP's max execution time doesn't interfere with the processing script
$serverload = sys_getloadavg();
//Check load and see if it's low enough to start processing
while ($serverload[0]>5)
{ //Wait for 1 minute to check load again
sleep(60);
}
// out of the loop - so let's do some processing now!
some_big_process();
To ensure that the script doesn’t run endlessly on a permanently overloaded server, time check in the while loop to auto-end the script it the server load remains high for a certain amount of time.
More reading:
Load & Load Averages on Wikipedia
PHP function – sys_getloadavg

Ansar Ahmed on May 18, 2010
The php.net link is broken for “sys_getloadavg() ” there is additional dot(.) in the beginning.
vinuthomas on May 18, 2010
Thanks Ansar. Fixed the link
P Patil on May 18, 2010
Good start on much-needed feature. I hope other “load” parameters such as I/O will get included too in future.
Sudheer on May 19, 2010
Quite useful.
vinuthomas on May 19, 2010
If you're running on a Linux based server, i/o also figures into the load calculations.
mixdev on October 18, 2010
Vinu,
Excellent info. Instead of taking a more expensive server, this can save a penny or two sometimes.
What cut-off values should we use? I think if it is a XEON quad, the optimum safe value must be 4 I guess? Also when it is cloud/VPS, CPU will be shared and this value will further reduce. Right?
vinu on October 18, 2010
Yeah – roughly add 1 for every processor/core is a safe bet for optimum settings. So for a quad core, processor 4 should be fine.
You can also add 0.5-1 more per processor if you find your server is still responsive on those loads. Trial and error to squeeze every bit of juice your server can provide
mixdev on October 22, 2010
For our MT account (XEON Quad, 4GB RAM), I used cutoff 3 and it works perfect. The app is a cpu eater but now performs quite decent for all requests served.