Skip to content
We areTOP 1%

What is consistent hashing, and why is it used?

HardConceptLoad Balancing
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

Answer

Consistent hashing maps both servers and keys onto the same circular hash space (a ring); each key belongs to the first server found moving clockwise from the key's position. When a server is added or removed, only the keys in its neighbouring range move, roughly K/N of K keys across N servers, instead of almost all of them as with hash(key) % N.

  • Virtual nodes: each physical server is placed at many points on the ring, which evens out load and lets stronger machines take more positions.
  • Uses: distributed caches, partitioning in Dynamo-style databases such as Cassandra, and sticky routing in load balancers.
  • Replication: a key can also be stored on the next few distinct servers clockwise.
  • Lookup is a binary search over the sorted ring positions, O(log V) for V virtual nodes.

More in Load Balancing

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account