Hashing Consistente

Anillo de hash

Escrito por Darjus Kazakevic · revisado el

Keys and nodes on one ring, so a node change moves only its own share.

The simple way to pick a node for a key is hash(key) mod N. It breaks when N changes: going from 4 to 5 nodes moves about 80% of the keys. For a cache, that is a near-total miss. For a database, it is a migration of almost everything.

Consistent hashing places nodes and keys on the same circle of hash values. A key belongs to the first node clockwise from it. When a node joins, it takes keys only from the arc just before it. Cassandra, Amazon's Dynamo and many Memcached clients use this idea.

En esencia — Put nodes and keys on one hash ring and give each key to the next node clockwise, so a node change moves only about 1/N of the keys.

Cuándo recurrir a él

Recurre a él cuando

  • A cache cluster resizes, and a total miss would flood the database
  • Clients route keys to nodes themselves, with no lookup table
  • A pool of storage or cache nodes that grows and shrinks often

Reconsidéralo cuando

  • A node count that never changes; plain modulo is simpler
  • The store already partitions keys for you, as most databases do
  • You need range scans over keys; hashing scatters them

Conceptos clave

The ring
All hash values, from 0 to 2³² − 1, arranged in a circle. Each node is hashed to one or more points on it. A key is hashed the same way and walks clockwise to the first node point.
Virtual nodes (vnodes)
Each physical node is placed on the ring many times, often 100–256 points. This evens out the arcs. When a node leaves, its keys spread over many nodes instead of all landing on one neighbor.
Weighted nodes
A bigger server gets more virtual nodes, so it owns a bigger share of the ring. A node with twice the memory gets twice the points.
Replicas on the ring (Preference list)
To keep three copies, store each key on the next three distinct physical nodes clockwise. Dynamo and Cassandra place replicas this way.
Alternatives
Rendezvous hashing scores every node for a key and picks the highest. Jump consistent hash is tiny and fast, but nodes can only be added or removed at the end. Redis Cluster uses 16,384 fixed slots instead.

Reglas de diseño

Place each node many times
One point per node gives uneven arcs, and one server can own twice its share. Use 100–200 virtual nodes per server, so each owns close to 1/N of the ring.
Every client must use the same ring
Same hash function, same node list, same number of virtual nodes. If two app servers disagree, one key goes to two nodes and you get misses and stale copies.
Plan for the keys that do move
The ring makes moves few, not free. A moved key is still a cache miss or a data copy. Warm up or migrate a new node's range before it takes full traffic.
Skip it when the store does it for you
Cassandra, DynamoDB and Redis Cluster already partition keys. Build your own ring only when your clients route keys themselves, as with a pool of Memcached servers.

Errores habituales

  • Too few virtual nodes, so one server owns 30% of the ring and runs hot.
  • Clients with different node lists after a deploy, sending the same key to two servers.
  • A weak hash function that puts similar names, like cache-1#1 and cache-1#2, close together, so arcs stay uneven.
  • Expecting the ring to fix a hot key. One key still maps to one node.