Lets say we have an application and it is used by 10 number of users. After a while, lets assume there is an increase in number of users, may be a 10X. Now instead of having a 4GB machine you are just scaling it up to 8GB. This scaling is called as Vertical Scaling. Even then it is not enough, what would you do now?
One option is to do a Horizontal scaling, We can have multiple instances. Lets have three machines (M1, M2 and M3). But how does this whole system works, so that we can handle many number of users?
The Need for Load Balancer
Now lets go back to basics. When we have one single machine (M1) How does an User reach our Machine?
Using the URL, the user could reach the machine.
In the machine, we would have set to map that URL to a IP. So whenever the user tries to hit that URL, it gets resolved into the mapped IP. This way, the user is able to reach the machine. The process of resolving the URL to IP is done using DNS (Domain Name System/Server). The URL is called as Domain name. It is like a label. So whenever we want, we can even change the IP that gets mapped to that label. But the label stays the same, so that even if we change the IP, the user could access our machine using the same URL. One IP can be mapped to one machine or URL. (It can be mapped to multiple IPs. lets take that later. Now as a basic, consider this.)
User --> URL <=> IP
Now our problem is, we increased the machine capacity to 8GB. But we couldn't handle the traffic(Users hitting our URL). So, then we increased our instances(M1, M2 and M3). Now M1 is mapped with IP1, M2 with IP2 and M3 with IP3. Now the problem is even though we have multiple machines, the URL can be mapped to only one IP, then how does the User can reach any other machines?
Now what we can do? We have scaled up, but how an we make it available to the user. Lets bring someone called Router as a middleman between our User and our Machines. SO lets map the URL to the IP of that Router. The user hits the router and it redirects that request to our machines. All the IPs of those machines would be configured at that router, so that it could route the requests. This router is also again a Machine. So this router or the machine or even this mechanism is called as the Load Balancer.
One more mechanism this handles here is called as Reverse Proxy.
What is mean by Proxy? One gets replaced with other. Reverse Proxy means, users hit the Router machine. They doesn't know about the multiple machines(M1, M2, M3) behind this router machine. This process is called as Reverse Proxy.
Similar to this we have one another concept called as Forward Proxy.
Lets assume you are working in a office. You and your colleagues are connected to your office network. You wanted to access (google.com). From each of your machines, you are going to send a request. All these requests, goes into a common router. There would be only one IP allocated for your office and that would be divided as subnets for all of your machines. All your machines would have only private IPs. But to interact with the internet, we need a public IP. That public IP gets mapped with that common router. So when you hit the google.com, google knows that it came from the public IP which is the common router's IP. But the actual request is sent by you. But how does the response reaches exactly to you (your machine)? This is done using the Route Table. This will have the details about the SourceIP and DestinationIP. Lets say it goes from your machine(IP1) to Common router IP then multiple Hops(connecting to intermediary machines (DNS resolved at each hops)) would happen to reach the google.com
This concept is called the Forward Proxy. So using this, the company can block the requests to certain websites/applications.
So this Reverse Proxy is what Load Balancer does. Is that all about Load Balancer? Answer is NO.
Types of Load Balancers
OSI Layers
These are all not actual components or devices. Except few, others are imaginary layers.
Here we have these L4 an L7 layers. What does this Transport Layer means? This is the area where you are the getting the request transferred from the server(Load Balancer-LB) to your application.
This Load Balancer is called as Transport Layer LB.
In this layer, you will be having the data, but you can't see the data. Only the IP of the source will be visible. You can't decode the data. This comes under the TCP/UDP protocols.
If you just going to redirect based on IPs, then you can use this L4 Load Balancer.
What are all the applications which uses TCP/UDP? Databases, Apps for Caching - RabbitQ.
Mainly this L4 LB is used for DB load balancing.
Similarly we have L7 layer- Application Layer. Here you can see everything including all data -> url, IP, cookies/sessions. But why do we need L4. L4 is faster way to route because it doesn't need to decode to get data, it just knows about the IP. So, comparatively L4 is faster. L7 is little bit slower than L4.
Using the data, in L4 LBs, you can segregate the requests and route/redirect it to different machines. For example, if the request asks for only static items(images,css files), then we can route it to a particular machine. If a request asks for all items, then we can route it to different machine.
In AWS, these LBs are given as two different services
ALB - L7 - Application Level Load Balancer (Based on data - secregation)
NLB - L4 - Network Level Load Balancer (Based on IP - secregation)
SEGREGATION
Now we have the load balancer in place along with our M1, M2 and M3 machines of different capacity. Either L4 or L7 can be used. But how do we segregate the requests and send those to different machines. On what basis we would decide that certain requests goes to M1 and certain to M2 and others to M3.
There are different methodologies to decide which request to be routed to which machine.
1. Round Robin
R1 to M1, R2 to M2 and R3 to M3 then R4 to M1, R5 to M2...
This way we route the requests one after the other.
2. Least Connections
I can't treat a machine(2GB) same like machine(5GB). I should not load these two machines similarly. May be based on the number of requests(in other words burden/load - Number of connections or how much CPU its consuming) being processed, i can send the current request to the machine which currently handling least connections/requests.
3. Weighted Round Robin
I personally know a particular machine has a least capacity and the other machine has a medium capacity and another with large capacity. SO I can assign weightage to each machines. Lets say if 11 requests came, I'll send a 5 to one and another 4 to another and 2 to another based on its weightage.
All these can be configured in the Load Balancer. Similarly we have different methodologies, the above ones are the most popular.
AWS-ELB
In AWS, we can consider to create two EC2 instances and each instances will have its own IP allocated. Now for each of this EC2, you would have one security group, which controls the incoming & outgoing request. You can even create one single common security group for multiple EC2 instances. It is basically used to check which requests can be allowed in or out.
And here in AWS, between the load balancer and your EC2 instances, there is one more component called as Target Groups (TGs).
This Target Group is just a logical grouping/segregation of your instances which you can decide based on various factors. For example, if you thought about grouping all high end machines, then all those EC2 instances can be grouped together into one Target Group. And another Target group with different lower end set of machines. This way you can have N number of Target Groups, But in AWS the maximum number TGs allowed is 5. But there is no limit with number of EC2 within a TG.
The load balancing algorithm can be set at the Load Balancer level and even within a Target Group level.













Top comments (0)