← All posts

October 2, 2021 · 5 min read

Load balancer

Systems-Design-Fundamentals

Let's try to understand the Load balancer(LB) with five questions.

  1. What is Load Balancer?
  2. Why do we need LB?
  3. What are the advantages and disadvantages of using it?
  4. What are the types of LB?
  5. What algorithms does it uses?

1. What is Load Balancer?

Load Balancer is a critical component of any distributed system. It generally sits between client and server, accepting the incoming network and efficiently distributing it across multiple servers using various algorithms.

2. Why do we need LB?

Modern high-traffic websites have to serve hundreds of thousands of concurrent requests from the client in a fast and reliable manner. A single instance of server can handle a limited amount of requests in a given amount of time. It's more likely that our server will become overloaded, which might lead to a failure in our system. this can be handled by scaling our system. We can scale it vertically, but there's a limit to the amount of power we can increase. There's a second option, which is to scale it by adding more servers/machines horizontally. It will increase our throughput by many folds. But of course, this is assuming that all of our clients will be requesting servers in a balanced way. But clients don't know where to direct their requests to the servers in this way. We need some mechanism to handle our redirecting problem. This is where Load Balancer comes into the picture. It sits between the client and server and balances the load evenly on the server. It makes sure that no one server is overworked.

3. What are the advantages and disadvantages of using it?

Advantages

Disadvantages

4. What are the types of LB?

A load balancer can be classified based on the duties it performs.

a.) Network Load Balancer / Layer 4 (L4) Load Balancer

L4 LB uses the information provided in the networking transport layer and distributes client requests across the cluster of servers. It operates at the transport layer, using the TCP and UDP protocols, extracts destinations IP addresses from the first few TCP streams, and does not inspect the packet content. L4 LB performs NAT on the request packet, changing the destination IP address from its own to one of the servers from the server pool. It does not consider any parameter at the application level like the type of content, cookie data, headers, locations, application behavior, etc. It only cares about the network layer information and directs the traffic according to this.

b.) Application Load Balancer / Layer 7 (L7) Load Balancer

L7 LB helps in creating a highly tuned and optimized server infrastructure. It operates at the application layer of the OSI model.The L7 LB evaluates a much more comprehensive range of data, including the HTTP header and SSL sessions, and redirects the traffic based on these parameters. In this manner, the application load balancer distributes traffic on individual usage and behavior.

5. What algorithms does it use?

Before forwarding a request, the load balancer ensures that the server they choose is functioning correctly. Health checks are done by regularly attempting to connect to the server to ensure that servers are listening. If some fail to respond, then they are removed from the pool of healthy servers. LB will not forward the traffic to it until it responds to the health check again. There are a variety of algorithms used based on the requirements.