Load balancing is the process of distributing the traffic to multiple server in order to reduce the load on one server. It also increase the availability of your system, if one of the server goes down, clients will not be affected as the traffic is routed by load balancer.
Another important reason for using load balancer is to increase the scalability of your system. Let’s say you have designed a system it serve the traffic of 1000 QPS (Queries Per Second) with current hardware.
Now there are two ways you can scale your system
- Vertical Scaling – By improving your system hardware, it has limitations because beyond certain point you cant scale.
- Horizontal Scaling – By add more similar server to increase your throughput, in this cases you need to distribute your request to multiple servers, in which load balancers are helpful
Load Balancer Algorithms
There are plenty of algorithms are available today. I have mentioned few of them
In this method it directs the request to server which has least connection. It is useful when there is persistent connections are used, it distributes connections evenly.
Least Response Time
In this method it directs the traffic to server which has low response time. When the server is loaded too much, it will respond slowly, by using this algorithm we can send the request to better responding server to avoid server being over loaded, so balancing of load will happen over the time.
It is simplest method of all, it directs the request to server in round-robin fashion. It is helpful if you have identical server group in terms hardware.
It is similar to round-robin but there is weight is involved here. It is useful when you have heterogenous mixture of servers being used. Let’s say you two group of servers, one group(G1) which can serve 2X of the load of other group (G2) due to better hardware, in that case you can use this weight to send more requests to group G1 servers.
It uses source IP hash to route the request to Server. It is helpful when you have a sticky connection with the server.
Benefits of Load Balancer
- Increases the availability and scalability of a system
- Protects the server being the overloaded, by distributing the requests evenly
- Software Load balancers can do predictive analysis before the contention happens and able to take actions
- Software Load balancers can also be able to provide rate limiting feature
- Load balancing can be happen between L4-L7 OSI layers, each layer has its own benefits
- L4 – Can route the traffic based on the IP and port
- L7 – Can use the HTTP Header such as sessionId, userId and other information in request to direct the traffic to relevant servers
Reverse Proxy Vs Load Balancer
Reverse Proxy accepts the request from users and forward it to the server. It is the public face of your website, it makes sense to deploy reverse proxy even with one web server, but if you don’t have multiple servers, then load balancers are not going to help you a lot.
- Security – Since it is in front of the website, it masks the servers behind the proxy, so exploits and vulnerability is less
- DDOS – It can also play a role in protecting website from DDOS attacks by the rejecting the connections based on IP or blacklisting and no of connections
- SSL – It can do the SSL encryption and decryption for your website
- Compression – It is possible to compress the data and send it to clients
- Caching – Static content can be cached in reverse proxy itself
- Works only with web servers for handling HTTP/HTTPS traffic. But load balancer can serve any traffic such as Database, Application etc.,