Skip to content
StackDecode
System Design
5. Advanced Database Architecture8. Load Balancing and ScalabilityAnimated

Consistent Hashing

Hashing onto a ring so few keys move when nodes change.

The idea

Nodes and keys share a hash ring. A key belongs to the next node clockwise, so adding a node moves only a small share of keys.

Watch it work

Press play, pause, or step through. Some animations have settings you can change.

N1N2N3N4N1: 6 keysN2: 11 keysN3: 11 keysN4: 2 keys
  • outlined = moved key

Nodes and keys are hashed onto the same ring. Each key belongs to the first node clockwise from it.

nodes:
4
keys:
30
moved:
0
1/2

Dots on the ring are keys; larger circles are servers.

Step by step

  1. 1Hash nodes onto a ring
  2. 2Hash each key onto it
  3. 3Walk clockwise to the owner
  4. 4Adding a node moves only nearby keys

Where it is used

Caches and distributed stores.

Watch out for

Uneven spread without virtual nodes.

Related