Proxypac - Loadbalancing with "if(Math.random() < 0.5)"

407 views Asked by At

We have 2 Proxy Servers and would like to loadbalance the traffic with the Proxypac. We are planing on implementing a loadbalancer to do this, but until then we want a better solution that manuall loadbalancing.

We would like to try this script, what do you guys think about this?

Thanks in advance

if(Math.random() < 0.5)
{
 return "PROXY 10.10.10.1:8080; " + "PROXY 10.10.10.2:8080";
}
else
{
 return "PROXY 10.10.10.2:8080; " + "PROXY 10.10.10.1:8080";
}
1

There are 1 answers

7
sstan On BEST ANSWER

If you don't mind having approximate even distribution over a large number of requests, then your code will work fine. But obviously, due to the randomness nature of your code, you could and will have moments where you keep hitting the same server over and over before you switch over. You never know, it's random.

If you are more interested in ensuring 50/50 distribution for every request, then you should probably do it something like this instead (I am assuming your code runs in a single thread. You would have to adjust for multi threading):

private static int requestCount = 0;

....

int localCount = requestCount;
requestCount++;
if (localCount % 2 == 0) {
{
    return "PROXY 10.10.10.1:8080; " + "PROXY 10.10.10.2:8080";
}
else
{
    return "PROXY 10.10.10.2:8080; " + "PROXY 10.10.10.1:8080";
}