Load Balancing Algorithms

Learn some common algorithms for load balancing.

How do LBs balance the load?

There are several load balancing algorithms that are commonly used in the industry. Let’s discuss the algorithms in two categories: application-layer algorithms and network-layer algorithms.

Application-layer algorithms

In application-layer load balancing, the load balancer has access to the data of the request. It can take decisions based on the request headers as well as the request body. Let’s discuss popular application-layer load balancing algorithms.

Hashing

A common application-layer algorithm is hashing. The LB can hash a set of predefined attributes and generate a hash value. The hash value is then mapped to one of the server nodes. Let’s give a simple example.

Assume that the request body from a client contains a user_ID. Upon receiving the request, the LB can do the following steps

  1. Hash the user_ID using any pre-configured hash function. Say for user_ID = abc123, the hash function returns a value 981723123.
  2. Take the returned value and map it to a server node. This can be done by using the modulo operation 981723123 % 5 which equals 3. Here, we are taking a modulo by 5 as this is the count of nodes in the system.
  3. Route the request to node 3.
Press + to interact
A quick demo of how hash-based load balancing looks like
A quick demo of how hash-based load balancing looks like

Endpoint evaluation

In this mechanism, the endpoint of the request is considered and routed accordingly. For example, let’s say we have two incoming requests. One hits the endpoint /v1/app/photos and the other hits /v1/app/videos. In such a setup, you might have two sets of servers for serving photos and videos respectively. The load balancer will evaluate the endpoints and route the request to the respective servers.

Press + to interact
In endpoint evaluation, different endpoints are served by different server nodes
In endpoint evaluation, different endpoints are served by different server nodes

Network-layer algorithms

In network-layer load balancing, the load ...