Using sys_getloadavg and num_cpus in PHP to Throttle API


Given that we know how to retrieve the number of CPU cores using PHP Function, we can then check the current load average, if it is larger than a threshold multipled by number of cores, we can response with 503 Server Too Busy.

The sys_getloadavg returns three integers representing the average system load (the number of running processes) in the last 1, 5, and 15 minutes. It will return FALSE on failure.

1
2
3
4
5
6
7
8
9
10
11
<?php
require('num_cpus.php');
 
$currentload = sys_getloadavg();
$numofcpus = num_cpus();
define("THRESHOLD", 0.8);
 
if ($currentload[0] > $numofcpus * THRESHOLD) {
    header('HTTP/1.1 503 Too busy, try again later');
    die('Server too busy. Please try again later.');
}
<?php
require('num_cpus.php');

$currentload = sys_getloadavg();
$numofcpus = num_cpus();
define("THRESHOLD", 0.8);

if ($currentload[0] > $numofcpus * THRESHOLD) {
    header('HTTP/1.1 503 Too busy, try again later');
    die('Server too busy. Please try again later.');
}

On a single core server, the healthy load is 0.8 or less, on a dual core server, the healthy load is 0.8*2 or less… In this case, we throttle the API and let the users retry later.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
206 words
Last Post: Teaching Kids Programming - Depth First Search Algorithm to Convert to Elephant Binary Tree
Next Post: Teaching Kids Programming - Noisy Palindrome Algorithms

The Permanent URL is: Using sys_getloadavg and num_cpus in PHP to Throttle API

Leave a Reply