Forum Moderators: phranque

Message Too Old, No Replies

Simple load balancing idea.

Would this work.

         

mack

12:20 am on Apr 7, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I had an idea for a pretty "rough and ready" solution for load balancing and to also to introduce an element of redundancy protection.

Lets say you have 2 servers all hosting the same website. This allows you to spread the load across 2 servers. What I was thinking was, to use a simple php script to detect a server going down and cut it out of the equasion.

Lets use a simple random script to decide who goes where. This will ensure that each server gets a prety even load. The random selection scrit will read from a text file..

server1
server2

this gives it 2 possible options.

Now we need to build the text file so we run a cron every so often, It runs a script, lets call it server.php that makes a request to each of the servers...

<?php
#server.php
include "ip.to.server.1./status.php" ;
include "ip.to.server.2./status.php" ;
if ($server1 == 'yes') {
} elseif ($server1 == 'no') {
if ($server2 == 'yes') {
} elseif ($server2 == 'no') {
?>

We could then use fopen to write the text file each time the script is run.

the file Status.php would simply be a php document placed in the root folder of each server
example...

<?php
#status.php
$server1="yes" ;
?>

The idea is to draw an include from all servers and this include will ensure that the server is placed within the random selection, and that any dead severs are removed.

Has anyone ever done anything like this and if so did it work, or do you have any better solutions.

Mack.

Thanks in advance.

jamesa

9:10 am on Apr 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check out the Load Balancing section of this page [httpd.apache.org] (apache.org)

ara818

9:25 am on Apr 7, 2004 (gmt 0)

10+ Year Member



Sounds like there may be some concurrency issues that would cause some strange errors or performance blips. Meaning if the file containing the servers currently online is accessed by two processes at the same time it will either fail or block and strange things could happen.

Anyway, your solution is simple, but in terms of the amount of "work" (as in network and file I/O) your servers have to do to be load balanced, it is actually not simple at all. Implementing load balancing at the PHP level is just not a good idea.

As for alternatives, free and fairly easy to setup alternatives exist such as LVS [linuxvirtualserver.org...]

[edited by: DaveAtIFG at 2:57 pm (utc) on April 7, 2004]