Sure.
This is a pretty straightforward thing.
Basically you want to read from the KVM (or some other resource) and then set the target.url to one or the other server endpoint. But let us not confine ourselves to the special case in which there are exactly 2 elements in the list. Suppose the list of servers has N endpoints. The goal of the logic should be to select 1 of those elements randomly, and set target.url to the value of the selected endpoint.
This is pretty easy in JavaScript. You only need to use a random selection on an array.
This function in JavaScript selects a random value from any array ‘a’ :
function selectRandomValue (a) {
var L1 = a.length,
n = Math.floor(Math.random() * L1);
return a[n];
}
Suppose my list is [“foo”, “bar”, “bam”]. That function will randomly return one of those items from the list, each time it is called.
This is the building block you need to produce your simple load balancer. The other complementary pieces are: getting the list from the KVM, and setting the target.url. You already know how to do this, so I won’t include any of that here. I guess you know how to assemble them together.
But now that we have solved the simple case, I suppose you will want more. For example you may want weighted random selection. Each item in the list might have a “weight” which is relative to the others. For example you may want the first item to be selected 50% of the time, the second element to be selected 40% of the time, and the third item 10% of the time. (This is handy when connecting Apigee to upstreams which are undergoing blue/green deployments. Maybe you want Apigee to direct only 2% of its load to the new system, while you verify its behavior.)
To do that you need a weighted random selector. I have one of those for you. [here]
To use it, you would do something like this:
var serverlist = JSON.parse(context.getVariable('servers_retrieved_from_kvm'));
// this list ought to look like this:
// serverip weight
// [
// ["1.2.3.4", 50],
// ["1.2.3.5", 10],
// ["1.2.3.7", 10],
// ["10.20.30.3", 1],
// ["10.20.30.4", 1],
// ["10.20.30.5", 1]
// ]
// Note: the weights are not percentages; they don't need to
// total to 100. The percentage for each item is calculated as
// weight(item)/sum-all(weight(item))
var wrs = new WeightedRandomSelector(serverlist);
var selected = wrs.select();
var selectedServerIP = selected[0];
context.setVariable('target.url', selectedServerIP);
The next thing you may want to do is “health monitoring”. You have a list of N servers, possibly weighted, and you want to load balance across all the nodes in the list, except those nodes that have been marked unhealthy.
That’s something that Apigee does natively with TargetServers. You should not try to implement health monitoring in your API proxies at that layer. It’s better handled in the runtime layer.
I hope this helps.