Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 26

Choosing the Right Load Balancing Strategy

Recommended resources

Introduction

Welcome back! In our last lesson, we established that horizontal scaling—adding more servers—is the key to building resilient, large-scale systems. We concluded that this strategy is impossible without a component acting as a "traffic cop" to distribute incoming requests across our fleet of servers. That component is the load balancer.

Today, we'll zoom in on the "brain" of the load balancer: its routing algorithm. Choosing how to distribute traffic is a critical architectural decision with significant impacts on performance, reliability, and cost. This lesson directly addresses one of the most common system design interview follow-up questions: "Which load balancing algorithm would you choose, and why?"

By the end of this lesson, you will be able to confidently compare load balancing strategies like Round Robin, Least Connections, and IP Hash, and select the appropriate one based on specific application requirements, demonstrating the nuanced, trade-off-driven thinking expected of a senior engineer.


The Role and Types of Load Balancers

Before diving into algorithms, let's quickly solidify the role of a load balancer. It sits between clients and your backend servers, ensuring that no single server is overwhelmed.

Load Balancing - The Right Way To Do It | Systems Design Interview 0 to 1 With Ex-Google SWE

To start, let's watch a brief segment from the 'Load Balancing - The Right Way To Do It' video by Jordan has no life. This will visually place the load balancer within a typical system architecture.

Watch from 01:37 to 02:39. Focus on where load balancers can be placed—not just in front of web servers, but also between other internal layers like caches and databases.

