Aller au contenu principal

Repair on the Read Path: Fixing the Staleness You Already Found

· 10 minutes de lecture
Clustron Team
Ingénierie des systèmes distribués

A quorum read does more work than it lets on. To answer a single read it contacts several replicas, waits for enough of them to respond, compares the versions they return, and hands back the newest. Buried in that last step is a fact the read rarely bothers to keep: having compared the versions, it knows precisely which replicas were stale and by how much. A naive implementation then discards that knowledge — it returns the fresh value to the client and forgets that it just caught a replica three versions behind.

That discarded knowledge is the cheapest repair opportunity the system will ever get. The read has already paid for the expensive parts: the network round-trips to multiple replicas, and the version comparison that identified the laggard. The stale replica is named, the correct value is already in hand, and the only remaining step is to write it back. Everything costly about a repair — finding the disagreement — has already happened as a side effect of answering the read.

Read repair is simply the decision not to waste that. When a read discovers a replica is behind, it pushes the winning value to that replica before, or just after, returning to the client. It is the natural partner of the quorum and the complement of background anti-entropy: the quorum guarantees a read finds the fresh value among its responses, and read repair uses that same moment to heal the replicas that were behind — on the hot path, for almost nothing. This post is about how it works, the blocking-versus-asynchronous trade-off, the digest-read trick that keeps it cheap, and the crucial reason it can never stand alone.

The read already knows who is stale​

Replay a quorum read. The coordinator gathers responses from R replicas, each carrying a value and a version — a per-key counter or a hybrid logical clock. It selects the response with the highest version and returns its value; that is the ordinary quorum guarantee at work. But the selection is a comparison, and the comparison has a by-product: every response below the maximum came from a replica that is stale, by exactly the version gap between it and the winner.

Read repair acts on that by-product. The set of stale replicas is already computed; the winning value is already loaded. The repair is one write — the winning value and its version — sent to each lagging replica, which applies it under the same last-write-wins rule that settles every conflict in the system, because the repair value's version is strictly newer than what the replica holds. No new reads, no Merkle tree, no coordination: the read turned itself into a repair with one extra write per laggard.

A quorum read already caught R3 behind — heal it on the way outR1 · v5R2 · v5R3 · v3currentcurrentstale — missed a writeread coordinatornewest version = v5client receives v5 ✓read repair: write v5 → R3the versions were compared to answer the read anyway, so the repair is nearly free — but cold keys that are never read still need anti-entropy

Blocking or asynchronous​

There is one decision to make: whether the repair happens before or after the client gets its answer.

Asynchronous read repair returns the winning value to the client immediately and fires the repair writes in the background. The client pays nothing for the healing, and the stale replica converges a moment later. The weakness is a small window: a follow-up read that lands on the not-yet-repaired replica, with a small R, can still see the old value until the in-flight repair completes. For most caches and data stores this is the right default — the latency win is real and the convergence lag is milliseconds.

Blocking read repair waits for the repair writes to be acknowledged before returning to the client. It costs the client that extra round-trip, but it buys a guarantee: once the client holds its answer, the replicas that were behind are already fixed, which strengthens read-your-writes and monotonic-read consistency because a subsequent read cannot slip back to a stale copy that the first read already touched. Systems that offer both typically default to async and switch to blocking for reads that have asked for stronger consistency.

Keep the healthy case cheap: digest reads​

A naive quorum read fetches the full value from every one of its R replicas, which is wasteful when the values are large and — the common case — all the replicas agree. The fix is the digest read: fetch the full value from one replica and only a digest, a hash of value-and-version, from the rest. Compare the digests against the full response. If they all match, there is no staleness, the read returns, and not a single byte of repair traffic moves. Only when a digest disagrees does the coordinator go back for the full value from the mismatching replicas, resolve by version, and repair.

// A quorum read: full value from one replica, digests from the rest.
var responses = await ReadQuorum(key, required: R);
var winner = responses.MaxBy(r => r.Version);

var stale = responses.Where(r => r.Version < winner.Version).ToList();
if (stale.Count == 0) return winner.Value; // digests agreed — zero repair, zero extra bytes

foreach (var r in stale) // push the winning value to each laggard
_ = RepairAsync(r.Replica, key, winner); // async: do not block the client
// (blocking variant: await these before returning)
return winner.Value;

The effect is that the mechanism is nearly free precisely when it is doing nothing useful. Agreement costs one full value plus a few cheap hashes; only genuine disagreement — the case you actually want to catch — pays the price of shipping whole values around.

Necessary, but not sufficient​

Read repair has one structural blind spot, and it is important: it only ever touches keys that are read. A hot key served thousands of times a second is reconciled constantly and can barely stay stale for long. But a key that is written, goes stale on one replica because that replica missed the replication, and is then never read again, will never be repaired by this mechanism — the trigger simply never fires. Staleness on cold data can sit there indefinitely, and the longer it sits, the more dangerous it is: the drifted replica may later be chosen to serve some other operation, or become the surviving copy after a failure, promoting an old value to the truth.

That gap is exactly what background anti-entropy fills. A scheduled Merkle-tree sweep reconciles every key whether or not anyone has read it, catching the cold drift the read path never visits. The two mechanisms are complementary by design: read repair is opportunistic and data-driven, healing the working set the instant you touch it at almost no cost; anti-entropy is exhaustive and time-driven, bounding staleness for everything else. Run read repair alone and your cold data rots quietly; run anti-entropy alone and you pay a full sweep's cost to fix hot keys that a read would have repaired for free. You want both.

Wrap-up​

A quorum read already contacts multiple replicas and compares their versions, so it already knows which replicas are behind — read repair is nothing more than the decision to act on that knowledge instead of discarding it, writing the winning value back to the laggards it just identified. Asynchronous repair keeps the read fast and converges in the background; blocking repair spends a round-trip to guarantee the fix lands before the client sees its answer, strengthening monotonic and read-your-writes guarantees. Digest reads keep the whole thing cheap when nothing is wrong, comparing hashes before shipping values so only real disagreement pays full freight. But read repair heals only what is read, so it must be paired with background anti-entropy to catch the cold keys the read path never reaches. Fix what is hot the moment you touch it; sweep the rest on a schedule — together they are the two halves of staleness control.