6 Common load-balancing algorithms
The diagram below shows 6 common load-balancing algorithms.
- Static Algorithms
- Round robin
The client requests are sent to different service instances in sequential order. The services are usually required to be stateless.
- Sticky round-robin
This is an improvement of the round-robin algorithm. If Alice’s first request goes to service A, the following requests go to service A as well.
- Weighted round-robin
The admin can specify the weight for each service. The ones with a higher weight handle more requests than others.
This algorithm applies a hash function on the incoming requests’ IP or URL. The requests are routed to relevant instances based on the hash function result.
- Round robin
- Dynamic Algorithms
- Least connections
A new request is sent to the service instance with the least concurrent connections.
- Least response time
A new request is sent to the service instance with the fastest response time.
- Least connections
What is a load balancing algorithm?
A load balancer is a software or hardware device that keeps any one server from becoming overloaded. A load balancing algorithm is the logic that a load balancer uses to distribute network traffic between servers (an algorithm is a set of predefined rules).
There are two primary approaches to load balancing. Dynamic load balancing uses algorithms that take into account the current state of each server and distribute traffic accordingly. Static load balancing distributes traffic without making these adjustments. Some static algorithms send an equal amount of traffic to each server in a group, either in a specified order or at random.
What are the different types of load balancing algorithms?
Dynamic load balancing algorithms
- Least connection: Checks which servers have the fewest connections open at the time and sends traffic to those servers. This assumes all connections require roughly equal processing power.
- Weighted least connection: Gives administrators the ability to assign different weights to each server, assuming that some servers can handle more connections than others.
- Weighted response time: Averages the response time of each server, and combines that with the number of connections each server has open to determine where to send traffic. By sending traffic to the servers with the quickest response time, the algorithm ensures faster service for users.
- Resource-based: Distributes load based on what resources each server has available at the time. Specialized software (called an “agent”) running on each server measures that server’s available CPU and memory, and the load balancer queries the agent before distributing traffic to that server.
Static load balancing algorithms
- Round robin: Round robin load balancing distributes traffic to a list of servers in rotation using the Domain Name System (DNS). An authoritative nameserver will have a list of different A records for a domain and provides a different one in response to each DNS query.
- Weighted round robin: Allows an administrator to assign different weights to each server. Servers deemed able to handle more traffic will receive slightly more. Weighting can be configured within DNS records.
- IP hash: Combines incoming traffic’s source and destination IP addresses and uses a mathematical function to convert it into a hash. Based on the hash, the connection is assigned to a specific server.
How does Cloudflare Load Balancing work?
Cloudflare Load Balancing uses health checks to steer traffic toward healthy servers. It also enables administrators to customize where regional traffic is handled in order to reduce the amount of distance that traffic has to travel. This approach is known as global server load balancing (GSLB).