0% completed
Load Balancing Algorithms
On This Page
- Round Robin
Use Cases
- Least Connections
Use Cases
Comparison to Round Robin
- Weighted Round Robin
Pros
Cons
Use Cases
- Weighted Least Connections
Pros
Cons
Use Cases
- IP Hash
Example
Pros
Cons
Use Cases
- Least Response Time
How Least Response Time Load Balancing Works
Pros
Cons
Use Cases
- Random
Pros
Cons
Use Cases
- Least Bandwidth
Pros
Cons
Use Cases
- Custom Load
How Custom Load Load Balancing Works
Pros
Cons
Use Cases
How to Choose
Key Takeaways
A load balancing algorithm is a method used by a load balancer to distribute incoming traffic and requests among multiple servers or resources. The primary purpose of a load balancing algorithm is to ensure efficient utilization of available resources, improve overall system performance, and maintain high availability and reliability.
Load balancing algorithms help to prevent any single server or resource from becoming overwhelmed, which could lead to performance degradation or failure. By distributing the workload, load balancing algorithms can optimize response times, maximize throughput, and enhance user experience. These algorithms can consider factors such as server capacity, active connections, response times, and server health, among others, to make informed decisions on how to best distribute incoming requests.
Here are the most famous load balancing algorithms:
1. Round Robin
This algorithm distributes incoming requests to servers in a cyclic order. It assigns a request to the first server, then moves to the second, third, and so on, and after reaching the last server, it starts again at the first.
Pros:
- Ensures an equal distribution of requests among the servers, as each server gets a turn in a fixed order.
- Easy to implement and understand.
- Works well when servers have similar capacities.
Cons:
- No Load Awareness: Does not take into account the current load or capacity of each server. All servers are treated equally regardless of their current state.
- No Session Affinity: Subsequent requests from the same client may be directed to different servers, which can be problematic for stateful applications.
- Performance Issues with Different Capacities: May not perform optimally when servers have different capacities or varying workloads.
- Predictable Distribution Pattern: Round Robin is predictable in its request distribution pattern, which could potentially be exploited by attackers who can observe traffic patterns and might find vulnerabilities in specific servers by predicting which server will handle their requests.
Use Cases
- Homogeneous Environments: Suitable for environments where all servers have similar capacity and performance.
- Stateless Applications: Works well for stateless applications where each request can be handled independently.
2. Least Connections
The Least Connections algorithm is a dynamic load balancing technique that assigns incoming requests to the server with the fewest active connections at the time of the request. This method ensures a more balanced distribution of load across servers, especially in environments where traffic patterns are unpredictable and request processing times vary.
Pros:
- Load Awareness: Takes into account the current load on each server by considering the number of active connections, leading to better utilization of server resources.
- Dynamic Distribution: Adapts to changing traffic patterns and server loads, ensuring no single server becomes a bottleneck.
- Adapts to Uneven Requests: Performs well when requests vary widely in how long they take, because slow requests hold connections open and new requests are steered to less busy servers.
Cons:
- Higher Complexity: More complex to implement compared to simpler algorithms like Round Robin, as it requires real-time monitoring of active connections.
- State Maintenance: Requires the load balancer to maintain the state of active connections, which can increase overhead.
- Potential for Connection Spikes: In scenarios where connection duration is short, servers can experience rapid spikes in connection counts, leading to frequent rebalancing.
Use Cases
- Uneven Request Durations: Suitable when requests vary widely in processing time, so counting active connections tracks real load. Note that plain Least Connections treats every server as equally capable; when server capacities differ, Weighted Least Connections is the right variant.
- Variable Traffic Patterns: Works well for applications with unpredictable or highly variable traffic patterns, ensuring that no single server is overwhelmed.
- Stateful Applications: Effective for applications where maintaining session state is important, as it helps distribute active sessions more evenly.
Comparison to Round Robin
- Round Robin: Distributes requests in a fixed, cyclic order without considering the current load on each server.
- Least Connections: Distributes requests based on the current load, directing new requests to the server with the fewest active connections.
3. Weighted Round Robin
Weighted Round Robin (WRR) is an enhanced version of the Round Robin load balancing algorithm. It assigns weights to each server based on their capacity or performance, distributing incoming requests proportionally according to these weights. This ensures that more powerful servers handle a larger share of the load, while less powerful servers handle a smaller share.
Pros
- Load Distribution According to Capacity: Servers with higher capacities handle more requests, leading to better utilization of resources.
- Flexibility: Easily adjustable to accommodate changes in server capacities or additions of new servers.
- Improved Performance: Helps in optimizing overall system performance by preventing overloading of less powerful servers.
Cons
- Complexity in Weight Assignment: Determining appropriate weights for each server can be challenging and requires accurate performance metrics.
- Increased Overhead: Managing and updating weights can introduce additional overhead, especially in dynamic environments where server performance fluctuates.
- Not Ideal for Highly Variable Loads: In environments with highly variable load patterns, WRR may not always provide optimal load balancing as it doesn't consider real-time server load.
Use Cases
- Heterogeneous Server Environments: Ideal for environments where servers have different processing capabilities, ensuring efficient use of resources.
- Scalable Web Applications: Suitable for web applications where different servers may have varying performance characteristics.
- Database Clusters: Useful in database clusters where some nodes have higher processing power and can handle more queries.
4. Weighted Least Connections
Weighted Least Connections is an advanced load balancing algorithm that combines the principles of the Least Connections and Weighted Round Robin algorithms. It takes into account both the current load (number of active connections) on each server and the relative capacity of each server (weight). This approach ensures that more powerful servers handle a proportionally larger share of the load, while also dynamically adjusting to the real-time load on each server.
Pros
- Dynamic Load Balancing: Adjusts to the real-time load on each server, ensuring a more balanced distribution of requests.
- Capacity Awareness: Takes into account the relative capacity of each server, leading to better utilization of resources.
- Flexibility: Can handle environments with heterogeneous servers and variable load patterns effectively.
Cons
- Complexity: More complex to implement compared to simpler algorithms like Round Robin and Least Connections.
- State Maintenance: Requires the load balancer to keep track of both active connections and server weights, increasing overhead.
- Weight Assignment: Determining appropriate weights for each server can be challenging and requires accurate performance metrics.
Use Cases
- Heterogeneous Server Environments: Ideal for environments where servers have different processing capacities and workloads.
- High Traffic Web Applications: Suitable for web applications with variable traffic patterns, ensuring no single server becomes a bottleneck.
- Database Clusters: Useful in database clusters where nodes have varying performance capabilities and query loads.
5. IP Hash
IP Hash load balancing is a technique that assigns client requests to servers based on the client's IP address. The load balancer uses a hash function to convert the client's IP address into a hash value, which is then used to determine which server should handle the request. This method ensures that requests from the same client IP address are consistently routed to the same server, providing session persistence.
Example
Suppose you have three servers (Server A, Server B, and Server C) and a client with the IP address 192.168.1.10. The load balancer applies a hash function to this IP address, resulting in a hash value. If the hash value is 2 and there are three servers, the load balancer routes the request to Server C (2 % 3 = 2).
Pros
- Session Persistence: Ensures that requests from the same client IP address are consistently routed to the same server, which is beneficial for stateful applications.
- Simplicity: Easy to implement and does not require the load balancer to maintain the state of connections.
- Deterministic: Predictable and consistent routing based on the client's IP address.
Cons
- Uneven Distribution: If client IP addresses are not evenly distributed, some servers may receive more requests than others, leading to an uneven load.
- Dynamic Changes: Adding or removing servers can disrupt the hash mapping, causing some clients to be routed to different servers.
- Limited Flexibility: Does not take into account the current load or capacity of servers, which can lead to inefficiencies.
A note on consistent hashing. The Dynamic Changes con has a standard solution. Instead of hashing modulo the number of servers, consistent hashing places servers and clients on a ring, so adding or removing a server remaps only about 1/N of the clients instead of nearly all of them. Load balancers that need stickiness and frequent membership changes use it for exactly this reason.
Use Cases
- Stateful Applications: Ideal for applications where maintaining session persistence is important, such as online shopping carts or user sessions.
- Stickiness Without Cookies: Useful when session persistence is needed but cookies are not available, since the client IP is visible even to a Layer 4 load balancer. Note that IP Hash does not route clients to nearby servers; placing users in the right region is the job of GeoDNS or anycast, before traffic reaches this load balancer.
6. Least Response Time
Least Response Time load balancing is a dynamic algorithm that assigns incoming requests to the server with the lowest response time, ensuring efficient utilization of server resources and optimal client experience. This approach aims to direct traffic to the server that can handle the request the fastest, based on recent performance metrics.
How Least Response Time Load Balancing Works
- Monitor Response Times: The load balancer continuously monitors the response times of each server. Response time is typically measured from when a request is sent to a server until a response is received.
- Assign Requests: When a new request arrives, the load balancer assigns it to the server with the lowest average response time.
- Dynamic Adjustment: The load balancer dynamically adjusts the assignment of requests based on real-time performance data, ensuring that the fastest server handles the next request.
Pros
- Optimized Performance: Ensures that requests are handled by the fastest available server, leading to reduced latency and improved client experience.
- Dynamic Load Balancing: Continuously adjusts to changing server performance, ensuring optimal distribution of load.
- Effective Resource Utilization: Helps in better utilization of server resources by directing traffic to servers that can respond quickly.
Cons
- Complexity: More complex to implement compared to simpler algorithms like Round Robin, as it requires continuous monitoring of server performance.
- Overhead: Monitoring response times and dynamically adjusting the load can introduce additional overhead.
- Short-Term Variability: Response times can vary in the short term due to network fluctuations or transient server issues, potentially causing frequent rebalancing.
Use Cases
- Real-Time Applications: Ideal for applications where low latency and fast response times are critical, such as online gaming, video streaming, or financial trading platforms.
- Web Services: Useful for web services and APIs that need to provide quick responses to user requests.
- Dynamic Environments: Suitable for environments with fluctuating loads and varying server performance.
7. Random
Random load balancing is a simple algorithm that distributes incoming requests to servers randomly. Instead of following a fixed sequence or using performance metrics, the load balancer selects a server at random to handle each request. This method can be effective in scenarios where the load is relatively uniform and servers have similar capacities.
Suppose you have three servers: Server A, Server B, and Server C. When a new request arrives, the load balancer randomly chooses one of these servers to handle the request. Over time, if the randomness is uniform, each server should receive approximately the same number of requests.
Pros
- Simplicity: Very easy to implement and understand, requiring minimal configuration.
- No State Maintenance: The load balancer does not need to track the state or performance of servers, reducing overhead.
- Uniform Distribution Over Time: If the random selection is uniform, the load will be evenly distributed across servers over a long period.
Cons
- No Load Awareness: Does not consider the current load or capacity of servers, which can lead to uneven distribution if server performance varies.
- Potential for Imbalance: In the short term, random selection can lead to an uneven distribution of requests.
- No Session Affinity: Requests from the same client may be directed to different servers, which can be problematic for stateful applications.
- Security systems that rely on detecting anomalies (e.g., to mitigate DDoS attacks) might find it slightly more challenging to identify malicious patterns if a Random algorithm is used, due to the inherent unpredictability in request distribution. This could potentially dilute the visibility of attack patterns.
Use Cases
- Homogeneous Environments: Suitable for environments where servers have similar capacity and performance.
- Stateless Applications: Works well for stateless applications where each request can be handled independently.
- Simple Deployments: Ideal for simple deployments where the complexity of other load balancing algorithms is not justified.
8. Least Bandwidth
The Least Bandwidth load balancing algorithm distributes incoming requests to servers based on the current bandwidth usage. It routes each new request to the server that is consuming the least amount of bandwidth at the time. This approach helps in balancing the network load more efficiently by ensuring that no single server gets overwhelmed with too much data traffic.
Pros
- Dynamic Load Balancing: Continuously adjusts to the current network load, ensuring optimal distribution of traffic.
- Prevents Overloading: Helps in preventing any single server from being overwhelmed with too much data traffic, leading to better performance and stability.
- Efficient Resource Utilization: Ensures that all servers are utilized more effectively by balancing the bandwidth usage.
Cons
- Complexity: More complex to implement compared to simpler algorithms like Round Robin, as it requires continuous monitoring of bandwidth usage.
- Overhead: Monitoring bandwidth and dynamically adjusting the load can introduce additional overhead.
- Short-Term Variability: Bandwidth usage can fluctuate in the short term, potentially causing frequent rebalancing.
Use Cases
- High Bandwidth Applications: Ideal for applications with high bandwidth usage, such as video streaming, file downloads, and large data transfers.
- Content Delivery Networks (CDNs): Useful for CDNs that need to balance traffic efficiently to deliver content quickly.
- Bandwidth-Bound Workloads: Best when the network link, not the CPU, is the resource that runs out first. For latency-sensitive traffic with small responses, Least Response Time is the better fit, because bandwidth usage says little about how quickly a small request completes.
9. Custom Load
Custom Load load balancing is a flexible and highly configurable approach that allows you to define your own metrics and rules for distributing incoming traffic across a pool of servers. Unlike standard load balancing algorithms that use predefined criteria such as connection count or response time, Custom Load load balancing enables you to tailor the distribution strategy based on specific requirements and conditions unique to your application or infrastructure.
How Custom Load Load Balancing Works
-
Define Custom Metrics: Determine the metrics that best represent the load or performance characteristics relevant to your application. These metrics can include CPU usage, memory usage, disk I/O, application-specific metrics, or a combination of several metrics.
-
Implement Monitoring: Continuously monitor the defined metrics on each server in the pool. This may involve integrating with monitoring tools or custom scripts that collect and report the necessary data.
-
Create Load Balancing Rules: Establish rules and algorithms that use the monitored metrics to make load balancing decisions. This can be a simple weighted sum of metrics or more complex logic that prioritizes certain metrics over others.
-
Dynamic Adjustment: Use the collected data and rules to dynamically adjust the distribution of incoming requests, ensuring that the traffic is balanced according to the custom load criteria.
Pros
- Flexibility: Allows for highly customized load balancing strategies tailored to the specific needs and performance characteristics of your application.
- Optimized Resource Utilization: Can lead to more efficient use of server resources by considering a comprehensive set of metrics.
- Adaptability: Easily adaptable to changing conditions and requirements, making it suitable for complex and dynamic environments.
Cons
- Complexity: More complex to implement and configure compared to standard load balancing algorithms.
- Monitoring Overhead: Requires continuous monitoring of multiple metrics, which can introduce additional overhead.
- Potential for Misconfiguration: Incorrectly defined metrics or rules can lead to suboptimal load balancing and performance issues.
Use Cases
- Complex Applications: Ideal for applications with complex performance characteristics and varying resource requirements.
- Highly Dynamic Environments: Suitable for environments where workloads and server performance can change rapidly and unpredictably.
- Custom Requirements: Useful when standard load balancing algorithms do not meet the specific needs of the application.
How to Choose
For most systems, the honest answer is that round robin or least connections is enough.
- Servers are the same size and requests are short: round robin.
- Servers are different sizes: weighted round robin.
- Requests take very different amounts of time, or connections stay open: least connections.
- Different sizes and uneven requests: weighted least connections.
- Responses are large files or video: least bandwidth.
- The same user must reach the same server: IP hash. It is usually better to move the session into a shared cache instead, and remove the need for stickiness.
In an interview, name the algorithm, then say what it does not know. That second part is what shows you understand it.
Key Takeaways
- The algorithm only picks between healthy servers. Health checks are a separate mechanism, so "ignores server health" is not a weakness of any algorithm.
- Round robin is simple and counts requests, not work. Weighted round robin adds server size, which you must configure by hand.
- Least connections reacts to real load with no configuration, but it is blind to server capacity. Weighted least connections fixes that.
- Least response time can be fooled by a server that fails quickly, because an error is fast.
- Least bandwidth measures the size of responses, not speed. It is for large downloads and video.
- IP hash gives stickiness. It does not route by location, it spreads load unevenly, and adding a server moves almost every client. Consistent hashing solves the last problem.
- Random of two choices is nearly as good as least connections and much cheaper to run.
The algorithm matters less than most people expect. Uneven load usually comes from somewhere else. Sessions kept in server memory, one very popular item, or requests that cost very different amounts. Fix those first.
ruthvicsai.s
· 2 months ago
How does Least Connections perform well in heterogeneous envs?
If server A and B can handle max 10 connections, while C can handle 5. Least connections does not know this, and can overload C since it has the lowest active connections.
Weighted least connections fixes this issue and could be good in these situations, however I do not see least connections performing well.
Is there an angle I am missing?
Piyush Kuhikar
· 2 months ago
what does capacity mean in weighted round robin?
"Capacity” usually means how much traffic that backend can handle relative to others, which can be based on different limiting resources, such as:
- network bandwidth (link speed / expected throughput),
- CPU and request processing capacity,
- memory / concurrency limits,
- observed performance (latency/throughput under load),
- hardware specs or SLA targets.
Piyush Kuhikar
· 2 months ago
I was confused between Least Bandwidth and Least Connections LB. Had to look over the internet. Here's what I found: In a Least Bandwidth load balancer, the balancer tries to send each new request to the backend that has the most available network capacity (equivalently, the backend with the lowest current bandwidth usage / lowest estimated load in terms of bytes-in/bytes-out). Practically, it means it’s tracking traffic rate (throughput), not just how many requests are happening.
Least Connections instead routes to the backend with the fewest active/ongoing connections (or least inflight requests). It’s tracking count of concurrent sessions, not how much data each session is moving.
Sajid Khan
· 4 months ago
I'm still fairly new to system design, so I spent quite a bit of time on this page because I prefer to fully understand a concept before moving on. While going through it, I was also doing some parallel research to validate my understanding.
From what I've read, Least Connections alone may not be sufficient in a heterogeneous environment because it doesn't take server capacity into account. A less powerful server and a high-capacity server could end up receiving a similar number of connections, which may lead to an uneven load distribution.
Because of that, I think this advantage should be attributed specifically to Weighted Least Connections rather than Least Connections in general, since the weighted variant considers the relative capacity of each server.
Please let me kno
anubhav
· 4 months ago
IN Least Response Time you said request to be routed to LB with least average response time. I think Average is not a good metric when it comes to response time as a single request suppose requesting for heavy data from backend will shoot up the average response time fast. It is suspectible to outliers instead better metric is median like p50 or p90 or p99 etc. They will give how many users request are fast and who are slowest (p99) generally users which are most critical since they have the most data. Median seems to be better metric
SOHEL RANA
· 4 months ago
I think if LB algos is used as per requests not connection then only IP Hash algo could be used for stateful web application. Here context is Least connection and Least bandwidth algo are used as per connection which is why it does get used in stateful scenario.
But if this is the case then other algo, least response, round robin can also be used. can anyone please clarify on this?
rvigneshramanujam
· 9 months ago
In case of a Microservice application , where the servers are added and removed dynamically and are short lived , which type of Load Balancer algorithm s used
Sid D
· a year ago
CDN's don't specifically have any high bandwidth demands. Its more application specific. So what makes LBW approach specifically useful for CDN use case? Can you expand further on this?
Sid D
· a year ago
An IP can map to any server from the pool. Its not possible that an IP from a specific region maps to a specific server that is geographically close to it. Then why is it mentioned that IP hash LB is useful in geographically distributed clients?
Aldemar Hernández
· 2 years ago
Why one of the use cases for least connections algorithm is stateful applications if information that must be maintained is active connections to each server and not session state.
Reading Progress
0%
On This Page
- Round Robin
Use Cases
- Least Connections
Use Cases
Comparison to Round Robin
- Weighted Round Robin
Pros
Cons
Use Cases
- Weighted Least Connections
Pros
Cons
Use Cases
- IP Hash
Example
Pros
Cons
Use Cases
- Least Response Time
How Least Response Time Load Balancing Works
Pros
Cons
Use Cases
- Random
Pros
Cons
Use Cases
- Least Bandwidth
Pros
Cons
Use Cases
- Custom Load
How Custom Load Load Balancing Works
Pros
Cons
Use Cases
How to Choose
Key Takeaways