How to Respond with 503 Service Busy to Requests when Server Load Average is High?


I got the email notification when the server load is high:

Your Server (helloacm) Load Alert Needs Attention! 11.54 110.63 102.98 3/391 5989

and I checked the apache access log and it turns out there are many requests to one of the API. But I don’t want too many requests at the same time overloading my server and the requests to other pages (blog, tools etc) cannot be served properly.

How to Check the Load Average in PHP?

The PHP function sys_getloadavg() returns the system load average for the last 1, 5 and 15 minutes (similar to the output of uptime).

1
2
$currentload = sys_getloadavg();
$load = currentload[0]; // system load of over last minute
$currentload = sys_getloadavg();
$load = currentload[0]; // system load of over last minute

How to Get the Number of Cores in PHP?

The Load Average is closely related to the number of cores in the current system. For example, if there are two cores, when system is fully loaded, it should be around 2.0 and if there is only 1 core, 0.5 load average means the system is 50% loaded.

Under Linux OS, we can obtain the content of the file “/proc/cpuinfo” and do a regex match to get the number of cores:

1
2
3
4
5
function num_of_cores() {
    $cpuinfo = file_get_contents('/proc/cpuinfo');
    preg_match_all('/^processor/m', $cpuinfo, $matches);
    return count($matches[0]);
}
function num_of_cores() {
    $cpuinfo = file_get_contents('/proc/cpuinfo');
    preg_match_all('/^processor/m', $cpuinfo, $matches);
    return count($matches[0]);
}

How to Send 503 to Browser when System is Overloaded?

So combining the above two inputs, we can also set a threshold:

1
2
$factor = 1.5; // each core maximum 1.5 times overloaded.
$threshold = num_of_cores() * $factor;
$factor = 1.5; // each core maximum 1.5 times overloaded.
$threshold = num_of_cores() * $factor;

And, if the current system load over last minute is considered high, then throw 503 error:

1
2
3
4
5
6
7
// https://helloacm.com/how-to-respond-with-503-service-busy-to-requests-when-server-load-average-is-high/
if ($load > $threshold) {
  header($_SERVER['SERVER_PROTOCOL'] .' 503 Service Temporarily Unavailable');
  header('Status: 503 Service Temporarily Unavailable');
  header('Retry-After: 300');
  exit("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n<html><head>\r\n<title>503 Service Temporarily Unavailable</title>\r\n</head><body>\r\n<h1>503 Service Temporarily Unavailable</h1>\r\n<p>The requested URL " . $_SERVER['SCRIPT_NAME'] . " is Temporarily Unavailable.</p>\r\n</body></html>");
}
// https://helloacm.com/how-to-respond-with-503-service-busy-to-requests-when-server-load-average-is-high/
if ($load > $threshold) {
  header($_SERVER['SERVER_PROTOCOL'] .' 503 Service Temporarily Unavailable');
  header('Status: 503 Service Temporarily Unavailable');
  header('Retry-After: 300');
  exit("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n<html><head>\r\n<title>503 Service Temporarily Unavailable</title>\r\n</head><body>\r\n<h1>503 Service Temporarily Unavailable</h1>\r\n<p>The requested URL " . $_SERVER['SCRIPT_NAME'] . " is Temporarily Unavailable.</p>\r\n</body></html>");
}
503 How to Respond with 503 Service Busy to Requests when Server Load Average is High? linux php programming languages regex

503 server busy

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
504 words
Last Post: How to Search and Replace using Regular Expression with Arithmetic Evaluation?
Next Post: Why and How to Turn Off Ping (ICMP) for Servers?

The Permanent URL is: How to Respond with 503 Service Busy to Requests when Server Load Average is High?

Leave a Reply