Fundamentals 10 min read

Consistent Hashing Explained: Hash Rings, Virtual Nodes, and Load Balancing

The article details the consistent hashing algorithm, describing how a hash ring and virtual nodes distribute keys across servers, minimize data movement during node changes, compare it with simple modulo hashing, and outline typical use cases such as load balancing, distributed caches, storage systems, and database sharding.

Subtle Storm
Subtle Storm
Subtle Storm
Consistent Hashing Explained: Hash Rings, Virtual Nodes, and Load Balancing

1. Technical Background

In modern distributed systems and high‑availability architectures, services are often deployed as clusters of multiple nodes (e.g., web servers, cache servers, database shards). The core problem is how to assign requests or data to these nodes while keeping data movement minimal when the number of nodes changes.

Traditional hash algorithms use a simple modulo operation: hash(key) % N (where N is the node count). When nodes are added or removed, the hash result for almost every key changes, causing massive data reshuffling, system jitter, and performance degradation. For example, expanding from 3 to 4 nodes requires migrating about 75% of key‑value pairs.

2. Drawbacks of Traditional Modulo Hashing

Modulo hashing is easy to implement and provides uniform distribution when the node count is fixed. However, it suffers from two critical issues in a distributed environment:

Node changes trigger large‑scale data migration because the mapping hash(key) % N becomes invalid for most keys.

Load imbalance can occur when the node count changes or the hash function is uneven, leading to hotspots.

3. Principles of Consistent Hashing

3.1 Building the Hash Ring

Consistent hashing maps the entire hash value space (0 ~ 2³²‑1, i.e., 0 ~ 4294967295) onto a circular ring. This large range accommodates many nodes and requests while fitting into a 32‑bit integer for fast computation.

Hash ring illustration
Hash ring illustration

3.2 Node Mapping

Each physical server is hashed to a point on the ring. A data key is also hashed; moving clockwise, the first node encountered becomes the storage destination. Example:

hash("ServerA") = 80°
hash("ServerB") = 200°
hash("ServerC") = 300°

3.3 Data Mapping

Keys follow the same rule. For instance:

hash("user123") = 150° → ServerB
hash("item999") = 250° → ServerC
hash("user888") = 10°  → ServerA (wrap‑around)

3.4 Impact of Node Changes

When a node is added or removed, only the keys that fall between the affected points need to move. Adding a new ServerD at 220° only migrates keys in the range 200° ~ 220° to the new node, leaving the rest untouched.

4. Virtual Nodes Mechanism

To further improve load balance, each real node is represented by multiple virtual nodes, calculated as hash(server + index). Virtual nodes are evenly spread around the ring, and keys map to virtual nodes first, then to the underlying physical server. This mitigates hotspot formation even when the number of physical nodes is small.

Advantages

Significantly better load balancing.

Smoother data migration during scaling.

5. Logical Diagram of Consistent Hashing

Consistent hashing logical diagram
Consistent hashing logical diagram

Key lookup steps:

Hash the key to a point on the ring.

Move clockwise to the first virtual node with a larger hash value.

Map that virtual node to its real server and route the request.

6. Comparison with Modulo Hashing

Data distribution: Modulo hashing – uniform; Consistent hashing – initially slightly uneven, but virtual nodes make it near‑uniform.

Node‑change impact: Modulo – large (most keys migrate); Consistent – small (only a few keys migrate).

Implementation complexity: Modulo – low; Consistent – moderate (maintain ring and virtual nodes).

Scalability: Modulo – poor; Consistent – strong.

Network friendliness: Modulo – poor; Consistent – high, suitable for distributed networks.

7. Typical Application Scenarios

7.1 Load Balancing

Consistent hashing ensures that requests for the same resource are routed to the same backend server, providing session stickiness. When servers are added or removed, only a subset of requests shift, improving stability. Implementations exist in Nginx, Envoy, HAProxy, etc.

7.2 Distributed Caching

Systems like Redis Cluster, Memcached, Twemproxy, and Amazon ElastiCache use consistent hashing to distribute cache keys, avoid data skew, and automatically reroute requests on node failure while triggering minimal data migration.

7.3 Distributed Storage Systems

HDFS, Cassandra, Ceph and similar platforms employ consistent hashing to spread large data sets across nodes, limit migration to local data on scaling, and enable flexible replica placement by selecting subsequent nodes clockwise as replicas.

7.4 Distributed Database Sharding

MySQL sharding can use consistent hashing for table routing, ensuring a user's data resides in the same shard. Edge‑node IP hashing also maps users to the nearest node, minimizing impact during capacity expansion.

8. Summary

Consistent hashing constructs a hash ring and virtual nodes to address load imbalance and costly data migration caused by dynamic node changes in distributed systems. Its core benefits are efficient data movement, flexible scalability, and high network friendliness, making it a cornerstone technology for load balancers, distributed caches, storage systems, sharded databases, CDNs, and other high‑concurrency, high‑availability architectures.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Scalabilityload balancingconsistent hashingdistributed cachingvirtual nodeshash ring
Subtle Storm
Written by

Subtle Storm

The micro era's marvels are boundlessly subtle.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.