DEV Community

Cover image for Load Balancer - 101
Uzair
Uzair

Posted on

Load Balancer - 101

Okay, Now What's a Load-Balancer?? ๐Ÿ™„

As the name suggest it has something to do with balancing the load, but here in backend system design architecture load generally refers to traffic or amount of incoming requests ๐Ÿ“ถ

So yeah, going by the name it manages traffic to servers so that they may not get overloaded by traffic โš–๏ธ

Imagine Facebook or Instagram, such a huge giant they are aren't they now let us assume all of their traffic and backend process is handled by a single server, not even a super computer can do such wonders ๐Ÿ˜‚
So definitely they must have multiple server spread across the globe ๐ŸŒ now how to decide which request goes to which server ๐Ÿคจ And how to make sure that a particular server is not stormed by lot of requests, while other servers sits idle and smile at each other ๐Ÿ˜†

This is the time when load balancer comes for rescue๐Ÿฆธ
It does the job of deciding which request goes to which server, and thus solving the above mentioned problems โœ”๏ธ

Let's see this in detail ๐Ÿ•ต๏ธ

When we talk about a server its not a single server, actually there are lot of server sitting behind load balancer, Load Balancer is a device (hardware) or may be a software (ex: Ngnix)
But when software is avalible then who will spend money on hardware ๐Ÿ’ฐ
So a software load balancer is widely used ๐ŸŽข

Untitled_Diagram.drawio

In the above image servers could be web server or application server, and server may further communicate with database etc etc

A load balancer acts as a traffic cop ๐Ÿ‘ฎ sitting in front of servers and routing clients across all servers with equal load ๐Ÿ“ถ This ensures that no single server have too much load which can lead to degradation of service or degradation of server's health ๐Ÿ‘จโ€โš•๏ธ

  • A load balancer divides load uniformly across all servers, in the above scenario every server would receive 25% of the load as there are 4 servers. โ™พ๏ธ
  • When client resolves URL into an IP address through DNS Lookup it basically gives the Load Balancer's IP not server's IP.
  • Requests are measured in QPS (Query per second)
  • This Adds extra security ๐Ÿ” as servers are not publicly exposed and remains in a private network. If a client tries to directly access servers it cant do so without going through load balancer.

Load balancing Algorithms ๐Ÿ–

The algorithm in a particular load balancer decide which server will be receiving the next incoming request

The Algorithms are as follow ๐Ÿ‘‡

  1. Round Robin
  2. weighted Round Robin
  3. Least Connections
  4. Least Response Time
  5. IP Hash

Lets take a look at all of them in detail ๐Ÿ•ต๏ธโ€โ™‚๏ธ

Round Robin โญ•

Requests are sequentially sent to 1st, 2nd 3rd,.... till last server and then next request is sent back to the first server and this process is continues in a circular manner ๐Ÿ”

Server_Farm

In the above case requests will be sent in this manner:

  1. S1
  2. S2
  3. S3
  4. S4
  5. S1
  6. S2
  7. S3

And So on...

Weighted Round Robin ๐Ÿ‹๏ธ

One Drawback in Round Robin is that it assume that all the servers have same computational power and there's no way we can assign more requests to the one with more power and less request to the one with less power. ๐Ÿšฆ

To counter this we assign weights to each server and based on that weight requests are redirected ๐Ÿ’ช

Server_Farm.drawio_(3)

Least Connections Algo โ†”๏ธ

Request are sent to server with least number of open connections โš–๏ธ

Server_Farm.drawio_(4)

In the above representation at this particular instance S3 has the least number of connections so the next incoming request will be received by server S3 according to Least Connection Algo

Least Response Time Algo ๐Ÿ”ญ

Request are sent to the server with fewest active connection and lowest average response time โ†”๏ธ

Server_Farm.drawio_(11)

Here the avg response time is calculated by multiplying number of connections and response time

Server_Farm.drawio_(12)

Server_Farm.drawio_(13)

In the first case S2 and S3 both have same avg response time so in that case either of S2 or S3 will receive the request.

But in case 2 S3 has the least avg response time hence it will receive the request

IP Hashing Algo #๏ธโƒฃ

Here IP of the client is used to determine the server to which request has to be redirected ๐Ÿฅข

An high level explanation would be:

Some logic is written in a function and an IP address is passed to that function, based on that logic which resides inside the function it return the server to which request has to be redirected for that particular IP address ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ”ง

Server_Farm.drawio_(15)

Server_Farm.drawio_(19)

Placement of Load Balancers ๐Ÿ›ค๏ธ

Based on certain criteria Load balancer can be placed at Layer 4 or Layer 7 in the OSI Model of a product ๐Ÿง˜โ€โ™‚๏ธ

An OSI Model consist of 7 layers which are ๐Ÿ‘‡

OSI_Model

Its Similar to layers in a Cake ๐Ÿฅฎ ๐Ÿ˜

Layer 4 Load Balancers:

The Layer 4 Load balancer are placed on the 4th Layer of OSI model i.e Transport Layer

They don't have access to data and decision to redirect request to a server is purely done on the basis on data.

Layer 7 Load Balancers:

Layer 7 Load Balancer are placed on Application layer and have access to incoming as well outgoing request and data and decision is made up on its basis, Layer 7 LB is very very much complex :/

At Last an high level architecture of load balancer can be depicted as:

Load balassssncer

Happy Load Balancing ๐Ÿš€

Top comments (6)

Collapse
 
vaguilera profile image
Victor Aguilera

Oh please, stop using emojis within the text. This trend is awful.

Collapse
 
uzaxirr profile image
Uzair

Ohh is it??
I belive it makes content more attractive and readers don't get bored!!

Collapse
 
vaguilera profile image
Victor Aguilera

Nice article in any case. Sorry if I sounded rude :)

Thread Thread
 
uzaxirr profile image
Uzair

No worries!

Collapse
 
aravin profile image
Aravind A

Simple and neat explanation of Load Balancer โ˜บ๏ธ

Collapse
 
uzaxirr profile image
Uzair

Thanks Aravind!!!