Skip to content
uencoder

Consistent Hashing

Consistent hashing remaps only 1/n of the keys when nodes are added or removed, minimizing redistribution; it is used in distributed caching and sharding.

Purpose

To minimize data movement when nodes join or leave, a core technique for distributed storage and caching.

Mechanism

Map both nodes and keys onto a 0-2^32 hash ring; each key belongs to the first node clockwise from it. Adding or removing a node affects only the keys in the adjacent arc. Virtual nodes (multiple hash points per node) improve balance. Load spreads roughly evenly.

Use cases

  • Distributed caching (Memcached)
  • CDN node sharding
  • Database sharding (Cassandra)
  • Load balancing

Examples

  • Input
    Node C joins
    Output
    Only keys in C's adjacent arc migrate

    Minimal migration

Common errors

Too few virtual nodes causes uneven load
Use 150+ virtual nodes per node

Related terms

References