Sub topic 10
Choosing replicas on the ring
This chapter assumes you know how the ring lookup works, including virtual nodes and the sorted list with binary search, and that you have the HashRing class and _hash function from the previous chapter. It also assumes you know what a replica is: a copy of a key's data on another node.
Why one node is not enough
A lookup returns one node per key. If that node fails, the data on it is unreachable. Storing each key on several nodes fixes this. The ring gives a natural rule for choosing them: the first node is the owner, and the next nodes are the ones that follow it clockwise.
This rule has a useful property. When a node leaves, the new replica set for each affected key differs from the old set by one node. Only a small part of the data has to be copied.
Walking clockwise and skipping duplicates
With virtual nodes, the next positions on the ring often belong to the same physical node. If you simply took the next three positions, you could store all three "replicas" on one machine. A crash would then lose every copy.
So the walk must collect distinct physical nodes. Start at the key's position. Visit each virtual node in clockwise order. Skip any node you have already chosen. Stop when you have enough nodes.
The code
import bisect
def get_replicas(ring, key, count):
if not ring._hashes:
raise LookupError("ring is empty")
start = bisect.bisect_right(ring._hashes, _hash(key))
replicas = []
for step in range(len(ring._hashes)):
node = ring._owners[ring._hashes[(start + step) % len(ring._hashes)]]
if node not in replicas:
replicas.append(node)
if len(replicas) == count:
break
return replicasThe function works in four steps:
bisect_rightfinds the first virtual node after the key's hash, as in a normal lookup.- The loop visits positions in order. The
% len(...)wraps from the end of the list back to the start. - The check
node not in replicasdrops repeat virtual nodes of a node already chosen. - The loop stops as soon as
countdistinct nodes are found. It never runs for more than one full lap of the ring.
The first item in the result is the same node that get_node returns, so it acts as the primary.
Use it like this:
ring = HashRing(["a", "b", "c", "d"])
print(get_replicas(ring, "user:42", 3))The output is a list of three different node names, for example ['c', 'a', 'd']. The exact order depends on the hash values.
Gotchas
- Too few nodes. If
countis larger than the number of physical nodes, the loop finishes a full lap and returns fewer nodes than you asked for. Check the length of the result if your system requires a fixed replica count. - Same rack or zone. Distinct machines can still share a rack, a power supply, or an availability zone. Production systems often extend this walk to skip nodes in a zone that is already chosen. The plain version here only guarantees distinct machines.
- Failures change the answer. If you remove a failed node from the ring, every replica set that included it changes. Adding a node moves only the keys in the ranges that node takes over, and it joins the replica sets of the keys just before those ranges.
- Consistency is a separate problem. Choosing replicas tells you where copies live. It does not decide how writes reach them or how many must acknowledge. Those rules belong to the replication protocol, not to the ring.
Exercise
A ring has three physical nodes, each with 100 virtual nodes. You call get_replicas(ring, "k", 4). What does it return, and why?
Show answer
It returns a list of all three nodes, in clockwise order from the key. The loop visits every virtual node in one full lap and finds only three distinct physical nodes. It never reaches a count of 4, so it never breaks early and returns the three it found. The caller must check the length if it needs four replicas.