Consistent Hashing: Simplifying Load Balancing in Distributed Systems

Sumeet Panchal
4 min readDec 5, 2024

--

Maintaining efficient traffic distribution is crucial in a distributed web architecture, where user requests are routed through load balancers to backend servers. A common challenge arises when servers are added or removed: How do we redistribute traffic with minimal disruption? This is where consistent hashing shines.

Let’s break down this concept and explore how it makes load balancing smarter, with a relatable real-world analogy.

What is Consistent Hashing?

Consistent hashing is a hashing technique that minimizes disruption when the system changes — such as adding or removing servers. Unlike traditional hashing methods, where all data mappings might need to be recalculated, consistent hashing ensures that only a small fraction of keys (or requests) are reassigned, making it ideal for scalable and dynamic environments.

How Consistent Hashing Works

  1. Hashing Nodes and Keys on a Circular Ring:
    - Imagine a circular ring labeled from 0 to 360.
    - Servers (nodes) and requests (keys) are assigned positions on this ring using a hash function (e.g., MD5 or SHA-256).
  2. Mapping Keys to Nodes:
    - A request is assigned to the first server encountered clockwise from its position on the ring.
    - This ensures an even distribution of requests across all servers.
  3. Handling Server Changes:
    - Adding a Server:
    Only the keys that map to the new server are reassigned, leaving the rest unaffected.
    - Removing a Server: Keys mapped to the removed server are reassigned to the next server in the ring.

This design minimizes the impact of changes and ensures most requests continue to be handled by their original servers.

A Real-World Analogy: The Mail Delivery System

To better understand consistent hashing, let’s use a simple analogy: assigning mail to delivery agents in a neighborhood.

Scenario: Traditional Hashing

Imagine a central mail office assigning houses (requests) to delivery agents (servers) based on house numbers. A simple rule might be:

  • Rule: Divide the house number by the total number of agents, and assign it to the remainder.
  • For 5 agents (A1, A2, A3, A4, A5), house 21 is assigned as 21 % 5 = 1 → Agent A2.

Problem: Adding or Removing Agents

If a new agent A6 is added, the rule changes and the mapping is recalculated for all houses:

  • House 21 is now assigned as 21 % 6 = 3 → Agent A4.

This causes significant disruption, as most houses get reassigned to different agents. In computing, this leads to cache misses, increased latency, and overhead.

Scenario: Consistent Hashing

With consistent hashing, delivery agents and houses are placed on a circular route:

  1. Placing Agents on the Route:
    - Agents (A1, A2, A3...) and houses (21, 42, 201...) are hashed to positions on a circular route.
    - For example, Agent A1 is at 50, A2 at 120, and house 21 is at 95.
  2. Assigning Houses:
    -
    A house is assigned to the first agent clockwise from its position.
    - House 21 (position 95) is assigned to A2 (position 120).
  3. Adding a New Agent:
    -
    When Agent A6 joins at position 110, only houses between 95 and 110 are reassigned to A6.
    - Other assignments remain unchanged, minimizing disruption.

This mirrors how consistent hashing ensures smooth scaling and fault tolerance in a distributed system.

Benefits of Consistent Hashing in Load Balancers

  1. Efficient Redistribution: Adding or removing servers affects only a subset of requests, minimizing cache misses and operational overhead.
  2. Scalability: Servers can be added or removed dynamically without major recalculations, making it ideal for scaling systems.
  3. Fault Tolerance: If a server fails, its requests are redistributed to the next server in the ring, ensuring continuity.
  4. Even Load Distribution: Using virtual nodes (multiple placements of a server on the ring) ensures balanced traffic distribution and avoids overloading any single server.

Practical Use Case: Distributed Web Applications

Consider a web application with millions of users. Requests are routed through a load balancer to backend servers:

  • Without consistent hashing: Every time a server is added or removed, most requests are rerouted, causing performance drops due to cache misses.
  • With consistent hashing: Only a small portion of requests are rerouted, preserving cache efficiency and ensuring seamless scaling.

Key Takeaways

Consistent hashing is like assigning mail to delivery agents using a circular route: even with changes, the system remains largely undisturbed. By reducing disruption, it ensures smooth scaling, fault tolerance, and balanced load distribution, making it a vital tool for modern distributed architectures.

Whether you’re designing scalable systems or managing traffic for a web application, consistent hashing simplifies complexity and keeps your system running smoothly.

How AWS and Azure Make Load Balancing and Consistent Hashing Easier

Cloud service providers like AWS (Amazon Web Services) and Microsoft Azure simplify the complexities of load balancing and distributed systems. These platforms offer managed services, enabling developers to focus on building applications while leaving the heavy lifting of infrastructure management, scaling, and fault tolerance to the cloud.

--

--

Sumeet Panchal
Sumeet Panchal

Written by Sumeet Panchal

Programming enthusiast specializing in Android and React Native, passionate about crafting intuitive mobile experiences and exploring innovative solutions.

No responses yet