Skip to content
System Design

8. Consistent Hashing Hindi – Distributed Caching & Sharding

June 12, 2026 13 min read

नमस्ते दोस्तों!

स्वागत है The Easy Master पर!

आपने distributed caching (Redis) और database sharding के बारे में सीखा। लेकिन एक बड़ी problem है – जब आप cache servers या shards add या remove करते हो, तो पुरानी data distribution टूट जाती है। Simple modulo hashing (key % N) use करोगे तो add/remove पर almost सारी keys remap हो जाती हैं – जिससे cache miss storm आता है और database पर load बढ़ जाता है।

Yahi problem consistent hashing solve karta है – ek hashing technique jo minimize karti hai remapping when servers change.

Jab maine pehli baar distributed cache design kiya, maine modulo hashing use kiya। जब node add ki, लगभग सारे keys mismatch हो गए और database पर heavy load आ गया। Phir maine consistent hashing implement ki – rehashing sirf 1/N keys की होती है। Life easy हो गई।

इस Consistent hashing Hindi article mein main aapko sikhata hoon:

✅ Simple hashing problems – modulo hashing ke issues
✅ Consistent hashing kya hai – ring-based approach
✅ Virtual nodes – load balancing kaise improve karein
✅ Advantages over simple hashing – minimal remapping
✅ Use cases – distributed caching (Redis), database sharding, load balancing
✅ Implementation intuition – hash ring, binary search
✅ Real-world examples – Amazon DynamoDB, Cassandra, Discord
✅ Common mistakes aur best practices

Chaliye consistent hashing ka concept samajhte hain! 🔄🗄️

1. Problem Statement – Simple Hashing Ki Problems

Distributed caching ya database sharding mein aap data ko multiple nodes (servers) mein distribute karte ho using a hash function.

Simple approach (Modulo hashing):

  • Hash key karo → get integer → modulo N (number of nodes)
  • Example: key = "user:123", hash = 123456, N = 3 → 123456 % 3 = 0 → node 0

Problems with modulo hashing:

ProblemExplanation
Massive remapping on node changeNodes add ya remove karo to N changes → almost all keys map to different nodes → cache miss storm → database overload
Not scalableEvery scaling event causes temporary performance degradation
Uneven distributionIf keys not uniformly distributed, some nodes get more load

Example: Suppose N=3, keys distributed. Now add 4th node, N becomes 4. Old mapping key % 3 vs new mapping key % 4 – only about 1/N keys remain same (approx 25% match). 75% keys remap → cache ineffective temporarily.

Personal Experience: Humne 3 node Redis cluster mein modulo hashing use kiya। 4th node add किया, to database queries 3x बढ़ गए। Isi तरह sharding में resharding painful होता है। Consistent hashing यही problem solve karta hai।

2. Consistent Hashing Kya Hai? – The Ring Idea

Consistent hashing ek distributed hashing technique hai jisme hash space (e.g., 0 to 2^32-1) को एक circular ring (hash ring) माना जाता है। Nodes aur keys dono ko ring पर hash किया जाता है। Each key is assigned to the first node encountered moving clockwise from the key’s position.

Visual analogy: एक घड़ी (clock) जहाँ numbers 0 se 2^32-1 तक हैं। Node और key दोनों को इस ring पर रखो। Key से clockwise direction में पहला node उस key को handle करेगा।

Key properties:

  • Monotonicity: Node add/remove hone par sirf neighbouring keys remap होती हैं (only 1/N fraction).
  • Load balancing (with virtual nodes): Keys distribution uniform होती है।

Simple modulo hashing ka alternative: Consistent hashing minimizes disruption when nodes change.

3. काम कैसे करता है – Hash Ring and Key Placement

Step-by-step:

  1. Define hash space – say 0 to 2^32-1 (or 0 to 2^64-1)
  2. Place nodes on the ring – Hash node identifier (e.g., IP address) to a position on ring.
  3. Place keys on the ring – Hash key to a position.
  4. Assignment: For a key, walk clockwise and pick the first node encountered. If reach end, wrap around.

Example (small ring 0-100):

Consistent hashing ring diagram | Consistent hashing Hindi
Code
Ring: 0--------25--------50--------75--------100
          NodeA     NodeB          NodeC
                (at 25)   (at 60)   

Key at position 30 → clockwise first node = NodeB (at 60). Key at position 90 → wrap to NodeA (at 25).

When a new node added (NodeD at 80):

  • Only keys between previous node (NodeB at 60) and new node (NodeD at 80) get remapped to NodeD.
  • Rest keys unaffected.

When a node removed (NodeB leaves):

  • Keys formerly assigned to NodeB get reassigned to next clockwise node (NodeC).
  • No other keys change.

Result: Only k/N keys remap, where k = total keys, N = nodes। Modulo hashing mein almost all remap।

4. Virtual Nodes – Load Balancing Ka Solution

Problem: With simple consistent hashing, if nodes have different hash positions, load distribution can be uneven. Also, adding a node may get a small segment if placed near another node.

Solution: Virtual nodes – Each physical node is represented by multiple virtual nodes on the ring.

