Sub topic 16
Testing with failure injection and operating the cluster
This chapter assumes you know the pieces built earlier: the log-structured storage engine, consistent hashing, N/R/W quorums, vector clocks, hinted handoff, and Merkle-tree anti-entropy. Here you check that they work together under failure, and you learn what to watch once the cluster is running.
Why normal tests are not enough
Unit tests show that each component works when everything else is healthy. Most distributed bugs appear only when something fails at an awkward moment. A node may crash after the write-ahead log (WAL) append but before the reply. A network partition may split the preference list. A clock may jump. Failure injection means causing these faults on purpose, in a controlled way, and then checking invariants.
Choose invariants first
Before injecting anything, write down what must stay true. For a Dynamo-style store with R + W > N, examples are:
- A write acknowledged by W replicas is visible to a later read that reaches R replicas, unless a newer write exists.
- Concurrent writes are never silently dropped. They appear as siblings or are resolved by the configured policy.
- After faults stop and anti-entropy finishes, all replicas of a key hold the same value.
- No acknowledged write is lost after a single-node crash and restart, because the WAL is replayed.
A fault catalogue
| Fault | How to inject | What it tests |
|---|---|---|
| Node crash | Kill the process with SIGKILL | WAL replay, hinted handoff |
| Slow node | Add network delay or pause the process | Failure detector, timeouts, tail latency |
| Network partition | Block traffic between node groups | Sloppy quorums, sibling creation |
| Message loss or reordering | Drop or delay RPCs | Retries, idempotent writes |
| Disk full or corrupt | Fill the volume, flip bytes in an SSTable | Error handling, checksums |
| Clock skew | Shift one node's clock | Last-write-wins hazards |
A minimal test harness
The sketch below simulates three replicas, drops writes to one of them, and checks the quorum invariant. It runs as shown.
import random
class Replica:
def __init__(self):
self.data = {}
self.up = True
def put(replicas, key, value, version, w):
acks = 0
for r in replicas:
if r.up:
r.data[key] = (version, value)
acks += 1
return acks >= w
def get(replicas, key, r_count):
answers = [r.data.get(key) for r in replicas if r.up][:r_count]
answers = [a for a in answers if a is not None]
return max(answers) if answers else None
random.seed(7)
n, r_count, w = 3, 2, 2
for trial in range(1000):
nodes = [Replica() for _ in range(n)]
nodes[random.randrange(n)].up = False # inject one crash
assert put(nodes, "k", "v1", 1, w)
for node in nodes: # a different node fails
node.up = True
nodes[random.randrange(n)].up = False
result = get(nodes, "k", r_count)
assert result is not None and result[1] == "v1", trial
print("invariant held in 1000 trials")Running many randomized trials with a fixed seed makes failures reproducible. Production tools follow the same idea at larger scale: Jepsen checks recorded operation histories against a consistency model, and chaos tools inject faults into real clusters.
Operating the cluster
Run the store with these practices:
- Monitor per-node latency percentiles (p50, p99), error rates, hint queue size, compaction backlog, and disk usage.
- Alert on divergence, such as long-running anti-entropy repairs or growing hint queues. They often mean a node is down or slow.
- Add and remove nodes one at a time. Streaming data to a new node competes with live traffic, so throttle it.
- Roll upgrades node by node. Wait for gossip to mark the node healthy and for hints to drain before moving on.
- Back up SSTable snapshots, and practice restoring them.
Exercise
A test kills one of three replicas (N=3, W=2, R=2), writes a key, restarts that node, then immediately kills a different replica and reads the key. Which mechanism lets the read still return the latest value, and what could still go wrong?
Show answer
The write reached two replicas, and any two-replica read quorum overlaps with them in at least one node, since R + W > N. Hinted handoff or read repair will bring the restarted node up to date. The remaining risk is that the one live replica holding the write is the only survivor, so the read may reach only one replica and fail to meet R=2, or the one holding the write may be unavailable. In that case the read returns an error or stale data, depending on the configuration.