Hello! Welcome to the second lesson in our module on "Scalability and Performance Principles."
In our previous lesson, we established the fundamental difference between vertical and horizontal scaling. We concluded that horizontal scaling is the key to building massively scalable systems, but it introduces a critical challenge: how do you distribute traffic intelligently across a pool of servers? Today, we answer that question.
This lesson directly addresses the learning outcome: Describe load balancing strategies and their trade-offs (round-robin, least connections, consistent hashing). Mastering these concepts is non-negotiable for a senior engineering interview, as it demonstrates your ability to design systems that are both resilient and performant.
1. What is a Load Balancer?
Before diving into specific strategies, let's solidify our understanding of what a load balancer is and where it fits into a system's architecture. At its core, a load balancer is a reverse proxy that acts as a "traffic cop" for your backend services.
Let's watch a short video to visualize this.
Load Balancing - The Right Way To Do It | Systems Design Interview 0 to 1 With Ex-Google SWE
This clip from the 'Jordan has no life' channel introduces the concept of a load balancer and shows how it can be placed between different layers of an application stack.
Watch from 01:37 to 02:36. Focus on understanding the primary role of a load balancer: to distribute requests across multiple nodes to prevent any single one from being overloaded or starved.
As the video illustrates, load balancers are not just for distributing client traffic to web servers. They are a fundamental pattern used between any two layers of a distributed system where you have multiple instances of the downstream service (e.g., between web servers and an application cache, or between application servers and a database cluster).
The decision-making logic inside a load balancer is governed by a load balancing algorithm. Let's explore the most important ones.
2. A Framework for Algorithms: Static vs. Dynamic
Load balancing algorithms can be broadly divided into two categories. This distinction provides a useful mental model for comparing them.
Top 6 Load Balancing Algorithms Every Developer Should Know
The ByteByteGo channel provides a clear overview of static versus dynamic load balancing algorithms. This will help us frame our discussion.
Watch the first minute of the video (00:07 to 01:17). Pay attention to the core difference: static algorithms do not consider the real-time state of the servers, while dynamic algorithms do.
To summarize:
- Static Algorithms: Distribute traffic based on a fixed, predetermined pattern. They are simple and fast but "blind" to the current load on the servers.
- Dynamic Algorithms: Monitor the servers and make real-time routing decisions based on performance metrics like connection count or response time. They are more intelligent but introduce complexity and overhead.
Now, let's dive deep into the specific algorithms you'll be expected to know in an interview. We'll use a fantastic article from AlgoMaster.io as our guide.
Design Load Balancer | System Design Interview - AlgoMaster.io
The article 'Design Load Balancer' provides an exceptional deep dive into various algorithms. We will use it as our primary reference for this section.
For now, just be aware of this resource. We will read specific sections of it as we cover each algorithm.
3. Core Load Balancing Algorithms
Let's explore the mechanics and trade-offs of the most common algorithms.
Round Robin and Weighted Round Robin
This is the simplest static algorithm.
Design Load Balancer | System Design Interview - AlgoMaster.io
Let's read about the Round Robin and Weighted Round Robin algorithms.
Read the sections titled 'Approach 1: Round Robin' and 'Approach 2: Weighted Round Robin'. Focus on how Round Robin works, its primary weakness, and how Weighted Round Robin addresses that weakness.
- Round Robin: Sequentially distributes requests to servers in a circular order.
- Pros: Extremely simple, stateless, and computationally cheap.
- Cons: Treats all servers as equal. If one server is slower or has less capacity, it can become overloaded.
- Weighted Round Robin: Assigns a "weight" to each server, distributing a proportional number of requests based on that weight.
- Pros: Accounts for heterogeneous server capacities (e.g., a mix of powerful and weaker machines).
- Cons: The weights are static. It doesn't adapt to transient issues like a server slowing down due to garbage collection or a temporary spike in expensive requests.
Least Connections
This is a classic dynamic algorithm that addresses the "blindness" of Round Robin.
Design Load Balancer | System Design Interview - AlgoMaster.io
Now, let's examine a dynamic approach that considers real-time server load.
Read the sections 'Approach 3: Least Connections' and 'Approach 4: Weighted Least Connections'. Understand how this algorithm naturally adapts to servers with different processing speeds and variable request costs.
- Least Connections: Sends the next request to the server with the fewest active connections.
- Pros: Naturally adaptive. A server that is slow or busy with long-running requests will have a higher connection count and will automatically receive less new traffic. This self-balancing property is very powerful.
- Cons: Requires the load balancer to track the state (connection counts) for each backend.
- Weighted Least Connections: A hybrid approach that sends traffic to the server with the lowest ratio of
connections / weight. This is often the best general-purpose choice, as it accounts for both pre-configured capacity (weight) and real-time load (connections).
This image provides a helpful visual summary of several algorithms, including the ones we've just discussed.