How virtual nodes work:

  • For each physical node, create V replicas (e.g., V=100 or 200) each with its own hash (using different suffixes).
  • Keys are assigned to virtual nodes, but all virtual nodes map back to same physical node.
  • More virtual nodes → more uniform distribution.

Benefits:

  • Better load balancing – Hotspots reduced.
  • Heterogeneous nodes – Powerful nodes can have more virtual nodes than weaker nodes.
  • Smoother addition/removal – New node gets a slice from many existing nodes, not just one neighbour.

Practical values: Cassandra uses 256 virtual nodes per physical node by default. Redis Cluster uses a fixed number of slots (16384) assigned to nodes – similar concept.

Visual: Physical node A → vnodeA1, vnodeA2, vnodeA3 scattered around ring। Removal of A means its vnodes removed, but keys reassigned to many different physical nodes (not just one neighbour).

5. Advantages of Consistent Hashing

AdvantageExplanation
Minimal remappingNode add/remove पर only ~1/N keys remap – cache efficient
ScalabilityEasy to add or remove nodes without massive reshuffling
Load balancing (with virtual nodes)Uniform distribution across nodes
Fault toleranceNode failure affects only its keys
SimplicityEasy to understand and implement (conceptually)
Used in many production systemsAmazon DynamoDB, Cassandra, Redis Cluster, Discord

Comparison table: simple modulo vs consistent hashing:

AspectModulo HashingConsistent Hashing
Remapping on node changeAlmost all keys (≈100%)~1/N keys
Load balancingDepends on hash uniformityGood with virtual nodes
Implementation complexityTrivialModerate
Use in dynamic clustersPoorExcellent

6. Use Cases in Real Systems

6.1 Distributed Caching (Redis Cluster)

Redis Cluster uses concept of hash slots (16384 fixed slots) – not exactly virtual nodes but similar idea: each node covers a range of slots. Consistent hashing variant.

When you add new node, some slots move from existing nodes to new node – only those keys remapped.

Benefits: Cache hit ratio remains high during scaling.

6.2 Database Sharding (Cassandra, DynamoDB)

Apache Cassandra uses consistent hashing with virtual nodes (vnodes) to distribute data across cluster. Each physical node has multiple vnodes spread in ring. Adding new node takes over vnodes from many existing nodes – rebalancing fast.

Amazon DynamoDB (the inspiration for consistent hashing) uses similar concept for partitioning. DynamoDB uses consistent hashing across storage nodes; requests routed to coordinator node that knows the ring.

6.3 Load Balancing (HTTP requests)

You can use consistent hashing to stick requests from same client to same backend server (session persistence). Instead of IP hash (which changes when backend count changes), consistent hashing minimizes remapping.

NGINX consistent hashing module: hash $request_uri consistent; – ensures cache-friendly distribution for reverse proxy caching.

6.4 Discord – Real-time Messaging

Discord uses consistent hashing to distribute guild (server) data across their storage nodes. Adding nodes doesn’t disrupt active sessions.

7. Consistent Hashing Implementation Intuition

Implementation doesn’t require full ring data structure. Instead, use sorted list (or binary search tree) of node hash positions.

Data structures:

  • Sorted list of node positions (after hashing node ID)
  • For key, find its hash, locate the smallest node position >= key hash (binary search). If none, wrap to first node.

Virtual nodes implementation: For each physical node, generate V virtual node positions using e.g., hash(node_ip + "_" + vnode_index).

Complexities:

  • Lookup: O(log N) for binary search (N = total virtual nodes)
  • Adding/removing node: O(log N) to insert/remove its virtual node entries.

Production libraries: Many implementations available – consistent-hashing package (Node.js), hash_ring (Python), guava (Java), ketama (memcached client).

8. Real-World Examples – DynamoDB, Discord, Cassandra

8.1 Amazon DynamoDB

  • Paper: “Dynamo: Amazon’s Highly Available Key-value Store” (2007)
  • Uses consistent hashing with virtual nodes to handle node failures and scaling.
  • Each node has many virtual nodes (hundreds).
  • Replication: Keys replicated to next N nodes clockwise on ring.

8.2 Apache Cassandra

  • Vnodes (virtual nodes) enabled by default – each physical node handles many token ranges.
  • Token ranges distributed using consistent hashing.
  • Adding new node: it gets vnodes taken from existing nodes, rebalancing without massive data movement.

8.3 Discord

  • Uses consistent hashing for distributing “guilds” (servers) across storage nodes.
  • Adding storage node: minimal data relocation.
  • Achieves high availability and fast scaling.

Lesson: Consistent hashing is battle-tested in massive production systems.

9. Common Mistakes (aur Unka Solution!)

MistakeWhy it’s wrongSolution
Too few virtual nodesLoad imbalance, hotspotsUse 100-200 virtual nodes per physical node
Not handling wrap-aroundKeys beyond highest node not assignedImplement wrap to first node (ring)
Hash function not uniformUneven distributionUse good hash (MurmurHash, SHA-1)
Reusing same hash for all vnodesVirtual nodes map to same positionAdd suffix or index for each vnode
Not updating ring when node failsRequests go to dead nodeUse membership protocol (gossip) to maintain ring
Ignoring replicationSingle node failure loses dataReplicate keys to next N nodes
Implementing from scratch when library existsReinventing the wheel, bug riskUse well-tested libraries

