ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/sharder/tasks/task-8-2-consistent-hash
TASK

Implementation

Use consistent hashing for shard assignment:

  1. Place shards on a hash ring
  2. Hash each key to a ring position
  3. Find the next shard clockwise from key position
  4. Use virtual nodes for better distribution
  5. Minimize key movement when shards join/leave

Sample Test Cases

Hash key to ringTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"hash_key","msg_id":2,"key":"mykey"}}
Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}
{"src":"n1","dest":"c1","body":{"type":"hash_key_ok","in_reply_to":2,"msg_id":1,"key":"mykey"}}

Hints

Hint 1
Hash keys to ring positions
Hint 2
Use virtual nodes for balance
Hint 3
Minimal movement on changes
OVERVIEW

Theoretical Hub

Consistent Hashing for Sharding

Traditional modulo hashing (key % N) redistributes most keys when N changes. Consistent hashing only moves keys between affected neighbors, minimizing data migration during rebalancing.

Virtual Nodes

With few shards, the ring may be imbalanced. Virtual nodes give each shard multiple ring positions, smoothing distribution. Typically 100-200 virtual nodes per shard.

Key Concepts

consistent hashingkey distributionvirtual nodes
main.py
python
Implement Consistent Hashing for Sharding - The Sharder | Build Distributed Systems