As your user base grows, your need for computing power also goes up. When you started with only a few users, you probably had only one web server. However, with the increased user base, you will need additional servers. As you put additional servers, you need to have some algorithm to decide which server should get the new traffic. Load Balancer helps you with this need.
What is a load balancer?
An entity that distributes the incoming traffic to backend servers efficiently is called as a load balancer. The load balancer could be a software solution (e.g. Nginx or HAProxy) or hardware solution (F5 load balancer or AWS Elastic Load Balancer).
In simple words, load balancer acts as a traffic cop, which sits in front of several web servers and routes client requests to one of the web servers. While doing this, it needs to ensure speed and capacity utilization. These two parameters drive the decision – where to route the next request!
- Number of servers are available in the server pool
- How to reach each of those servers (IP and/or port)
- Load on each of those servers are
These data points are used by various algorithms listed below.
Load Balancing Algorithms
- Round Robin – requests are routed to all the servers sequentially, irrespective of the load on those servers.
- Weighted Round Robin – each server in the pool is given weighting. Server with higher weighting gets more requests.
- Least connections – new request is sent to the server which has the least number of open connections
- Adaptive Load Balancing – load balancer detects the load on each server (e.g. via an agent deployed on the server) and routes traffic to the least loaded server.
- Source IP Hash – A token is generated using source and destination IP addresses and is used to decide where to route that request. As a result, all requests from the same source can be sent to the same web server in the pool and hence is useful where session needs to be maintained.
Advantages of Load Balancer
- Distribution of the traffic as per the configured algorithm
- High availability – if any server goes down, traffic is routed to other servers.
- Scalability – you can add or remove servers as per the business needs.
Some load balancers also come with additional functionality such as SSL handling, session stickiness, which you can leverage to improve your application performance. Hence it is important to compare and identify the most suitable load balancer for your needs.