An important distinction to make early on is the layer at which a load balancer operates. This determines how much information it has to make a routing decision.

  • Layer 4 (Transport Layer): Operates on the TCP/UDP level. It forwards traffic based on IP addresses and ports.

    • Pros: Very fast and efficient as it doesn't need to inspect the content of the request.
    • Cons: "Dumb" routing. It can't make decisions based on what the user is trying to do (e.g., read a specific URL or use a certain HTTP method).
  • Layer 7 (Application Layer): Operates at the HTTP level. It can inspect the full request, including headers, cookies, URL paths, and the request body.

    • Pros: Enables "smart" routing. You can route traffic based on the URL (/api/v2/* goes to one pool of servers, /api/v1/* to another), user identity (based on a cookie), or other application-specific logic.
    • Cons: Slower than Layer 4 because it has to parse the HTTP request. This overhead is usually negligible for the flexibility it provides.

For most modern backend systems, we are primarily concerned with Layer 7 load balancing because it allows for more intelligent and flexible traffic management.


A Deep Dive into Load Balancing Algorithms

The core of our lesson is understanding the different algorithms a load balancer can use. Each one represents a different trade-off between simplicity, adaptiveness, and complexity.

We will use a comprehensive article as our main guide for this section. It provides the depth and detail needed for a senior-level discussion.

Design Load Balancer

The article 'Design Load Balancer' from AlgoMaster.io offers an excellent, in-depth comparison of the most important algorithms. It covers not just how they work, but also their strengths, weaknesses, and ideal use cases.

Read the section titled '6.1 Load Balancing Algorithms'. It's a detailed section, so take your time. For each algorithm (Approaches 1 through 6), focus on understanding the core mechanism and the 'When it falls short' and 'Best for' subsections.

Now, let's consolidate and structure the information from that reading into an interview-ready framework. We can group the algorithms into two main categories: static (which follow a fixed pattern) and dynamic (which adapt to real-time server conditions).

Load Balancing Algorithms Visual Comparison
This diagram illustrates the request distribution patterns of several common load balancing algorithms, including Round Robin, IP Hash, and Least Connections.

Static Algorithms: Simple and Predictable

These algorithms don't consider the current state of the backend servers.

  1. Round Robin:

    • How it works: Sequentially cycles through the list of servers. Request 1 -> Server A, Request 2 -> Server B, Request 3 -> Server C, Request 4 -> Server A.
    • Pros: Extremely simple, stateless, and predictable.
    • Cons: Blind to server capacity and real-time load. A slow or overloaded server will continue to receive traffic.
    • Interview Answer: "A good, simple baseline for a fleet of identical, stateless servers with uniform request costs."
  2. Weighted Round Robin:

    • How it works: A variation of Round Robin where you assign a "weight" to each server. A server with a weight of 3 will receive three times the traffic of a server with a weight of 1.
    • Pros: Accounts for heterogeneous server capacities (e.g., a powerful c5.2xlarge instance vs. a smaller t3.medium).
    • Cons: Still blind to transient load. The weights are static.
    • Interview Answer: "Useful when you have a mixed fleet of servers with known, different capacities."
  3. IP Hash:

    • How it works: The load balancer calculates a hash of the client's source IP address and uses that to select a server.
    • Pros: Provides "sticky sessions" or session persistence without storing any state. The same client will always be routed to the same server.
    • Cons:
      • Breaks with NAT: Many users behind a corporate or mobile network share the same public IP, creating a "hot spot" on one server.
      • Uneven Distribution: Hashing can lead to an uneven spread of traffic.
      • Poor Scalability: If you add or remove a server, the result of hash(IP) % num_servers changes for most clients, invalidating their sessions.
    • Interview Answer: "A simple way to achieve session persistence at Layer 4, but it's brittle. Cookie-based stickiness at Layer 7 is usually a better approach. The remapping problem on server changes is a major drawback."

Dynamic Algorithms: Smart and Adaptive

These algorithms monitor the servers and make decisions based on real-time metrics.

  1. Least Connections:

    • How it works: The load balancer tracks the number of active connections to each backend server and sends the next request to the server with the fewest connections.
    • Pros: Naturally adaptive. It automatically sends less traffic to slow or overloaded servers because they will hold onto their connections for longer. It handles variable request costs well.
    • Cons: Requires the load balancer to maintain state (connection counts).
    • Interview Answer: "An excellent choice for workloads where request processing times can vary significantly. It prevents a few slow requests from causing a server to become a bottleneck."
  2. Weighted Least Connections:

    • How it works: Combines weighted round robin and least connections. It sends the new request to the server with the lowest ratio of active_connections / weight.
    • Pros: This is often the most effective algorithm for general HTTP traffic. It accounts for both the pre-configured capacity of servers (weight) and their real-time load (connections).
    • Interview Answer: "This would be my default choice for a production API fleet. It's capacity-aware and adapts to dynamic load, providing the best of both worlds."

A Special Case: Consistent Hashing

This algorithm addresses the critical flaw of simple hashing methods like IP Hash. It's a crucial concept for senior-level interviews.

Load Balancing - The Right Way To Do It | Systems Design Interview 0 to 1 With Ex-Google SWE

To understand why Consistent Hashing is so powerful, let's return to the Jordan has no life video. Its visualization of the 'hash ring' is one of the clearest explanations available.

Watch from 04:48 to 07:50. Pay very close attention to the ring diagram and the explanation of what happens when a new server is added or an old one is removed. This is the key insight.

  • How it works: Both servers and request keys (e.g., a user ID or a cache key) are hashed onto a conceptual ring. A request is routed to the first server found clockwise from its position on the ring.
  • The Key Advantage: When a server is added or removed, only the keys that would have mapped to it are redistributed. With simple hash % N hashing, almost all keys get remapped.
  • Interview Answer: "Consistent hashing is not typically used for general stateless API load balancing. Its real power is in stateful, distributed systems. It's essential for load balancing distributed caches like Redis or Memcached, as it minimizes cache misses (and subsequent database load) when cache nodes are added or removed. It's also fundamental to sharded databases like DynamoDB or Cassandra."

Choosing the Right Algorithm: A Framework for Discussion

In an interview, you'll earn points not by stating the "right" algorithm, but by demonstrating your thought process. Here's a summary table 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 Known capacity differences among backends
Least Connections Moderate Per-node state High Workloads with variable request costs
Weighted Least Conn. Moderate Per-node state High Default for production HTTP with mixed servers
IP Hash Simple Stateless None Basic session persistence (use with caution)
Consistent Hashing Complex Stateless None Distributed caches, sharded DBs, dynamic scaling

Table adapted from the AlgoMaster.io article.

Load Balancing Algorithms and Use Cases
This table provides concrete examples of when to apply each of the main load balancing algorithms.

An Interview Scenario

Interviewer: "You're designing the backend for an e-commerce site. Which load balancing algorithm would you choose for your main application servers?"

Your Thought Process:

  1. Acknowledge the trade-offs: "The best choice depends on the specific characteristics of our application servers and traffic. I'd start by considering if the servers are stateful or stateless."
  2. State your assumptions: "Assuming our application servers are stateless—which is a best practice for scalability—we can rule out needing sticky sessions for simple requests."
  3. Propose a robust default: "My default choice would be Weighted Least Connections. Here's why:
    • It's adaptive. E-commerce traffic is bursty and request costs vary (browsing an item is cheap; checking out is expensive). Least Connections ensures that a server getting bogged down by expensive checkout operations won't be flooded with new requests.
    • It's capacity-aware. In a cloud environment, we might use a mix of instance types. The 'weighted' part allows us to route traffic proportionally to each server's capacity."
  4. Discuss alternatives and their drawbacks: "We could start with something simpler like Round Robin, but it's not robust enough for a production e-commerce site as it doesn't adapt to load. We could use IP Hash if we had a legacy stateful component, but that's a fragile solution I'd avoid in a new design."

Conclusion

You now have a solid framework for analyzing and discussing load balancing strategies. This topic is a cornerstone of system design and a great opportunity to showcase your understanding of how to build scalable and resilient systems.

Key Takeaways:

  • Load balancers are the enablers of horizontal scaling. They can operate at Layer 4 (fast but simple) or Layer 7 (smarter but with more overhead).
  • Static algorithms (Round Robin, Weighted RR) are simple but blind to real-time load.
  • Dynamic algorithms (Least Connections) are adaptive and generally better for real-world, variable workloads.
  • Weighted Least Connections is often the best all-around choice for a production HTTP/API tier.
  • Consistent Hashing is a specialized, but critical, algorithm for stateful distributed systems like caches and databases, as it minimizes disruption during scaling events.

Preview of the Next Lesson:

We've successfully distributed traffic to our backend servers. Now, how do we make those servers respond as quickly as possible? One of the most powerful techniques is caching. In our next lesson, we will explore "Caching strategies," covering different caching patterns like write-through and write-around, and discussing where to place caches in your architecture for maximum impact. You'll see how concepts like consistent hashing are directly applied to make distributed caching effective.