Load balancer based on percentage distribution algorithm

Viewed 1336

Let's assume I have the following entities:

public class Account {
    private String id;
    private List<Host> hosts;
}

public class Host {
    private String name;
    private int percentageLoad;
}

I have a web application with only one endpoint /check with an accountId as a request parameter.

An example of a request could be: localhost:8080/check?accountId=123456.

That service has to validate that accountId exists in an embedded database, and if so, it answers with a host's name. Those hosts have a load percentage and the answers should be balanced depending on their load percentage.

It may be confusing what I want to explain, so I will put an example:

Assume that I have in the database the following Account:

Account:
  - id: 123
  - hosts: [{machine1, 60}, {machine2, 40}]

As you can see that the account has 123 as id and two hosts with one percentage load for each one.

If I get the following request: localhost:8080/check?accountId=123, then as that account id exists in the database, it should balance the host's name answer depending on the percentage load.

More clear, let's say I make 10 similar requests: localhost:8080/check?accountId=123, then the hostname machine1 should be returned 6 times (as it has a load percentage of 60%), and machine2 should be returned 4 times (as it has a load percentage of 40%).

I really don't know which strategy to apply in order to balance it.

I thought about a counter which counts the number of answers for each machine. Or pick a random number between 0 and 10 and decide one machine or another depending on that number. But I think they are not good approaches, any idea about an algorithm to balance that?

1 Answers

You'll need another variable in the Host class:

public class Host {
    private String name;
    private int percentageLoad;
    private int percentageAccum;
}

The initial value for percentageAccum is the value of percentageLoad.

When a request is received:

  • choose the host with the largest percentageAccum
  • subtract 100 from the percentageAccum for the chosen host
  • add percentageLoad to percentageAccum for all hosts, including the chosen host

A slight optimization is to determine the next choice while updating the percentageAccum for all of the hosts.

Related