In this lesson · topic 2 of 7
Topic 2
The ring and virtual nodes
4 min read
Put everything on a circle
Pick a hash function with a big range and bend that range into a circle, so the largest value is next to zero. Hash each node’s name onto the circle. Hash each key onto the same circle with the same function. A key belongs to the first node you meet walking clockwise from the key.
Step through it below. The ring is the real one the lab uses, with three nodes, two points per node, and twenty-four keys.
Placing and moving keys on a ring
Step 1 / 4- nodes
- 3
- ring points
- 6
Hash the nodes onto a ring
Each node is hashed to points on a circle of hash values. Here every node gets two points, so there are six marks. Those extra points are virtual nodes.
Hash a key to a point
The key is hashed with the same function, so it lands somewhere on the same circle. This is key 3.
Walk clockwise
The key belongs to the first node point it meets going clockwise. Nothing else is consulted, so any client with the ring can find the owner on its own.
Add a node
The new node adds points of its own. Each new point takes over only the arc between it and the previous point. Every key that moved went to the new node, and no key moved between old nodes.
- Hash the nodes onto a ring. Each node is hashed to points on a circle of hash values. Here every node gets two points, so there are six marks. Those extra points are virtual nodes. nodes: 3; ring points: 6.
- Hash a key to a point. The key is hashed with the same function, so it lands somewhere on the same circle. This is key 3. key position: 14% around the ring.
- Walk clockwise. The key belongs to the first node point it meets going clockwise. Nothing else is consulted, so any client with the ring can find the owner on its own. owner: node 1.
- Add a node. The new node adds points of its own. Each new point takes over only the arc between it and the previous point. Every key that moved went to the new node, and no key moved between old nodes. keys moved, ring: 4 of 24; keys moved, hash mod N: 21 of 24.
Why a join moves so little
A new node puts its points on the circle. Each point takes over only the stretch between itself and the previous point. Keys outside those stretches are untouched. Every key that moves goes to the new node, and none move between existing nodes. Leaving works the same way in reverse: a node’s keys go to the next node clockwise and nothing else changes.
The problem with one point per node
With one point per node, the arcs between points are random lengths. Some nodes get long arcs and own a lot of keys. Some get short arcs and sit nearly idle. Random points on a circle are not evenly spaced.
The fix is virtual nodes. Each physical node places many points on the circle, scattered by hashing different names for the same node. Many small arcs average out, so each node’s total share is closer to its fair share. A node that leaves also spreads its keys over many neighbours instead of dumping them on one.