AfterAcademy Tech
•
29 Apr 2020

Welcome to AfterAcademy!
In this blog, we will learn the concept of Load Balancing in System Design. This is a famous question that is asked in almost every System Design interview. So, we are going to discuss the following in this blog:
So, let's get started with the "Why Load Balancing?" question.
If you are more found of videos than text, you can watch our Load Balancing video:
Suppose a few days back, you made some website, let's say AfterAcademy to provide a one-stop learning solution for everyone i.e. Students, Professionals, learning enthusiasts, etc. Initially, the website was new, so you bought one server than can handle let's say 1000 users at a time.
But when you started putting more good content on the website, then the number of users started growing a lot and now your server is unable to handle the request of so many users. What will you do?
Since your user base is growing, you can plan to buy more servers. So, let's say to give your users a better experience, you are having a total of N servers. So, whenever a user is requesting something from your website, you need to redirect the request to one of your servers and in return, the server will provide some response to the user. The request sent to the server should be such that no server is overloaded and the equal number of requests should be sent to each server. So, here comes the role of Load Balancing.
Load Balancing is a process that is used to uniformly route the request of users/clients to the different servers that are available for use i.e. to the servers that are currently in working condition. In simpler words, load balancing is a process of balancing the load on different servers.
For example, if you are having 10 servers and 1000 users, then in an ideal situation each server should handle the request of 100 users.
The process of Load Balancing is done with the help of Load Balancers. The following image illustrates the Load Balancing process:

To avoid the case of failure of the Load Balancer, we can add one backup Load Balancer. At a time, only one Load Balancer will be active and the other Load Balancer will keep on watching the status of active Load Balancer. So, in case of failure, the other Load Balancer will take over the charge and do the Load Balancing thing.

To make the most of Load Balancing, we can use Load Balancers in 3 places:
This is how Load Balancing is performed. Apart from the distribution of loads, there are various other benefits of Load Balancing. Let's find.
Some of the benefits of Load Balancing are:
Now, let's look at some of the algorithms that are used for Load Balancing.
From the available servers, the Load Balancer will route the request to only those servers which are responding correctly, or in other words, the request will be sent to those servers that are "healthy".
Out of those working servers, the Load Balancers uses various algorithms to route the request. Some of these algorithms are:
Out of these algorithms, Hashing is the most widely used algorithm. Let's learn about it.
Hashing is one of the common methods used in Load Balancing. Here, we have something known as requestId that is uniquely and randomly generated. Each request by the client is uniquely identified by this requestId.
So, along with the request, the requestId will also be sent. With the help of Load Balancer, this requestId will be hashed with a proper Hash Function, and to get the server where the request will be directed, we will find the modulo of the result of the Hash Function with "N" i.e. the total number of active servers available.
If you are not familiar with the Hash Function and Hashing, then learn from here.
Note: The Hash Function should be such that the number generated by it should be uniformly distributed over all servers.
The following is an example of Load Balancing using Hashing:

So, for example, if the requestId is 154 and there are 5 servers available. Let's say the Hash Function will give the result as 23. Then the following calculations will be done:
total number of servers = 5
hashFunction(requestId) i.e. hashFunction(154) = 23
so, the assigned server will be: 23 % 5 = 3
therefore, the 3rd server will be assigned
So, by performing the above method the load is uniformly divided i.e. if the total number of requests is R and the total number of active server is N then load on each server is going to be R/N .
Now, let's see if there is some problem with this hashing technique or not.
So we have seen how hashing can be used for the purpose of Load Balancing. Now, let's take an example where we have 100 requestId and 5 servers. So,
requestId 1 to 20requestId 21 to 40requestId 41 to 60requestId 61 to 80requestId 80 to 100The following pie-chart describes the same:

Note: For better performance, the servers keep the data of the users as cache. For example, the server 1 will keep the important or frequently used data ofrequestId1 torequested20.
Now, due to some reasons, the server 5 is down. So, we need to distribute the request from requestId 80 to 100 to all the other 4 servers and now each server will be handling 25 requests each.
So, the new pie-chart will look something like below:

Note: The dotted lines are representing the old distribution and block lines are representing the new distribution.
If you see in the above image, then you will find that there are a lot of operations being performed for migrating the requestId:
requestIds. Now, it is serving 25. So, it is gaining 5 requestIds form server 2 i.e. the data of 5 users is migrated to server 1 from server 2.requestIds. Now, it is serving 25. So, it is loosing 5 requestIds (given to server 1) and gaining 10 requestIds form server 3 i.e. the data of 10 users is migrated to server 2 from server 3.requestIds. Now, it is serving 25. So, it is loosing 10 requestIds (given to server 2) and gaining 15 requestIds form server 4 i.e. the data of 15 users is migrated to server 3 from server 4.requestIds. Now, it is serving 25. So, it is loosing 15 requestIds (given to server 2) and gaining 20 requestIds form server 5 i.e. the data of 20 users is migrated to server 4 from server 5.So, if you see the above process, then you will find that there is a lot of data migration operations being performed here i.e. giving and taking of requestIds. This will lead to downtime until all the migrations are not performed and we don't need this situation. Some improvements must be done to this normal Hashing process and this is done with the help of Consistent Hashing.
We have seen that there is some problem with the normal Hashing approach and this can be removed by using Consistent Hashing.
So up next, you can learn about Consistent Hashing from AfterAcademy video.
Also, you can find the whole playlist of System Design from here.
Know about other Backend technologies by learning from our open-source projects.
Keep Learning 🙂
Team AfterAcademy!
AfterAcademy Tech
In this blog, we will learn what is an FTP protocol, how does it works, what are various modes in which it works, what are the advantages and disadvantages of using it.

AfterAcademy Tech
In this blog, we will mainly learn about the OSI model and its working in detail. We'll focus on the features and working of each layer of the OSI model.

AfterAcademy Tech
In this blog, we will learn what is DHCP, what are various entities related to DHCP and how does it work. We will also discuss how IP configurations are dynamically done.

AfterAcademy Tech
In this blog, we will mainly learn about the TCP/IP model and its working in detail. We'll focus on the features and working of each layer of the TCP/IP model.
