How do Load Balancers Distribute Traffic?
Understanding Different Types of Load Balancing Algorithms
What is Load Balancing?
As a service grows, the need to improve fundamental properties like scalability, response time, performance, and availability becomes crucial. One effective way to achieve this is by setting up replicas of the service and distributing incoming traffic evenly across all instances. This method, known as Load Balancing, relies on sophisticated software or hardware called Load Balancers. Every load balancer follows a set of rules to find the best candidate from a group of nodes/instances for a client’s request. This essay explores two broad categories of load-balancing algorithms: static and dynamic.
Static Load Balancing Algorithms
In scenarios where the system's state is stable and homogeneous, and where traffic load remains relatively constant, static load-balancing algorithms are highly efficient. These algorithms establish load-balancing rules at compile time and operate independently of the system's state at runtime.
1) Round Robin
Round-robin distributes client requests to a group of servers in a cyclical manner. Rather than relying on specialized software or hardware, it uses an authoritative name server, such as the Domain Name System (DNS), which returns a different IP address from the list for each client request. This approach assumes that all servers have the same performance.
2) Weighted Round Robin
This algorithm is a variation of round-robin, where each server is assigned a weight based on its priority or capacity. Servers with higher weights receive more requests compared to those with lower weights. This approach allows servers with greater capacity or better performance to handle more requests than lower-performing servers. The requests are still assigned cyclically.
3) IP Hash
This algorithm uses a hash function to convert the client's IP address into a number that maps to a specific server. The hash function considers factors such as the number of servers, their availability, and their weights when generating the number.
Dynamic Load Balancing Algorithms
In scenarios where the system's state is heterogeneous and traffic patterns vary frequently, dynamic load-balancing algorithms are highly effective. These algorithms establish load-balancing rules based on the system's state both before and during runtime. They adjust the load distribution in real time by continuously examining the current state of the system.
1) Least Connection
In this approach, the load balancer identifies the server with the fewest open connections at the time of the client's request and forwards the request to that server. Similar to round-robin, this method assumes that all servers have the same performance capabilities.
2) Weighted Least Connection
This algorithm is a variation of the least connection algorithm, where each server is assigned a weight based on its priority or capacity. Servers with higher weights receive more requests compared to those with lower weights. For example, if all servers have the same number of connections, the server with the higher weight will be chosen. This approach enables servers with greater capacity or better performance to handle more requests than lower-performing servers.
3) Least Response Time
This approach uses each server's response time and active connection count to determine the fastest server. This ensures that all client requests are handled with the lowest possible delay.
4) Resource Based
Each server in the pool runs software known as an agent, which collects real-time information about the server's load based on its available CPU and memory. The agent shares this information with the load balancer, which then uses it to identify the best server with sufficient resources to handle a client request and forwards the request accordingly.