Writing a weighted load balancing algorithm

2019-03-16 19:52发布

问题:

I've to write a weighted load balancing algorithm and I'm looking for some references. Is there any book ? that you can suggest to understand such algorithms.

Thanks!

回答1:

A simple algorithm here isn't that complicated.

Let's say you have a list of servers with the following weights:

A 10
B 20
C 30

Where the higher weight represents it can handle more traffic.

Just divide the amount of traffic sent to each server by the weight and sort smallest to largest. The server that comes out on top gets the user.

for example, let's say each server starts at 10 users, then the order is going to be:

C - 10 / 30 = 0.33
B - 10 / 20 = 0.50
A - 10 / 10 = 1.00

Which means the next 5 requests will go to server C. The 6th request will go to either C or B. The 7th will go to whichever one didn't handle the 6th.


To complicate things, you might want the balancer to be more intelligent. In which case it needs to keep track of how many requests are currently being serviced by each of the servers and decrement them when the request is completely fulfilled.

Further complications include adding stickiness to sessions. Which means the balancer has to inspect each request for the session id and keep track of where they went last time.


On the whole, if you can just buy a product from a company that already does this.



回答2:

Tomcat's balancer app and the tutorial here serve as good starting points.