Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirecting users if server is overcrowded or busy using php?

Tags:

php

.htaccess

is it possible to use php to redirect users to page i.e. busy.php when the server is busy or overcrowded, or something similiar? thanks :))

like image 927
getaway Avatar asked Dec 06 '25 12:12

getaway


1 Answers

I second using a dedicated load balancer instead of doing this with PHP. But if this is not an option for some reason, you can try with sys_getloadavg:

Returns three samples representing the average system load (the number of processes in the system run queue) over the last 1, 5 and 15 minutes, respectively.

Example from Manual:

<?php
$load = sys_getloadavg();
if ($load[0] > 80) {
    header('HTTP/1.1 503 Too busy, try again later');
    die('Server too busy. Please try again later.');
}
?>

The implementation in php-src uses getloadavg under the hood. So the values returned are the same you'd get from running cat /proc/loadavg or uptime.

A simple explanation of Linux load averages can be found at http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages:

On multi-processor system, the load is relative to the number of processor cores available. The "100% utilization" mark is 1.00 on a single-core system, 2.00, on a dual-core, 4.00 on a quad-core, etc.

Also see https://superuser.com/questions/23498/what-does-load-average-mean-on-unix-linux

like image 167
Gordon Avatar answered Dec 08 '25 01:12

Gordon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!