views:

336

answers:

3

Hello all,

I wish to make use of my unlimited shared hosting to create several small shared hosting accounts that I can offload processing to. So I need to create some logic in PHP to create the basics of a Load Balancer.

I have 4 shared accounts, one is the main site and the other 3 is the processing server accounts. Please see image.

I want to know how I can determine which server to pass the next processing job to? Do I keep a variable of the last server I passed to and go from one to the other? etc.

What is the best method?

Thanks all

A: 

Might be going off the hook, but, could you reproduct round-robin / dns / using a method of random requests?

Shamil
+3  A: 

My KISS Tinpot suggestion would be to use JavaScript upon submitting the form to select a server at random.

A list available servers that the upload page would use should be served up as a JS file from the main server. A cron job should update this list periodically to ensure only alive servers are up for selection.

Simple example:

servers.js (hosted on main server, auto-generated):

/* Auto Generated 01/01/2009 01:01:01 */
var Servers = new Array();
    Servers[0] = "42.81.240.101";
    Servers[1] = "42.81.240.120";
    Servers[2] = "42.81.240.199";

function LoadBalancer_GetIP()
{
    return Servers[Math.floor(Math.random() * Servers.length)];
}

upload.html:

<html>
    <head>
        <script type="text/javascript" src="servers.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            function DoUpload() {
                TestForm.action = "http://"+LoadBalancer_GetIP()+"/upload.php";
                TestForm.submit();
            }
        </script>
        <form id="TestForm" method="POST" action="">
            <input type=button value="Upload" onclick="DoUpload()">
        </form>
    </body>
</html>
gridzbi
A: 

Please reconsider, what are those jobs you're trying to break up? What kind of jobs.

First for the answer, you could split jobs evenly based on some parameter of the job, like let's say, each parameter comes with a username field, that submitted the job. You split your userbase between those servers and send appropriate request to designated server.

Other strategy - more complex one - would be for the load balancer to recive some basic info from those 3 workers, which would tell the load of each of them, than distribute jobs basing on that load.

I personaly would go for the complex method, of course if given subject would require that.

Marcin Cylke