Sub topic 15
Testing consistency claims in practice
Prerequisites: you know linearizability, eventual consistency, and quorums from earlier chapters, and you can read simple Python.
A database's documentation says "strongly consistent" or "tunable consistency". Those are claims about histories. You can test them by recording what clients did and checking the recording against a model.
What a test records
A history is a list of operations. Each entry has a client, an operation, the time it was invoked, the time it returned, and the result. Real-time bounds matter because linearizability depends on them.
Client A: write(x, 1) invoked t=0, returned t=2
Client B: read(x) invoked t=3, returned 0Initial x is 0. B's read began after A's write returned, yet it saw the old value. No single ordering of these two operations respects real time and also makes the read legal, so the history is not linearizable.
A minimal checker
Real checkers search for a valid ordering. This sketch handles one register with sequential, non-overlapping operations. It checks only that each read returns the latest completed write.
def check_register(history, initial=0):
"""history: list of (kind, value, invoke, ret), kind is 'w' or 'r'.
Assumes operations do not overlap. Returns True if every read
returns the value of the latest write that returned before it began."""
current = initial
for kind, value, invoke, ret in sorted(history, key=lambda h: h[2]):
if kind == "w":
current = value
elif value != current:
return False
return True
stale = [("w", 1, 0, 2), ("r", 0, 3, 4)]
fresh = [("w", 1, 0, 2), ("r", 1, 3, 4)]
print(check_register(stale)) # False
print(check_register(fresh)) # TrueOverlapping operations make the problem harder. The checker must try orderings in which concurrent operations take effect at some point between invocation and return. This search is NP-complete in general. Tools such as Knossos and Porcupine use pruning to make it practical.
Fault injection
Consistency bugs rarely appear on a healthy network. A test run usually combines three parts:
- Concurrent clients issue reads, writes, and compare-and-set operations against the system.
- A fault injector creates partitions, pauses processes, skews clocks, and kills nodes.
- A checker analyzes the recorded history after the run.
Jepsen is the best-known framework for this approach. It has found violations in several systems that advertised strong guarantees.
Testing eventual consistency
Eventual consistency has no real-time bound, so the check differs. Stop writes, heal all faults, wait, then read from every replica. If replicas still disagree after a generous wait, convergence failed. Also record the time to converge, because "eventually" in production often needs a number.
Common errors
- Reading wall-clock times from different machines. Skewed clocks corrupt the invoke and return ordering. Take timestamps on one machine, or on the client.
- Treating a timeout as a failure. A timed-out write may still have been applied. Record it as indeterminate, and let the checker accept either outcome.
- Passing once. Races are probabilistic. Run many seeds with varied fault schedules.
- Concluding that no violation means correctness. A checker only shows that the histories it saw were valid.
Exercise
A client's write to a key times out. A later read by another client returns the new value. Should the checker treat the write as failed, and why?
Show answer
No. A timeout means the outcome is unknown, since the write may have been applied before the response was lost. The checker should treat the write as indeterminate, possibly completing at any later time, so the read of the new value is legal.