• Uncategorised

Virtual Nodes in Consistent Hashing — Why They Matter

When building large-scale systems (like URL shorteners, distributed caches, or databases), we often use consistent hashing to decide which server stores which data.

But consistent hashing alone isn’t enough.

To make it truly scalable and balanced, we use Virtual Nodes (vnodes).


🔄 Quick Recap: Consistent Hashing

In consistent hashing:

  • Both servers and keys are placed on a hash ring
  • A key is assigned to the first server clockwise

This avoids massive data reshuffling when nodes are added or removed.


❌ Problem Without Virtual Nodes

Suppose we have 3 database servers:

ServerPosition on Ring
DB-A10°
DB-B150°
DB-C300°

Now look at responsibility ranges:

ServerKey Range
DB-A300° → 10° (small)
DB-B10° → 150° (large)
DB-C150° → 300° (large)

Issues

  • Uneven data distribution
  • Some nodes overloaded
  • Performance imbalance
  • Hard to predict load

This happens because server hashes are random, not evenly spaced.


✅ Solution: Virtual Nodes

Instead of placing each physical server once, we place it multiple times on the ring.

Example:

ServerVirtual Nodes (Positions)
DB-A10°, 80°, 170°, 290°
DB-B40°, 140°, 220°, 310°
DB-C70°, 120°, 200°, 330°

Now the ring is divided into many small segments shared across servers.


📊 What This Fixes

ProblemWithout VnodesWith Vnodes
Load imbalanceHighLow
HotspotsLikelyRare
ScalabilityPoorSmooth
Data migration impactLargeSmall

➕ Adding a New Server

Without virtual nodes:

  • New server may take one huge chunk
  • Some nodes lose massive data

With virtual nodes:

  • New server gets many small chunks
  • Load redistributes smoothly
  • Only ~1/N data moves

🧠 Why This Works

This leverages probability:

The more points each server has on the ring, the more evenly key ranges distribute.

It’s similar to splitting a pizza into many slices instead of just three big ones.


⚙️ How Systems Use Virtual Nodes

When system starts:

for each server:
   create multiple virtual node entries
   hash each and place on ring

When routing:

key_hash = hash(key)
find next vnode clockwise
route to its physical server

You may also like...