Course contentsShow
← Databases & SQL
Lesson 1698 of 1,95316. Distributed Data ArchitecturesFree lesson

Consistent Hashing with Virtual Nodes

Distribute data evenly by mapping multiple virtual nodes to single physical shards to mitigate hotspots.

In this lesson, you will learn how "Virtual Nodes" extend consistent hashing to solve the distribution imbalance inherent in basic hash-based sharding.

The Problem: Hash Imbalance

When using standard consistent hashing, you map both data objects and physical storage shards onto a logical "hash ring" (typically a circle from 0 to 232−12^{32}-1). You locate a node by hashing its ID and placing it on the ring. Data items are hashed, and their target node is the first physical shard encountered moving clockwise.

The flaw here is non-uniform distribution. Because nodes are placed randomly, the distance between two nodes might be large, causing one shard to take on significantly more data than others, creating "hotspots." Furthermore, if you have nodes with different hardware capacities, standard consistent hashing cannot account for these differences in power.

The Solution: Virtual Nodes

To fix this, we introduce Virtual Nodes (or "vnodes"). Instead of placing a single physical node on the ring once, we map each physical node to multiple points (vnodes) on the ring.

For example, if physical node Node-A is high-capacity, we might map it to 200 virtual points on the ring, while a weaker Node-B is mapped to only 50.

// Concept of mapping vnodes
ring = HashRing()
physical_node = "Server_Alpha"
virtual_nodes = ["Server_Alpha_0", "Server_Alpha_1", ..., "Server_Alpha_199"]

for vnode in virtual_nodes:
    hash_val = hash(vnode)
    ring.add(hash_val, physical_node)

Why This Works

  1. Uniform Distribution: By increasing the number of points on the ring, the probability of any single shard dominating a segment of the ring decreases significantly. The data spreads out much more evenly across the physical hardware.
  2. Heterogeneity: We can give more vnodes to powerful servers and fewer to weaker ones, effectively weight-balancing the cluster based on actual capacity.
  3. Smooth Rebalancing: When a physical node fails, its vnodes are distributed across multiple different physical neighbors rather than just one "next" node, preventing a cascading failure or a single shard from becoming an immediate bottleneck.

Key Takeaway: Virtual nodes break the rigid mapping of physical shards to the hash ring, enabling balanced data distribution and heterogeneous hardware utilization.