Web Hosting Talk







View Full Version : Very simple load balancing system in php


yegorpb
01-16-2007, 01:09 AM
Id like to rewrite the load-balancing system I currently have on my site. Keep in mind, this is just to load-balance the 6 media servers that I have, to split the video views among them equally. Each video server is an exact mirror, and I currently just make my site pick a random url via the rand() function, so the connections are split 6 ways, which works great. The problem is, that the load is split evenly, and Id like to lessen the load on some servers, and increase it on others, so I wanted to have some kind of "weight" system, where I can assign a priority to each URL, so some URLs have a higher probability to be displayed than others.

The way I was thinking to do that is just to add a drop down menu with weight values 1-9 next to each media server URL. If each server has a 1 next to it, its split evenly. If you increase the weight value on one server, its priority increases, and it appears more often.

The problem is, I have no idea how to even approach this. Can anyone give me some tips on how I can accomplish this?

Thanks

Steven
01-16-2007, 02:07 AM
why not just do it with LVS load balancing software and create a true load balancer.

karlkatzke
01-16-2007, 02:32 AM
yep, you'd be best suited with something like LVS or a hardware load balancer. Added bonus is that they can detect if one server is slower than another and adjust without you having to do it manually.

yegorpb
01-16-2007, 04:47 AM
Because its highly unnecessary. The only thing the servers serve is streaming media (.wmv), so the only thing I need to balance is the number of connections per server.

Steven
01-16-2007, 12:27 PM
Think about it. Doing this in php will add added processing requirements thus maybe bringing up server load. LVS can balance in several ways, round robin, least connections, etc. And it uses virtually NO cpu power.

Korvan
01-16-2007, 01:19 PM
Steven, while a hardware load balancer might be the ideal solution - it is not the cost effective solution the OP is looking for. It is obvious he wants to avoid purchasing a hardware load balancer.

For a simple php solution just using the rand function (which by itself is very little load) you can assign weights by giving servers you want to have more traffic more numbers to chose from. For instance server1 could be 0-10 while server 2 is only 11-16.

To give you some idea this is an overly simple way to do it. It really doesnt take much cpu to do the following:

<?php
$max = 0;
$serverchoice = 0;

$server = array();
$servermin = array();
$servermax = array();
//example values: You would probably have your values seeded by your dropdowns.
$server[0] = 10;
$server[1] = 12;
$server[2] = 14;
$server[3] = 2;
$server[4] = 6;
$server[5] = 12;

//create the ranges for the rand functoion:
for ($i = 0; $server[$i] > 0; $i++)
{

$servermin[$i] = $max;
$max += $server[$i];
$servermax[$i] = $max-1;
}
//obtain our random number
$rand = mt_rand(0, $max-1);
//pick the server
for ($i = 0; $server[$i] > 0; $i++)
{
//if rand is greater than the min and less than the max we have our server
if($rand >= $servermin[$i] && $rand <= $servermax[$i] )
{
$serverchoice = $i;
//no need to proccess anymore
break;
}
}
//Unset our vars
unset($rand);
unset($max);
unset($server);
unset($servermin);
unset($servermax);
//I have an echo here obviously you would change it so its useful to your script
echo($serverchoice);
?>


As I said this is overly simple, you will probably want to put it inside a class or a function and you could if you want put it inside a foreach loop (instead of for) as well.

Obviously for what you want to do you would have your dropdowns assign the value that is put in the server array to determine its odds of getting chosen. The larger the number the more often it will be picked. If the number is double another servers number it has twice the chance of getting picked.

yegorpb
01-16-2007, 01:55 PM
Thanks Korvan, this is exactly what I needed! Im not to good with arrays, so this will help me greatly!

Steven, you might be right about the LVS or hardware solution, but in this case it will be a total overkill, and I dont want to spend the extra money implementing this, when I can do it just as easily in php.

Steven
01-16-2007, 04:41 PM
to: Korvan

LVS is free

to: Yegorpb

takes about 1 hour to implement in direct route mode.


what ever makes you happy.

Steven
01-16-2007, 04:47 PM
The problem I see is, Korvans setup is not intelligent in the way that, some servers will be more loaded then others because it does not take into account servers that have downloads in process. LVS on the other hand can work in least connections so that the server with the least number of connections get the serve.

yegorpb
01-17-2007, 01:31 AM
To be honest, I dont know how LVS works, or how to implement/control it. Not all of my servers are in the same DC, and they dont run the same OS, so Im not sure whether thats a problem.

Korvan
01-18-2007, 12:05 PM
The problem I see is, Korvans setup is not intelligent in the way that, some servers will be more loaded then others because it does not take into account servers that have downloads in process. LVS on the other hand can work in least connections so that the server with the least number of connections get the serve.
You could make it detect and modify the values based on server load. I have no idea on exactly how everything is setup on his end so there is no way I can possibly just give him a script to do that for him. All I can provide is a way to do a weighted random system like he asked. Its a bit more work but its doable.

As far as LVS, you mean Linux Virtual Servers: http://www.linuxvirtualserver.org/

Assuming he is using Linux as his hosting enviroment (and he probably is) LVS might be more of an ideal solution.

karlkatzke
01-18-2007, 01:50 PM
The problem I see is, Korvans setup is not intelligent in the way that, some servers will be more loaded then others because it does not take into account servers that have downloads in process. LVS on the other hand can work in least connections so that the server with the least number of connections get the serve.

He could also set up a reporting service on each of his servers (a php page that does nothing but display the number of current downloads, active apache threads, etc ... could easily be done with exec() or snmp) and then the central server collects that data and adjusts load based on it. His logic could also include the geographical location of the IP address that's requesting the image ... That would do just about the same thing, and add some other features not present on most load balancers.

The most complex part of a "Load Balancer" appliance is the layer 7 routing that makes it look like there's just one monster of a server behind everything. He doesn't need this feature, since all of his image servers have different subdomain names and he just swaps them in the PHP code.