Sub topic 15
Testing consistency claims in practice
This chapter assumes you know the definitions of linearizability, sequential consistency, and eventual consistency from earlier chapters. It also assumes you can read short Python programs. Here you will learn how to check whether a real system keeps the promises in its documentation.
Why vendor claims need testing
A database's documentation may say "strongly consistent" or "tunable consistency". Those phrases are marketing unless you know the exact model, the exact configuration, and the failures it covers. Real systems have broken their stated guarantees because of bugs in leader election, clock handling, retry logic, and storage engines. Many of these bugs appear only during network partitions, process pauses, or node restarts.
Testing a consistency claim means answering one question: given what clients saw, is there any legal explanation under the claimed model? The test does not inspect the database's internals. It records what clients did and what they observed, then checks that record against the model.
The black-box approach
Recall that a consistency model is a contract on histories. A history is a list of operations. Each operation has a start time (the invocation), an end time (the completion), and a result. A black-box test works in four steps:
- Run several concurrent clients against the system and record every operation.
- Inject faults while the clients run.
- Stop the faults, wait for the system to settle, and read the final state.
- Run a checker over the recorded history.
Jepsen is the best-known framework built on this loop. Its fault injector is called a nemesis. Its linearizability checkers include Knossos, and Porcupine is a similar checker written in Go. Elle checks transactional isolation levels from histories. You can use these tools directly, but the idea is simple enough to build in miniature.
A small linearizability checker
Take a single register, a variable that supports write and read. A history is linearizable if you can place each operation at one instant between its start and end, so that the resulting sequence is a legal run of a single copy.
The checker below searches for such a sequence. At each step it picks an operation that no other remaining operation strictly precedes in real time. A write sets the state. A read must return the current state.
def linearizable(history, initial=0):
"""Brute-force check of a single-register history.
Each operation is a dict with keys: kind ("read" or "write"),
value, start, end. Suitable only for small histories.
"""
def search(remaining, state):
if not remaining:
return True
for op in remaining:
# op cannot go next if another remaining op finished before it started
if any(o["end"] < op["start"] for o in remaining if o is not op):
continue
rest = [o for o in remaining if o is not op]
if op["kind"] == "write":
if search(rest, op["value"]):
return True
elif op["value"] == state:
if search(rest, state):
return True
return False
return search(list(history), initial)
stale = [
{"kind": "write", "value": 1, "start": 0, "end": 2},
{"kind": "read", "value": 0, "start": 3, "end": 4},
]
overlap = [
{"kind": "write", "value": 1, "start": 0, "end": 2},
{"kind": "read", "value": 0, "start": 1, "end": 4},
]
print(linearizable(stale)) # False: the read began after the write completed
print(linearizable(overlap)) # True: the read overlapped the write, so it may be ordered firstThe first history is the one from the chapter on linearizability. The read started at time 3, after the write finished at time 2, yet it returned the old value. No legal ordering exists, so the checker returns False. In the second history the read overlaps the write, so placing the read first is legal.
The brute-force search takes exponential time in the worst case. Checking linearizability is NP-complete in general. Production checkers prune the search with memoization and by splitting histories by key, because operations on different keys are independent for a per-key register model. Even so, you must keep test histories short, often a few hundred operations per key, and run many such tests rather than one long one.
Handling operations with unknown outcomes
Real clients time out. If a write times out, you do not know whether it took effect. It may have been applied immediately, applied later, or never applied. Treating a timeout as a failure produces false alarms, because a later read may legitimately see the "failed" write.
The standard fix is to record a timed-out operation as indeterminate. Give it an end time of infinity, so it may be linearized at any point after its start, or dropped altogether. A checker must allow both possibilities. The toy checker above does not model dropped operations, so extend it before you use it on a system with timeouts.
Choosing faults to inject
A system can pass every test in a healthy cluster and fail the first time a node is paused. Useful faults include:
| Fault | What it exposes |
|---|---|
| Network partition between replicas | Split-brain writes, stale reads from the minority side |
| Partition that isolates the current leader | Two leaders serving writes or reads at once |
| Process pause (SIGSTOP, long garbage collection) | Leases that expire while a node still believes it is leader |
| Clock skew or clock jumps | Last-write-wins losing newer data, lease logic that trusts wall clocks |
| Crash and restart with data on disk | Acknowledged writes that were never flushed |
| Slow or lossy links | Timeout handling and retry bugs |
Combine faults with a workload that creates contention. Many clients writing and reading the same few keys will expose problems that spread-out traffic never reaches.
Checking weaker models
Linearizability has the sharpest checker, but you will often test weaker claims.
- Eventual consistency. Stop the writes and the faults, wait, then read every key from every replica. All replicas should return the same value, and that value should be one that some client wrote. A system that never converges, or that converges to a value nobody wrote, has a bug. Also record how long convergence takes, since "eventually" with no bound is hard to rely on.
- Session guarantees. Each client can check its own history. For read-your-writes, a client that wrote value
vto a key must never later read a value older thanv. For monotonic reads, successive reads of one key by one client must never go backward in version order. This requires unique, ordered values such as an incrementing counter per key. - Causal consistency. Record which operations a client had observed before each write. Check that no replica exposes an effect before its causes.
- Transactions. Tools such as Elle infer dependency cycles between transactions and report which isolation level the history violates.
The design of the workload matters as much as the checker. If every write stores the same value, a stale read cannot be told from a fresh one. Use unique values, for example (client_id, sequence_number), so each read identifies exactly which write it saw.
Reading the result
A passing test means only that this run found no violation. It does not prove the system is correct. A failing test is stronger evidence, because a counterexample is a concrete history you can replay and show to the database's authors.
Check that your test can fail. Run the checker against a deliberately broken setup, such as a configuration with R + W <= N or reads served from a lagging follower. If the test passes on a system you know is broken, the workload or the fault schedule is too weak.
Common mistakes
- Using wall-clock timestamps from different machines as operation times. Clock skew can invent or hide violations. Record start and end times at one observer, usually the test controller.
- Testing only a healthy cluster.
- Testing the wrong claim. A system may be linearizable for single-key reads but only snapshot-isolated for transactions.
- Treating a lack of failures in a short run as a guarantee.
Exercise
A client writes the value 5 to key k at times 0 to 2, and the write is acknowledged. Another client starts a read of k at time 4 and gets back the previous value 3. No other operations occur. Is the history linearizable? What changes if the write timed out instead of being acknowledged, and its end time is treated as infinity?
Show answer
With the acknowledged write, the history is not linearizable. The write completed at time 2, before the read started at time 4, so the write must come first in any legal order, and the read must return 5. With the write recorded as indeterminate (end time infinity), the history is linearizable. The write may be linearized after the read, or never take effect, so the read of 3 is legal.
Exercise
Why should a consistency test use unique values for writes rather than repeating the value 1?
Show answer
With repeated values, a read that returns 1 could have seen any of the writes, so the checker cannot tell a stale read from a fresh one. Unique values let each read identify exactly which write it observed, which makes both the check and the counterexample precise.