Better Programming

Advice for programmers.

Follow publication

Member-only story

How Consistent Hashing Is Used by Load Balancers to Distribute Requests

Zeng Hou Lim
Better Programming
Published in
6 min readFeb 14, 2020
Photo by Chaitanya Tvs on Unsplash

Vertical vs. Horizontal Scaling

In a monolithic architecture, clients typically make requests to one single server. As the number of requests starts to scale, the single server does not have sufficient capacity to serve all the incoming requests.

Vertical scaling could be an option, where more CPU/RAM is added to the servers. This option could work for only so long before the hardware limitations are encountered.

In most cases, horizontal scaling, in which more servers are added, is usually a more scalable alternative.

Vertical v.s. horizontal scaling

Redirecting Requests With a Load Balancer

When we scale horizontally, the requests are directed to the load balancer instead of the servers directly.

The load balancer’s job is exactly what its name describes: its purpose is to balance the load on each server by distributing the requests as uniformly as possible.

Hash function and modulo (%)

All incoming requests, which will have a unique identifier (e.g. IP address), are assumed to be uniformly random.

Using a hash function, we are able to obtain an output value, after which we apply the modulo function to get the number that corresponds to the server that the load balancer should be directing the request to.

  1. hash(ipAddress) → output
  2. Output % number of servers -1 → server ID

It is important to use a good hash function to ensure that the output values are spread out across a range of values to improve the randomness. The modulo function then guarantees that the server ID is in the range of 0.(Number of servers -1.)

Visualizing the mapping

Let’s take a step back to visualize how we could possibly use an array as a data structure to map each request to…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Zeng Hou Lim
Zeng Hou Lim

Written by Zeng Hou Lim

Excited about living my best life and becoming a better engineer. I like taking complex ideas and breaking them down.

Write a response