Sub topic 6
Replicating each key to N nodes
Partitioning spreads keys across nodes, but a single copy of each key is a single point of failure. If the node that owns a key dies, that key is gone or unreachable. Replication fixes this by storing each key on N nodes. This chapter assumes you know how a consistent-hash ring places keys and virtual nodes, as covered in the previous chapters.
The replication factor
The replication factor, written N, is the number of nodes that hold a copy of each key. A common choice is N = 3. With three copies, the store keeps serving a key after one node fails, and it can still recover the data after two failures if repair runs in time.
Larger N improves durability and read availability. It also costs more storage and more write traffic, and it makes consistency harder to maintain. N is usually fixed per cluster or per keyspace.
Preference lists
The ring already tells you where a key lives. To replicate, walk clockwise from the key's hash position and take the first N distinct physical nodes. This ordered list is the key's preference list. The first node is the coordinator for the key. The others are its replicas.
Virtual nodes complicate this. Several consecutive positions on the ring may belong to the same physical machine. If you simply took the next N positions, two replicas could land on one machine, and one failure would remove both. The walk must skip positions whose physical node is already in the list.
import bisect
import hashlib
def h(key: str) -> int:
return int.from_bytes(hashlib.md5(key.encode()).digest()[:8], "big")
class Ring:
def __init__(self, nodes, vnodes=64):
self._points = sorted(
(h(f"{node}#{i}"), node) for node in nodes for i in range(vnodes)
)
self._hashes = [p[0] for p in self._points]
def preference_list(self, key: str, n: int):
"""Return up to n distinct nodes, walking clockwise from the key's position."""
start = bisect.bisect(self._hashes, h(key)) % len(self._points)
result = []
for i in range(len(self._points)):
node = self._points[(start + i) % len(self._points)][1]
if node not in result:
result.append(node)
if len(result) == n:
break
return result
ring = Ring(["node-a", "node-b", "node-c", "node-d"])
print(ring.preference_list("user:42", 3))The if node not in result check is what enforces distinct physical nodes. The loop is bounded by the number of ring points, so it returns fewer than N nodes if the cluster has fewer than N machines.
How a write reaches the replicas
A client sends a write to any node, which forwards it to the coordinator if needed. The coordinator sends the write to all N nodes in the preference list in parallel. It does not have to wait for all of them before answering the client. How many acknowledgments it waits for is controlled by the write quorum, covered in a later chapter.
Reads work the same way. The coordinator asks the replicas, collects their responses, and returns a result. Because replicas can disagree, reads may need to compare versions.
Common errors
- Replicas on the same failure domain. Distinct machines are not enough if they share a rack, power supply, or availability zone. Production systems add rack or zone awareness to the preference list so replicas spread across failure domains.
- Ignoring membership changes. When a node joins or leaves, preference lists change for part of the keyspace. Data must move to match, or reads will look in the wrong places.
- Assuming replicas are identical. Replication is asynchronous in practice. A replica that was down or slow holds stale data until repair catches it up.
Exercise
A cluster has 3 physical nodes, each with 64 virtual nodes, and N = 3. A key's clockwise walk first meets positions owned by node-a, node-a, node-b, node-a, node-c. What is the preference list, and why can't you take the first three positions?
Show answer
The preference list is [node-a, node-b, node-c]. The first three positions would give node-a twice and node-b once, so two replicas would sit on the same machine and one failure would remove both. The walk must skip positions owned by nodes already in the list.
Exercise
With N = 3 and exactly 2 physical nodes in the cluster, what does the preference_list function above return, and what does that mean for durability?
Show answer
It returns only 2 nodes, because it cannot find a third distinct node after scanning every ring point. The key has two copies instead of three, so durability is lower than the configured N implies.