SDE & MLE Prep

Consistent Hashing

Place nodes and keys on a hash ring; a key belongs to the first node clockwise. Adding/removing a node moves only about 1/N of the keys (with hash % N, almost all keys would move). Virtual nodes (each server appears at many ring positions) smooth out load and heterogeneity.

            0
       N3 .   . N1          key k1 -> walk clockwise -> N1
        .        .          add N4 between N1 and N2:
     k3 .         . k1        only keys in (N1, N4] move to N4
        .        .
       N2 .   . (N4)
           k2

Used by: Dynamo/Cassandra, Memcached clients, CDNs, load balancers for sticky routing.