Past a certain point, web applications outgrow a single server deployment. Companies either want to increase their availability, scalability, or both! To do this, they deploy their application across multiple servers with a load balancer in front to distribute incoming requests. Big companies may need thousands of servers running their web application to handle the load.
In this post we're going to focus on the ways that a single load balancer might distribute HTTP requests to a set of servers. We'll start from the bottom and work our way up to modern load balancing algorithms.
# Visualising the problem
Let's start at the beginning: a single
Each simulation has controls at the bottom. You can
For a lot of websites, this setup works just fine. Modern
Here we see that a rate of 3 RPS causes some
No more
# When round robin doesn't cut it
In the real world, it's rare for
Let's see what happens when we vary
While most
Request queues help us deal with uncertainty, but it's a trade-off. We will
Everything said above applies equally to
The
Despite its flaws, however, round robin is still the default HTTP load balancing method for nginx.
# Improving on round robin
It's possible to tweak round robin to perform better with variance. There's an
algorithm called "weighted round robin" which involves getting humans
to tag each
In this simulation, we use each
While this handles the variance of
It stands to reason that if one
I've added text to each
Let's see how it handles a complex situation, with high variance in both
# Moving away from round robin
Dynamic weighted round robin seems to account well for variance in both
This is called "least connections" load balancing.
Because the
This algorithm performs extremely well regardless how much variance exists.
It cuts through uncertainty by maintaining an accurate understanding of what
each
Let's see this in action in a similarly complex simulation, the same parameters
we gave the dynamic weighted round robin algorithm above. Again, these
parameters are randomised within given ranges, so press
While this algorithm is a great balance between simplicity and performance, it's
not immune to
# Optimizing for latency
Up until now I've been avoiding a crucial part of the discussion: what we're
optimising for. Implicitly, I've been considering
What we're often more concerned about is latency. This is measured in
milliseconds from the moment a
I ran 3 simulations with identical parameters for 60 seconds and took a variety of measurements every second. Each simulation varied only by the load balancing algorithm used. Let's compare the medians for each of the 3 simulations:
You might not have expected it, but round robin has the best median latency. If we weren't looking at any other data points, we'd miss the full story. Let's take a look at the 95th and 99th percentiles.
Note: there's no colour difference between the different percentiles for each load balancing algorithm. Higher percentiles will always be higher on the graph.
We see that round robin doesn't perform well in the higher percentiles. How can it be that round robin has a great median, but bad 95th and 99th percentiles?
In round robin, the state of each
We can take a look at the full data in histogram form:
I chose the parameters for these simulations to avoid
Least connections handles overload much better, but the cost of doing that is slightly higher 95th and 99th percentile latencies. Depending on your use-case, this might be a worthwhile trade-off.
# One last algorithm
If we really want to optimise for latency, we need an algorithm that takes latency into account. Wouldn't it be great if we could combine the dynamic weighted round robin algorithm with the least connections algorithm? The latency of weighted round robin and the resilience of least connections.
Turns out we're not the first people to have this thought. Below is a simulation using an algorithm called "peak exponentially weighted moving average" (or PEWMA). It's a long and complex name but hang in there, I'll break down how it works in a moment.
I've set specific parameters for this simulation that are guaranteed to exhibit
an expected behaviour. If you watch closely, you'll notice that the algorithm
just stops sending
So how does it do this? It combines techniques from dynamic weighted round robin with techniques from least connections, and sprinkles a little bit of its own magic on top.
For each
That value is then taken and multiplied by the number of open connections to the
So how does it compare? First let's take a look at the 50th, 95th, and 99th percentiles when compared against the least connections data from earlier.
We see a marked improvement across the board! It's far more pronounced at the higher percentiles, but consistently present for the median as well. Here we can see the same data in histogram form.
How about
It starts out performing better, but over time performs worse than least
connections. This makes sense. PEWMA is opportunistic in that it tries to get
the best latency, and this means it may sometimes leave a
I want to add here that PEWMA has a lot of parameters that can be tweaked. The implementation I wrote for this post uses a configuration that seemed to work well for the situations I tested it in, but further tweaking could get you better results vs least connections. This is one of the downsides of PEWMA vs least connections: extra complexity.
# Conclusion
I spent a long time on this post. It was difficult to balance realism against ease of understanding, but I feel good about where I landed. I'm hopeful that being able to see how these complex systems behave in practice, in ideal and less-than-ideal scenarios, helps you grow an intuitive understanding of when they would best apply to your workloads.
Obligatory disclaimer: You must always benchmark your own workloads over taking advice from the Internet as gospel. My simulations here ignore some real life constraints (server slow start, network latency), and are set up to display specific properties of each algorithm. They aren't realistic benchmarks to be taken at face value.
To round this out, I leave you with a version of the simulation that lets you tweak most of the parameters in real time. Have fun!
EDIT: Thanks to everyone who participated in the discussions on Hacker News, Twitter and Lobste.rs!
You all had a tonne of great questions and I tried to answer all of them. Some of the common themes were about missing things, either algorithms (like "power of 2 choices") or downsides of algorithms covered (like how "least connections" handles errors from servers).
I tried to strike a balance between post length and complexity of the simulations. I'm quite happy with where I landed, but like you I also wish I could have covered more. I'd love to see people taking inspiration from this and covering more topics in this space in a visual way. Please ping me if you do!
The other common theme was "how did you make this?" The original version used PixiJS. The current version uses a Lit component with SVG for the live simulations and charts. If writing visual explanations like this is something you're interested in, I recommend it!
# Playground
This is Load Balancing V2. You can also read the original V1 version, with its PixiJS simulations and Plotly charts.