Skip to main content

Load Balancing



What is Load-Balancing ?
Distributing network traffic across multiple servers  effectively  is called Load-Balancing.To meet high volumes,it is generally requires adding more servers. Load-Balancer route the client requests to the servers in an optimized way and takes care that no server is overworked. If new server is added to the group, It will start sending requests to this new server.

Load-Balancing Algorithms:
Why algorithms came into the picture ? Actually Load-Balancer selects the server based on two factors :
a) periodically pinging server to check its availability.
b)  defined algorithms - based on which load-balancer selects the server.

Algo 1: Least Connection (Default algorithm)
Least connection is the default algorithm to select server. Serve which has least number of active transactions is picked by Load-Balancer to handle request.Load-Balancer maintains records of transactions for each server.
For example: 
See the below figure, Load-Balancer will select server 2.

XL
 Now discuss this in detail: Lets say all the servers has same capacity and still some of the servers are overloaded as there may be the situation that the client stayed in those servers for longer duration and connected to other severs for shorter duration.Active connections on the server where client stayed longer will pile up and hence based on Least connection algorithm Load Balancer will route the request in the server with least active connections,

Algo 2: Weighted Least Connections :
Now lets consider above Least Connection algorithm with different server capacities (CPU/RAM etc.). You would definitely want to allocate more requests to the higher capacity server than the lower capacity servers. Weighted Least Connection is the solution. 
Let's say there are 2 servers, server 1 and server 2 and server 2 has high configuration.With this algorithm Load balancer will allocate more requests to server 2 to utilize it better.Like Least Connection this also allocate request to the server with least number of active connections but the higher configuration server will handle more requests based on the wight proportion defined during the Load Balancer setup. Weight proportion is calculated by the server capacity. Server 1 : Capacity X, Server 2 : Capacity : 10X the weight for each server would be 1:10 .
 There might be the scenario if you don't want to overload any of the server for some reason, you may use this algorithm to give extra weight to another servers.


Algo 3: Round Robin
Round Robin is vastly used and simple algorithm. Load-Balancer distributes requests in the cyclic order irrespective of server inefficiencies.

L

 Both the Servers are ready to take request, suppose request comes and load-balancer routes to Server 1 then if 2nd request comes it will be routed to Server 2. 3rd and 4th will be routed to Server 1 and 2 respectively in a cyclic order. Even if one of the server has stronger configuration i.e. RAM, CPU etc. As per Round Robin algorithm Load-Balancer will follow the cyclic order. 

Algo 4: Weighted Round Robin:
Like Round Robin this is also cyclic but the higher configuration server will handle more requests.Rest it is same as Weighted Least connection algorithm i.e. weight would be defined during Load Balancer setup and high weight server will handle more requests.
i.e. if weight proportion to server 1 and server to is 1:10. first 10 requests will go to server and 11th request will go to server 1 and next 10 request will go to server 2 and so on.

Algo 5: IP Hash:

 This algorithm generates a hash key using client and server IP addresses which means this key would be assigned to client for subsequent requests which assure that the client is routed to the same server that it was using earlier.This algorithm can be used to achieve Session Persistence or Sticky Session.

Session Persistence:
In the multiple server environment a user might experience in loosing cart items during navigation. Persistence session or sticky session is the culprit. As you know Http is a stateless protocol which means in subsequent request it doesn't maintain any information about the user so to identify server uses client's ip or cookie to track users session. Sticky session is to make sure all the requests goes to the same server which has its user's session tracking information.

Layer 4 & Layer 7 Load Balancing:
In Layer 4 Load Balancing , Load balancer Decides the server on which it will redirect the requests on  the basis of the IP addresses of the origin and destination servers (Network Layer : Layer 3) and the TCP port number of the applications (Transport Layer : Layer 4).
On the other hand Layer 7 Load Balancing decides routing on the basis of  OSI Layers 5, 6, and 7 which together makes Http.


To get overview of  OSI Model, Please read my post .

Find it here also C-Sharp-Corner







Comments

Popular posts from this blog

Abstract Factory Design Pattern

Abstract Factory : return families of related or dependent objects without specifying their concrete classes. AbstractFactory- IVehicle ConcreteFactory - Maruti, Honda AbstractProduct- IDiesel, IPetrol Product- DezirePetrol, ZenDiesel, AmazeDiesel, CityPetrol Client- This is a class which uses AbstractFactory and AbstractProduct interfaces to create a family of related objects Implementation is pretty much straight forward: Since now you have good understanding of Factory, there is nothing much to explain. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AbstractFactory {     class Program     {         static void Main(string[] args)         {             IVehicle marutiVehicle = new Maruti();             CarClient marutiClient = new CarClient(marutiVehicle); ...

Database Factory Design Pattern C#

Many developers has confusion on how to write database code in application. I will discuss several approaches here and try to provide explanation on each approach. Approach 1 : Easiest approach which is very straight forward which will work absolutely fine. The issues we will get to manage the project in terms of software principles. Like if you going to change Database provider, you would need to change using statements and other issue like mixing Database code with business logic etc. I will not go in detail here, I am assuming you already have an understanding of design principles. I am just exploring different approaches to achieve the database connectivity. using System.Collections.Generic; using System.Data.SqlClient; namespace DatabaseFactory {     class Program     {         static void Main(string[] args)         {             var employees = SomeDabOperation();   ...

Dependency Injection and Inversion Of Control C# - Par 2 (Implementation)

Before going through this post, I suggest you to go through my previous post on basic concepts in Dependency injection and Inversion of control. Demo on product. This demo is based on a traditional implementation approach: We have a Product class and it has a method which takes the discount and based on some business logic it applies the discount on the product. Product service will interact with database using ProductRepository. If you will notice ProductService has dependency on 2 concrete classes i.e. Discount and ProductRepository new Discount(); new ProductRepository(); We can hence say that ProductService is tightly coupled because of these 2 dependencies. We may require to use different discount based on several factors such as Product type, Some Festival or any other factor. Based on different discounts we may need to change ProductService class also in future you may change the database from SQL to Oracle, Again it may require ProductService changes which is viol...