What is Weighted Round Robin Load Balancing?
Round robin is a style of load balancing where instead of a server being chosen randomly, the load balancer iterates through the servers based on a predetermined order. In weighted round robin load balancing, the number of consecutive uses of the same server will be based on the weight each server has. In this case, weight is a number you choose that increases the percentage of requests that will go to an indivudal server. Servers with higher weights tend to be more powerful servers with higher CPU counts and RAM capacity.
You can think of weight like a counter. As we receive requests, we send them to one server while keeping count. Once the count is greater than the weight set for that server, we switch to the other one, and restart our count. Again once we have counted more than the number assigned to the second server, we will switch to another (or in our case back to our original server), and begin counting again.
In this section, we're going to be implementing a relatively simple weighted round robin. But first, why would we want round robin, and in particular why a weighted round robin?
If our two servers were to be relatively similar to each other, it shouldn't matter which one a request is sent to, so round robin would be a "fair" load balancer, evenly distributing load to each server. The addition of weighting could help us in cases where our servers aren't well-matched, sending an appropriate ratio of requests to each server.
Regardless, it is a relatively common load balancing strategy, so we're going to add it as an option for our canary deployment server.
Because we now have two methods of load balancing, let's first create a type
for our load balancing style, as well as constants for those two possibilities. In the load_balancer
file:
// RandomPerRequest sends you to a random backend every time.
This lesson preview is part of the Reliable Webservers with Go course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Reliable Webservers with Go, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
