Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect Users Based on Server Traffic with PHP

Tags:

php

curl

I am making a website and I want to spread my traffic out to avoid crashing and to make sure I can offer 100% uptime. I will do this by having 5 different servers and one main server. Allow me to explain.

I have one website (domain.com) and on the homepage of that site there is a cURL frame that connects users to one of the other five servers which has the least traffic on it. I want the script to change the cURL frame to display the server with the least traffic.

Can anyone help me or suggest how I could code this as I am a PHP beginner.

Thanks in advance, Callum

like image 950
Callum Whyte Avatar asked Nov 13 '10 00:11

Callum Whyte


2 Answers

Here we go:

Get the 5 slave servers to update a table in a database on the main server inserting their load every X seconds/minutes. Then in the main server, check which of them has the lowest load and redirect the user to that particular one. alt text

How to get the server load?

There's a function called sys_getloadavg(); which will return 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 in an array.

That way, if the system load is over a set number, you should redirect the visitor to a different slave server. For example:

$load = sys_getloadavg();
if ($load[0] > 80) {
    //insert into database "I'm busy!!"
    $query = "UPDATE `server_load` SET `load` = $load WHERE `server_id` = 1";
    mysql_query($query);
}

Note the 0 on $load means that it's getting the server load for the last minute, use 1 or 2 for the 5 and 15 minute average system load. The query, would be on a script on the slave servers and every time it was run, it would update the average server load on the table server_load.

How to choose from the main server

Once you've isolated the 'get server load' functionality from the slave servers. All you need to do from the main server is query the database and get the lowest load from the server_load table. The mentioned table, would be functional enough with a timestamp field, an id field, and a load field with the following structure:

`timestamp` int(11) not_null
`id` int(1) not_null autoincrement
`load` int(3) not_null

For a basic tutorial introducing mysql and php interaction I suggest this link from phpsense. If you dedicate enough time an read through the documentation I've linked you with, you should be able to achieve your goal. Make sure to ask all your following questions as independent entities if you are unable to find them on this site already. Chances are they've been asked over and over again by people preceding you. Hope I've been of help.

Sources:

  • php docs
  • phpsense
like image 70
Juan Cortés Avatar answered Nov 01 '22 08:11

Juan Cortés


You're going to need something to determine the traffic on each server; this should output a metric that you can compare against. Each of the five servers should be computing their traffic load dynamically, and updating that somewhere (I'd suggest a database). So your front-end server can, when a user comes in, query the database for the server with the least load, and direct them there.

like image 1
Paul Sonier Avatar answered Nov 01 '22 09:11

Paul Sonier