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?