Hash-Based Methods and the Rise of Consistent Hashing
Sometimes, you need requests from the same user to always go to the same server. This is called session persistence or "stickiness." A simple way to achieve this is with a hash-based algorithm.
However, this simple approach has a major flaw.
Design Load Balancer | System Design Interview - AlgoMaster.io
First, read about the simple IP Hash method and its significant limitations from our main article.
Read the section 'Approach 5: IP Hash (Source Hashing)'. Pay close attention to the three problems it lists, especially the massive reshuffling that occurs when backends are added or removed.
This "massive reshuffling" is often called the rehashing problem. When you use a simple hash(key) % N formula, changing N (the number of servers) changes the destination for almost every key. This is catastrophic for caches (causing mass cache misses) and disruptive for user sessions.
Consistent Hashing was invented to solve this exact problem.
Design Consistent Hashing - ByteByteGo | Technical Interview Prep
This article from ByteByteGo provides an excellent, focused explanation of the rehashing problem and how consistent hashing solves it.
Read the sections 'The rehashing problem' to solidify your understanding of the issue, and then 'Consistent hashing', 'Add a server', and 'Remove a server' to learn the core mechanism. Focus on the concept of the hash ring.
The basic consistent hashing algorithm has its own issues, namely that server partitions on the ring can be uneven. This is solved by using virtual nodes.
Design Load Balancer | System Design Interview - AlgoMaster.io
Finally, let's see how virtual nodes improve distribution and summarize the use cases for consistent hashing.
Now, read 'Approach 6: Consistent Hashing' in the AlgoMaster article. This provides a great summary and explains the crucial concept of 'virtual nodes' for achieving better load distribution.
- Consistent Hashing: Maps servers and keys onto a circular hash ring. A key is assigned to the first server found moving clockwise from the key's position.
- Pros: When a server is added or removed, only a small fraction of keys are remapped. This makes it ideal for dynamic environments like auto-scaling groups or distributed caches (e.g., Memcached).
- Cons: More complex to implement than other algorithms.
4. Choosing the Right Algorithm: An Interview-Ready Summary
In an interview, you must be able to recommend an algorithm and justify your choice. The best choice depends entirely on the system's requirements.
Here is a summary of the trade-offs to guide your decision-making.
| Algorithm | Complexity | Statefulness | Adaptiveness | Best For |
|---|---|---|---|---|
| Round Robin | Very Simple | Stateless | None | Homogeneous backends, uniform requests |
| Weighted Round Robin | Simple | Stateless | None | Backends with known, fixed capacity differences |
| Least Connections | Moderate | Per-node state | High | Workloads with variable request costs or processing times |
| Weighted Least Conn. | Moderate | Per-node state | High | Strong default choice for most production HTTP apps |
| IP Hash | Simple | Stateless | None | Basic session persistence (use with caution) |
| Consistent Hash | Complex | Stateless | None | Dynamic scaling environments, distributed caches, databases |
How to articulate this in an interview:
"For the stateless API servers, I would start with Weighted Least Connections. This algorithm is adaptive and will naturally balance the load even if some requests are more expensive than others or if we have a mix of server instance types.
However, for the distributed cache layer, we must use Consistent Hashing. This ensures that when we add or remove cache nodes, we minimize the number of cache misses by only remapping a small fraction of the keys, which is critical for maintaining performance."
Conclusion
Today we've demystified load balancing, a cornerstone of scalable system design. You've moved beyond simple definitions to understand the deep trade-offs between the most important algorithms.
Key Takeaways:
- Load balancing enables horizontal scaling by distributing traffic across a pool of servers.
- Round Robin is simple but blind to server state. Weighted Round Robin accounts for different server capacities but is still static.
- Least Connections is a dynamic algorithm that adapts to real-time load, making it a robust choice for many applications.
- Consistent Hashing is the essential solution for dynamic environments where servers are frequently added or removed, as it minimizes the disruption of remapping keys.
- Your choice of algorithm must be a deliberate trade-off, justified by the specific needs of the system components you are designing.
In our next lesson, we will explore another critical performance-enhancing technique: caching strategies. We'll discuss patterns like write-through, write-back, and write-around, and see how effective caching can drastically reduce the load on your databases and backend services.