Hash Ring — System Design

Point to the post that gets each night watch report on the village ring road: 18 levels of consistent hashing, hash rings and virtual nodes.

What you learn in this game

Consistent hashing puts servers and keys on one ring: every name is hashed to a number from 0 to 99 and stands at that number. In this village the ring is the ring road: the night-watch posts are the nodes (servers) and the houses are the keys. A house's report is always carried clockwise (the numbers rising) to the first post it meets. That post owns it, even when another post is closer behind.

The ring road loops: after 99 the numbers come back to 0 at the gate. A house that stands after the last post finds no post up to 99, so its report is carried on past 0 to the post with the smallest number. That is called wrapping around.

This is what consistent hashing is for. When a new node joins the ring, it only takes the keys on the arc just before it (from the node before it up to itself). Every other key stays on the same node. Compare hash % N, like Mr. Bayu's lockers: N changes, the remainders change, and nearly every key moves. That is why Cassandra, DynamoDB and memcached use a ring.

When nodes stand close together, the rest of the ring becomes one very long arc, and the node at its end carries nearly every key. This is called a lopsided load. A random hash does not promise the nodes spread out evenly. The way out is virtual nodes: each server is put at many points at once, so its arcs are spread out.

Virtual nodes fix the lopsided load: one server is hashed several times under different names (pos-harun#1, pos-harun#2), so it stands at several points of the ring. Its arcs are spread over the whole ring road, its load is more even, and when that server goes down its keys are shared among several other servers. The rule stays the same: a key goes to the first point clockwise, and that point belongs to a regu (a server).

What the levels ask

  1. Tap the post that owns each house.
  2. Tap the post that owns each house.
  3. If there is no post up to 99, carry on past 0.
  4. Tap the post that owns each house.
  5. Mind the house between two posts that stand close.
  6. A house at a post's number is owned by that post.
  7. Tap the post that owns each house tonight.
  8. Tap the post that owns each house without Mr. Harun's post.
  9. Tap the post that owns each house.
  10. Tap the post that owns each house, then see who is busiest.
  11. Track every owner, the ones that wrap around too.
  12. Tap the post that owns each house tonight.

And more, 18 levels in all.