#📝 Note: Consistent Hashing

#Overview

Consistent hashing is a distributed hashing scheme that minimizes key redistribution when nodes are added or removed from a cluster. Unlike modular hashing (key % N), which remaps nearly all keys when N changes, consistent hashing remaps only K/N keys on average (K = total keys, N = nodes).

#Core Idea

#How It Works

Hash Ring (simplified):

        Node A (hash=100)
           │
    ┌──────┴──────┐
    │             │
    │   Keys in   │
    │  range      │
    │  (300,100]  │
    │             │
Node C           Node B
(hash=300)      (hash=200)

Key "user:123" → hash = 150 → assigned to Node B (first node clockwise)
Key "user:456" → hash = 250 → assigned to Node C
Key "user:789" → hash = 50  → assigned to Node A

#Virtual Nodes

Aspect Without Virtual Nodes With Virtual Nodes (150/node)
Distribution Uneven (depends on hash luck) Near-uniform
Heterogeneous HW Can't account for capacity More vnodes for stronger machines
Node addition Uneven key pickup Smooth redistribution
Memory overhead Low Moderate (150 ring entries per node)

#When to Use

#When NOT to Use

#Trade-offs

#Used In This Repo