A Load Balancer (LB) allows traffic distribution among many available web servers. The client connects to a public IP, makes the request and the Load Balancer will redirect it to an available web server using a private IP.
HORIZONTAL SCALABILITY
Advantage
- If the traffic of the web server suddenly increases, we can dynamically add new web servers, and the Load Balancer will distribute the request across all nodes.
Tradeoff: Increased complexity
- Horizontal Scalability demands the servers to be stateless: The data needs to be extracted to an external storage, so everything can be shared across all nodes
SINGLE POINT OF FAILURES
- If one of the available webservers goes down, the Load Balancer will route all the new traffic to another available node. This approach prevents your application from going down.
Note: Load balancer became a single point of failure itself. To avoid this problem you can also setup multiple load balancers.
TRAFFIC ROUTING STRATEGIES
There are many algorithms for routing traffic with Load Balancers. The two primary approaches are Dynamic Load Balancing and Static Load Balancing.
DYNAMIC LOAD BALANCING
Load Balancer examines the current state of servers and then distributes traffic.
Algorithms
- Least connection: Load Balancer chooses the server with the least number of connections. The assumption is that all connections will require an approximate amount of processing power.
- Use-case: Unpredictable incoming request connection times with a set of servers whose members have similar processing capabilities/resources
- Weighted least connection: The administrator can assign different weights to servers. This approach states that different servers will be able to handle more connections than others.
- Weighted response time: Load Balancer calculates the average response time from each server and takes into consideration the number of connections each server has in the current moment. Then it takes the requests and sends them to servers with faster response times.
- Resource-based: Load Balancer distributes the load based on available resources (CPU/Memory)
- Use-case: Applications where the workload is varied and server health is determined by resources consumption
STATIC LOAD BALANCING
Load Balancer follows fixed rules independent of the current server state.
Algorithms
- Round robin: Distributes traffic based in a rotation way using DNS
- Use-case: Predictable incoming request connection times with a set of servers whose members have similar processing capabilities/resources
- Weighted round robin: Administrator can assign different weights to servers. Servers with higher weights receive more traffic
- IP hash: Mathematical function to hash the source IP address. Depending on the hash result, the request will be routed to different servers
- Use-case: When it is important that a client always return to the same server in all connections