Sub topic 11
Cluster membership and failure detection with gossip
This chapter assumes you know how the hash ring and replication work from earlier chapters, and that you can read short Python snippets. Every node on the ring must know which other nodes exist and which are alive. This chapter covers how a leaderless cluster learns that without a central coordinator.
Why membership is needed
A coordinator uses the ring to choose a key's replicas. If its view of the ring is stale, it can send writes to a node that left the cluster or skip a node that joined. A central membership service solves this, but it becomes a single point of failure. Gossip removes that dependency.
How gossip works
Each node keeps a table that maps every known node to a heartbeat counter and a local timestamp. At a fixed interval, such as once per second, a node does the following:
- It increments its own heartbeat counter.
- It picks a few random peers.
- It sends them its table.
- Each peer merges the table into its own, keeping the higher counter for every node.
Information spreads like a rumor. With N nodes, a new fact reaches every node in roughly O(log N) rounds. No node is special, so no single failure stops the spread.
Detecting failures
A node cannot tell the difference between a crashed peer and a slow network. It can only observe that a peer's heartbeat counter has stopped increasing. The simplest rule is a timeout: if a peer's counter has not increased for T seconds, mark it suspected.
import time
class Membership:
def __init__(self, self_id, timeout=5.0):
self.self_id = self_id
self.timeout = timeout
self.table = {self_id: (0, time.monotonic())} # node -> (heartbeat, last_update)
def tick(self):
beat, _ = self.table[self.self_id]
self.table[self.self_id] = (beat + 1, time.monotonic())
def merge(self, remote):
"""remote maps node -> heartbeat counter."""
now = time.monotonic()
for node, beat in remote.items():
local_beat = self.table.get(node, (-1, 0))[0]
if beat > local_beat:
self.table[node] = (beat, now)
def suspected(self):
now = time.monotonic()
return [n for n, (_, seen) in self.table.items()
if n != self.self_id and now - seen > self.timeout]
def snapshot(self):
return {n: beat for n, (beat, _) in self.table.items()}
a, b = Membership("a"), Membership("b")
a.tick()
b.merge(a.snapshot())
print(b.snapshot()) # {'b': 0, 'a': 1}The code stores the time the node last saw a counter increase, using its own clock. It never compares clocks between machines, which avoids clock-skew problems.
Common errors
- Fixed timeouts that are too tight. A garbage-collection pause or a busy network triggers false suspicion. Too long a timeout delays detection. Production systems often use an accrual detector such as phi accrual, which outputs a suspicion level based on the observed spread of heartbeat arrival times.
- Treating suspicion as permanent. Suspicion is local and reversible. If a higher counter arrives later, clear the suspicion.
- Confusing temporary and permanent removal. A slow node should not be removed from the ring, because that would trigger large data movement. Removal should be an explicit operator action. Short outages are handled by hinted handoff, covered in a later chapter.
- No seed nodes. A new node needs at least one known address to contact. Configure a few stable seed nodes.
Trade-offs
| Choice | Benefit | Cost |
|---|---|---|
| Faster gossip interval | Quicker detection | More network traffic |
| Larger fan-out | Faster spread | More messages per round |
| Shorter timeout | Quick failover | More false positives |
Exercise
A node's heartbeat counter has not increased in your table for 8 seconds, and your timeout is 5 seconds. Should you remove the node from the ring? Explain.
Show answer
No. Mark it as suspected and stop sending it requests if appropriate, but do not remove it from the ring. The silence may come from a slow network or a pause, and removal would trigger expensive data movement. If a higher counter arrives later, clear the suspicion. Permanent removal is an explicit operator decision.