How To Split Randomly But Unevenly – PHP Code For Load UNBalancing

I read over a recent post on load splitting, that got me thinking about code I’ve written over the years – how to divide the work between many resources, keeping in account how much each can ‘handle’?

For example, if you have a server that can handle 3 times the load of another, it makes sense that it should get 3 times the calls – so if you send a visitor randomly to each server, it should get 3/4 of them, and the other 1/4.

In PHP, you could just do this:

if (mt_rand()>0.75) { pick one server } else { pick the other server }

But how to scale it?

The trick is to use an array, with each one representing the ‘capacity’ of the server. The total then is ALL the traffic.

In PHP, you could set up an array like this:


with each value representing a server and its capacity.

The array total is 80, so if we created a random number from 0-79, here’s how it would map out:

Pick 0-9: server ’0′, picked about 10/80 of the time
Pick 10-59: server ’1′, picked about 50/80 of the time
Pick 60-79: server ’2′, picked about 20/80 of the time

And here’s the code to do this:

while ($x-$server[++$i]>=0) $x-=$server[$i];
// or more cryptic but faster: 
// while ( ($x-=$server[++$i]) >= 0 ) {;}
// the server to use is in $i

The advantage of this is the dynamic splitting – by changing the values of $server we can affect the load of each one dynamically – for example, if we set the value $server[1] to zero we’ve effectively taken it offline. If another server gets some extra memory, we just bump it up. And so on.

So this is a simple solution – now how can we complicate it?

UNO R3 CH340G Starter Kit LED LCD Breadboard Shield Relay Sensor for Arduino DIY
End Date: Thursday Aug-28-2014 8:27:52 PDT
Buy It Now for only: $45.99
Buy It Now | Add to watch list
1Pcs New ACS712 5A range Current Sensor Module for Arduino New design b
$1.50 (3 Bids)
End Date: Wednesday Jul-30-2014 1:23:56 PDT
Bid now | Add to watch list
Ethernet Shield W5100 For Arduino Main Board 2009 ATMega 328 1280 MEGA2560 AK b
$7.00 (14 Bids)
End Date: Wednesday Jul-30-2014 0:57:06 PDT
Bid now | Add to watch list

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>