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.

Get hands-on with 1400+ tech skills courses.