What Is Load Balancing and What Are the Usages of Load Balancing
If you are working for bigger firms and if you want to handle lots of traffic you would have heard about the term Load balancers which people use very often. Let’s see what is Load Balancing and why do we prefer Load Balancing.
What is Load Balancing?
Let’s take the example of Amazon. Amazon might be deploying lots of servers in the back-end for the UI part. Let’s consider these are the different servers that are deploying in the Amazon infrastructure which are running the same code as the Amazon UI. Now does the request get to each of these servers? Let’s say I am hitting the amazon.com website and How does Amazon know it needs to redirect to the particular instance? How a new request needs to go to a different instance. That is well Load Balancers are useful. Load Balancers do the routing (Example: Request 1 goes to the particular server which is marked as Green, the request 2 goes to a different server which is marked as yellow and the request 3goes to the red server, and request 4 goes to the black server). All these requests may be from a different user or maybe from the same user. So like this we can balance the load by distributing the request for several servers.
What are the usages of Load Balancing
- Distribute incoming traffic to the network by efficiently distributing across multiple servers.
- Reliability and High Availability are maintained by redirecting requests only to the servers which are available in the network. If there are servers that are unavailable or offline those servers will be removed from the load balancing setup. Internally load balancers are maintaining the health check of all the servers. And the request will be redirected to the available servers only.
- Ease of use in adding and removing servers in the network as per demand. Let’s say Amazon needs to scale up its servers for a special function. They know that the number of requests which is going to come for the servers is more. Based on that they can scale up or scale down based on the demand. They just need a simple configuration change.
Load Balancing Algorithms
There are different algorithms which are used in Load balancers in order to redirect requests from different servers. We can see 3 different common algorithms which companies prefer for setting up a Load Balancing.
- Round Robin(Sequential request distribution) — The requests are redirected to different servers here. Let’s say there are 3 servers in the network and the first request goes to the first server, the second goes to the second server and the third goes to the third this sequence is followed for all requests. Every server gets a new request every time.
- Lest Connections(Request sent to the least used server in the network) — The request will be sent to the server which has the least requests or which is processing the least number of resources. In order to do this, the Load Balancer needs to know which server having the least number of resources so that needs to do some additional computing because to know the count of resources used. This might get some metadata from all the servers and get some time to compute.
- IP Hashing(Request sent to the server based on Client IP) — This is useful when some network or some specific servers need to prevent preference over the other. Let’s say the client sends a request and the client request needs to be delicately going to a particular set of servers. That is an IP hashing is used. So the redirection can be done based on the client IP address and only specific servers specific to that client IP address will be redirected to that particular network.
You can redirect specific requests for the specific servers on IP Hashing and based on that we can improve our performance on the servers.
Session Persistence in Load Balancer
For example, we are using the Amazon cart and we have added lots of information into the cart. And this particular cart information is stored in the session(client-side browser). The browser usually stored all the cache information and stores it. And every time a new Request has made browser will give the same session ID. So what happens generally is if the requests are redirected to different servers the session ID tends to be lost. Because the first session which was generated by server1 might not be visible for server2. So the session information is lost and the cart will be disappeared.
This is a general use-case we will have to handle that in the application end. We can either use a common cache(Redis cache to store our session information) or we need to allow the Load Balancer to go to the same instance every time. In general, these Load Balancers have this concept of Sticky Sessions. Because they know that particular session ID came for the first time it should be redirected to server1 and for the same request for the second time still, I need to redirect to Server1 and I will not consider the traffic and other factors.
Most of the Load Balancers have this concept of Session Persistence because they know that they need to redirect to the same instance if the request is sent from the same client. In general, let’s say you need to handle that then you can go for a caching mechanism where you can use this session ID to be stored in a different cache or stored in a separate database and route the request for the different servers.
The only difference between these approaches is the read operations will be more and the performance will be high in Caching mechanism.
So in this tutorial, we discussed What is Load Balancing, Usages of Load Balancers, Load Balancing Algorithms, and Session Persistence. I hope you all get a basic understand of Load balancers and you might be able to use it in your enterprise application implementation.