top of page
Writer's pictureRohit Modi

System Design : Load Balancing And Consistent Hashing - Part 1

Updated: Jul 8, 2022

Hi Guys, a warm welcome to tech-maze, today's article is on load balancing which is very important module to consider when we design a system, there is no specific load balancing algorithms or technique which can work for all types of project, this can vary based on the requirements and need.


In general we go for one of the following algorithms.


1. Round Robin Algorithm (here request picks up and forward to server, it doesn't check if any server is overloaded, for small project where traffic is low, this can work without any issue)


2. Weightage Round Robin (In this type of approach, server with more power gets the more request, lets say out of 4 server, server A has 64 GB RAM and other servers serve B, server C, server D has 32 GB RAM, hence more request will be forwarded by load balancer to server A)


3. Least Connections (Load balancer picks the server which has least request to process and forward the received request)


4. Least Time (Load balancer picks the server which takes least time to serve a request)


5. IpHash (Load balancer identify the server based on the hash value calculated by hashing the IP address of the client )


6. composite algorithms (mix of any of two)


Lets have a look how load balancing works with the following diagram




In the above diagram, we can see that request comes to Load Balancer, and it distributes load among the servers.

here we have taken 3 servers.


Load balancer takes following steps to consider which server should process any request, here in this article we will consider hash function to distribute the load


step 1 :


Load balancer receives the request with request id


step 2 :


it calculates the hash value of the received request id by using hash function, provided by hashing algorithms such MD5, SHA etc.


step 3 :


In this step, it calculates the hash value of the request id, then modulates the obtained hash value with number of server and then redirect the request to the appropriate server as below


req1 = 98798

hash(req1) = 9

server = 9%3 = 0


req2 = 45634

hash(req2) = 10

server = 10%3 = 1


req3 = 83793

hash(req3) = 17

server = 17%3 = 2


req4 = 91873

hash(req4) = 22

server = 22%3 = 1


lets take one of the above example, here we have 3 servers and obtained hash value is 17 then the result of 17%3 will be 2, hence request will go to server 2 (S2)


since request id is never a random value and based on the selection process of load balancer, same request will always go to same server hence using cache memory or implementing a caching tool in each server is a great idea to enhance the application performance by decreasing the response time.

So that if same request comes to the server, it can be served by the cache memory.


So far so good, but there is a problem with this kind of setup, what if number of user increases a lot and we have to introduced few new server or number of user decreases and we have to remove few servers.


Lets take the first point where number of users has increases and we have to add one more server to serve all incoming requests. The algorithm implemented for the load balancer will take care of this without any issue, it seems quite perfect setup and we don't have any problem in the redirection of the request.

But there is a catch/issue in this, lets have a look in following diagram and understand the underlying problem



After adding another server, lets see which server is going to serve the existing request now.


req1 = 98798

hash(req1) = 9

server = 9%4 = 1


req2 = 45634

hash(req2) = 10

server = 10%4 = 2


req3 = 83793

hash(req3) = 17

server = 17%4 = 1


req4 = 91873

hash(req4) = 22

server = 22%4 = 2


here we can see request which were served by server 0 (S0) , server 1 (S1) and server 2 (S2) now serving by server 1 (S1) and server 2 (S2), and when we upgrade the distributed system by either adding server or removing server, it actually dumped all the existing cache memory data, because it is irrelevant and no longer associated to the same server, which is really not acceptable in highly available application and organizations avoid going down in this route.

So what can be the solution, because this is the approach we have been following since long time.

The solution is Consistent hashing, by implementing consistent hashing we can overcome from great impact on the application which has introduced by adding/removing of the server, I will publish article on consistent hashing shortly, stay tune for that.


Most frequently used Load balancer are


Nginx.

Azure Traffic Manager.

AWS Elastic Load Balancing.

HAProxy.


That's all folks in this article, keep visiting for more articles and don't miss article on Consistent Hashing, which is going to publish in few days.

A Big thank you for checking out my article, this really encourage me to come up with more topics.



49 views0 comments

Recent Posts

See All

Yorumlar


Spring Boot
bottom of page