What is the consistent hashing with virtual nodes in detail?

Answer

Consistent hashing with virtual nodes is the gold standard for distributing data across a dynamic cluster. Basic consistent hashing: imagine a ring of hash space [0, 2³²-1]. Each physical node N maps to one position: pos(N) = hash(N_id) mod 2³². Each key K maps to: pos(K) = hash(K) mod 2³². Key K is assigned to the first node clockwise from pos(K). Problem with basic consistent hashing: with few physical nodes, the hash positions are non-uniform — one node might own 40% of the ring while another owns 10% (uneven load). Also, node heterogeneity is unaddressed (powerful nodes should handle more load). Virtual nodes (vnodes): each physical node is mapped to V virtual nodes (V = 100-200 typically): pos(N, i) = hash(N_id + i) for i in [0..V]. The ring now has N×V positions. A key maps to the first vnode clockwise, which maps back to a physical node. Benefits: (1) Load uniformity: as V increases, the distribution of load per physical node converges to equal (law of large numbers — many positions per node averages out); (2) Heterogeneous nodes: powerful nodes get more vnodes; (3) Adding a node: new node steals roughly even shares from all existing nodes (not just neighbors); (4) Removing a node: its vnodes' keys are redistributed across all remaining nodes. Replication: key K is replicated to the next R distinct physical nodes clockwise. Real usage: Cassandra, DynamoDB, Riak, Voldemort all use vnode consistent hashing.