AlgoViz

Consistent hashing

medium 28 min

Consistent hashing puts keys and machines on one circle.

Where it shows up

The same pattern appears across everyday software.

  • Memcached clients

    The servers do not know about each other. Client libraries in the style of ketama hold the ring, so losing one cache server loses only its share of the hits.

  • Dynamo, the 2007 paper

    Amazon's shopping cart store placed each key on a ring and kept copies on the next machines clockwise. Virtual nodes let machines of different sizes share the work.

  • Apache Cassandra

    Every node owns tokens on a ring. Copies go to the following nodes, with racks and datacenters taken into account.

  • nginx in front of caches

    The consistent parameter of the hash directive keeps a URL on the same cache server even when another server is added, which protects the hit rate.

  • Envoy and Maglev

    Load balancers hash a connection or a header so that a user stays with one backend, and adding a backend disturbs few users.

  • Redis Cluster, a cousin

    It hashes keys into 16,384 fixed slots and assigns slots to masters. Moving a slot is an explicit operation instead of a walk on a ring.