What is consistent hashing, and why is it used?
HardConceptLoad Balancing
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
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | What is a load balancer, and why is it used? | Concept | Easy | Read the answer to What is a load balancer, and why is it used? | |||
| 2 | What are the common load-balancing algorithms, and when would you use each? | Concept | Medium | Read the answer to What are the common load-balancing algorithms, and when would you use each? | |||
| 3 | What is the difference between Layer 4 and Layer 7 load balancing? | Concept | Medium | Read the answer to What is the difference between Layer 4 and Layer 7 load balancing? | |||
| 4 | What is consistent hashing, and why is it used? | Concept | Hard | Read the answer to What is consistent hashing, and why is it used? |