Load Balancers

System Design

Load Balancers

Load Balancing Algorithms

Load Balancing Algorithms

Load Balancing Algorithms

Effective load balancers will intelligently determine which server should process a user request in a server pool by using different algorithms. A load-balancing algorithm is the logic that a load balancer uses to distribute incoming traffic to between servers.

The following are examples of load-balancing algorithms:

Round Robin

The load balancers queue the client requests and direct them in a round-robin fashion. The first request goes to the first server, the second goes to the second server, and so on.

When the load balancer comes to the end of the list, It directs a request back to the first server. The round-robin approach is easy to implement and evenly distributes the traffic across all servers.

However, there is a risk that a server with low capacity receives many requests and becomes overloaded because the algorithm does not consider the server’s capacity. The algorithms work well in a server pool that has the same processing power.

Weighted Round Robin

This algorithm is an advanced version of the round-robin Algorithm. It distributes the traffic based on the weight scores of the servers. For instance, If server one is as twice as powerful as servers two and three, server one is provisioned with a higher weight than servers two and three.

When there are 5 sequential client requests, the load balancer will route 2 requests to server one, 1 request to server two and three each, and the last request will be routed to server one again.

The bigger the server, the more the requests to handle.

IP Hash Algorithm

The client and destination IP addresses are hashed to generate a unique hash key which is used to allocate a user to a specific server. The key can be regenerated if a session is broken and the user will be redirected back to the server. It has the advantage of caching, as the server caches data for that specific user.

This algorithm is appropriate in scenarios where it’s vital for a client to return to the same server for each successive connection.

Least Connection

This algorithm checks which server has the fewest current connections opened and sends traffic to that server. It assumes all the servers have equal processing power just like the round-robin approach.

Weighted Least Connection

This algorithm is an advanced least connection method where you can assign different weights on the servers depending on the processing power. The algorithm will make decisions on where to route the traffic depending on active connections and the weights of servers. If there are two servers with the least number of connections, the server with the highest weight is chosen.

Weighted Response Time

It averages the response times for all the servers with the number of active connections each server has to determine where to route the request. The algorithm ensures faster service for the user by determining the server with the quickest response time.

Random Algorithm

This algorithm uses a random number generator to distribute the client requests randomly to the servers. The algorithms assume the servers have similar configurations.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board