10. Best Practices – Production-Level Tips

✅ Use virtual nodes (100-200) – Good load balancing. More vnodes = better distribution but more memory.

✅ Choose good hash function – MurmurHash3, CityHash, SHA-1. Avoid simple string hashing.

✅ Make ring aware of node weights – Powerful nodes can have more vnodes.

✅ Implement membership protocol – Nodes should join/leave gracefully, updating ring.

✅ Use consistent hashing for replication too – Store copies on next N nodes clockwise.

✅ Library recommendation – For Node.js: consistent-hashing. For Python: hash_ring. For Java: Guava’s Hashing#consistentHash.

✅ Test scaling scenarios – Simulate node addition and removal, verify only fraction of keys remap.

✅ Monitor distribution – Check if some nodes get disproportionate keys (adjust vnode count).

✅ Combine with data versioning / vector clocks – For conflict resolution (Dynamo-style).

Personal Experience: Initially we implemented consistent hashing with 10 virtual nodes per physical node. Load distribution was uneven – some nodes 2x load. Increased to 200 vnodes, distribution became nearly uniform. Always use enough vnodes!

11. Resources – Cheat Sheet & Practice Prompts

Consistent Hashing Cheat Sheet

ConceptKey points
Ring sizeTypically 0 to 2^32-1 or 2^64-1
Virtual nodes100-200 per physical node
Key assignmentWalk clockwise from key position, pick first node
Remapping fractionNode add/remove → ~1/N keys remap
Wrap-aroundIf no node clockwise from key, go to first node

Hashing Algorithms Comparison

AlgorithmSpeedUniformityUse
MurmurHash3FastExcellentGeneral purpose
SHA-1SlowVery uniformCryptographic, legacy
CRC32FastGoodSimple
CityHashFastExcellentGoogle’s

Practice Prompts

Beginner:

  • Simple consistent hashing implementation karo in Node.js/Python without virtual nodes। Hash ring of 100 units, 3 nodes। Keys assign karo, then add 4th node, print how many keys remapped।

Intermediate:

  • Virtual nodes implement karo (10 per physical node)। Load distribution check karo – 100,000 keys, 5 nodes। Graph banake dekho key distribution per physical node।

Advanced:

  • Replication implement karo – each key stored on next 2 nodes clockwise। Test node failure scenario – remove one node, verify data still available from replicas।

12. FAQ

Q1: Consistent hashing aur modulo hashing mein kya antar hai?

Modulo hashing mein server list change hone par almost all keys remap होती हैं। Consistent hashing mein sirf ~1/N keys remap होती हैं।

Q2: Virtual nodes kyun zaroori hain?

Virtual nodes ke bina load distribution uneven हो सकती है – kuch nodes ko ring ka bada segment mil sakta है। Virtual nodes scatter each node across ring, improving balance.

Q3: Consistent hashing distributed caching mein kaise use hota hai?

Redis Cluster aur Memcached (via ketama) consistent hashing use karte hain for key distribution. Node add/remove hone par cache hit ratio high रहता है।

Q4: Kya consistent hashing database sharding mein use hota hai?

Haan – Cassandra, DynamoDB, aur some sharded MySQL setups consistent hashing use karte hain for partition placement.

Q5: Consistent hashing ke implementation options kya hain?

Khud implement kar sakte ho (sorted list + binary search) ya libraries use karo (e.g., hash_ring in Python, consistent-hashing in Node.js)。

Q6: Consistent hashing mein hash space kitna bada hona chahiye?

2^32 (4294967296) sufficient for most cases। Some systems use 2^64 (e.g., Cassandra token ring).

13. Conclusion – Ab Aapki Baari!

Bahut badhiya! Aapne aaj seekh liya:

✅ Consistent hashing Hindi – complete concept guide
✅ Simple modulo hashing ki problems – massive remapping
✅ Consistent hashing ring concept – minimal remapping
✅ Virtual nodes – load balancing aur distribution
✅ Advantages over simple hashing
✅ Use cases – distributed caching (Redis), database sharding (Cassandra), load balancing
✅ Real-world examples – DynamoDB, Discord, Cassandra
✅ Implementation intuition – binary search on sorted positions
✅ Best practices aur common mistakes

Consistent hashing distributed systems mein scaling aur fault tolerance ka important building block है। Cache miss storms ko avoid karta hai aur resharding smooth banata है।

Aapki challenge: Small consistent hashing demo implement karo (choose any language)। Without virtual nodes and with virtual nodes compare karo load distribution। Apna code link comment mein share karo!

Next topic kya chahiye?

  • Bloom Filters – Space-efficient Probabilistic Data Structures?
  • CAP Theorem Deep Dive – PACELC?
  • Leader Election & Consensus (Raft, Paxos)?

Comment mein batao!

The Easy Master ke saath consistent hashing seekhte raho। Happy hashing! 🔄🗄️

Resources

Additional Resources

Views: 411

TheEasyMaster

Author at The Easy Master.

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *