Consistent Hashing
Hashing spreads keys evenly, and the plain way to turn a hash into a node, the remainder after dividing by the node count, breaks the moment the count changes. Going from ten nodes to eleven moves about ten keys in every eleven. Consistent hashing is the arrangement in which adding the eleventh node moves about one key in eleven, all of it onto the new node.
That one property is why cache tiers and leaderless stores use it, and why it comes up in nearly every round where a node is added or lost. Load balancers use the same idea to keep a client on one server, which Networking Deep Dive covers, and the hash table on one machine is Computer Science Deep Dive's. This lesson is about data spread over many machines.
What Modulo Costs
Under modulo, a key keeps its node only when its hash leaves the same remainder divided by ten as divided by eleven. That happens for about one key in eleven. The other ten move.
In a store, that means copying nearly all the data at once. In a cache tier, it means a miss on nearly every key at once. Take Chapter 2's cache tier: 20,000 reads a second over ten nodes. Ten keys in eleven now map to a node that does not hold them, so about 18,000 reads a second fall through to the store while the new placement warms. That number is the reason the ring exists.
The Ring
Nodes and keys are hashed onto the same circle of hash values, and a key belongs to the first node clockwise from it. A new node takes only the keys between itself and the node before it, all from one neighbour. A node that leaves hands its keys to the next node on.
From ten nodes to eleven, about one key in eleven moves. The cache tier sees about 1,800 misses a second instead of 18,000, a tenth of the load that modulo sends to the store. Against Chapter 2's store, sized for 200 reads a second with room for 600, even that is more than three times too many, so the new node is filled with the hottest keys of its arc before it takes traffic.
Every client or router has to place keys on the same ring, so they all need the same membership. It comes from gossip, in which each node passes what it knows of the others to a few peers at a time until every node has it, which the key-value store in Chapter 10 uses, or from a configuration store, which Chapter 5 covers. A client whose view is stale asks the wrong node, so the system needs a way to correct it: a redirect from the node, or a fresh copy of the membership.
Virtual Nodes
With one point per node, the arcs come out uneven, so one node can own a much larger share of the keys than another. A node that leaves hands its whole load to one neighbour, which can double that neighbour's work.
Many points per node, called virtual nodes, fix both. The shares even out, a departed node's keys spread over all the others, and a bigger machine can take more points than a small one. The cost is a bigger map, nodes times points, and a join that becomes many small transfers instead of one. Dynamo and Cassandra place data this way.
Copies on the Ring
A key's copies go to the next nodes clockwise, skipping points that belong to a machine already chosen, so that the copies land on distinct machines. Dynamo calls this list the preference list. Spreading copies across zones is the same walk with one more rule: skip a machine whose zone already holds a copy. How many of those copies must answer a read or a write is the subject of quorums, in Chapter 4.
Fixed Slots Instead
Many systems hash keys into a fixed number of slots instead, chosen once and far larger than the cluster will grow, and keep a small table from slot to node. Adding a node moves whole slots, and only those slots' keys move. That is the ring's property, with the move decided by an operator or a controller instead of falling out of the hash. Redis Cluster works this way.
The cost is that the slot count caps the cluster, and the table is a directory, the scheme from the previous lesson, that someone keeps. Choose a ring when membership changes often and nothing should own a map. Choose fixed slots when moves are deliberate and someone should say which data goes where.
A ring with virtual nodes places keys by the hash alone, so when a node joins, what moves follows from the ring with no central map. Use it for a cache tier or a leaderless store whose clients compute placement themselves.
Fixed slots put keys into a slot count chosen once and move slots through a map. Use them when moves should be deliberate and visible, and the slot count is safely larger than the cluster will ever be.
How Interviewers Probe This
The first probe is "You add a node to the cache tier. What happens to the hit rate?" A good answer says that about one key in the new node count misses at first, turns that into reads a second on the store, contrasts modulo, and says how the misses warm back up. You might say: "Going to eleven nodes moves about one key in eleven, so roughly 1,800 reads a second miss until they're refilled. With modulo it would be 18,000."
The second is "Why virtual nodes?" A good answer names the uneven arcs, the departed node's load landing on one neighbour, and machines of different sizes, and then gives the cost: a bigger map.
The third is "Would you use consistent hashing here?" A good answer ties the choice to how often membership changes and whether anything should own a map, and names fixed slots as the other way, with Redis Cluster as the example.
- "Consistent hashing balances load." It balances keys, and only roughly without virtual nodes. A hot key is still one point on the ring, which is the next lesson's problem.
- "Nothing moves when a node joins." About one key in the new node count moves. Less is not none, and it still has to be copied.
- "Every distributed store uses a ring." Many use fixed slots, or ranges with a map. The ring is one way to get small moves, not the only one.
- "Virtual nodes are free." The map grows, and every join or departure is many small transfers, each tracked and throttled.
- "The keys arrive on the new node the instant it joins." The copy takes time, and until it finishes the old owner serves those keys or they miss. The next lesson walks through the live move.
- Say what fraction of keys moves when a node joins, with the node count, before naming the scheme.
- Put many virtual nodes on every machine of any ring, and more of them on bigger machines.
- Say who holds the membership, gossip or a configuration store, and what a client with a stale view does.
- Choose fixed slots when an operator must control what moves, and size the slot count for the largest cluster the horizon needs.
Knowledge Check
A cache tier grows from ten nodes to eleven. About what share of the keys moves under modulo placement, and on a ring?
- About one in eleven under both, since one node in eleven is new
- About one in ten under modulo, and none at all on the ring
- About ten in eleven under modulo, and one in eleven on the ring
- About half under modulo, and about one in eleven on the ring
What do virtual nodes fix on a ring?
- Uneven arcs, a lost node's load landing on one neighbour, and machine size
- Hot keys, by giving each popular key several points of its own on the ring
- Copy placement, by putting every replica of a key in a different zone
- Membership, by removing the need for gossip or a configuration store
Where do a key's three copies go on a ring with virtual nodes?
- To the next three points clockwise, whichever machines own them
- To the next points clockwise, skipping any machine already chosen
- To three random machines, picked again by the router on each write
- To the machine before the key's point and the two machines after it
A team wants every data move planned and visible, and their cluster will stay at a few hundred nodes at most. Which placement fits?
- A ring with virtual nodes, where every join moves keys with no map
- Modulo of the node count, recomputed by every client after a change
- Range partitions that split on their own as each slice keeps growing
- Fixed slots and a table from slot to node, moved by an operator
A cache tier of ten nodes on a ring serves 20,000 reads a second. One node is added. What does the store behind it see?
- About 18,000 more reads a second, until the whole tier is warm
- About 2,000 fewer reads a second, since the tier has more memory
- About 1,800 more reads a second, until the moved keys are warm again
- No change at all, because moved keys are copied before it joins
You got correct