Implementing Anti-Entropy and Repair

1. Understanding Anti-Entropy Mechanisms

MechanismWhenCost
Read repairOn read divergence detectedLazy; bounded to active keys
Hinted handoffReplica temporarily downBuffered writes replayed
Background repairScheduled scanFull data sweep, expensive
Merkle-tree syncPeriodic comparisonO(log n) tree traversal

2. Implementing Read Repair

StepAction
1Coordinator queries R replicas
2Detect divergent versions (timestamp / vector clock)
3Return latest to client
4Async write latest to stale replicas

3. Implementing Write Repair

PatternDetail
DefinitionCoordinator writes to all replicas; failed writes hinted
Async replication tailBackground catches up missing writes
Combined with hinted handoffStandard Cassandra/Dynamo flow

4. Implementing Merkle Trees for Synchronization

PropertyValue
StructureBinary tree; leaves = chunk hashes; internal = hash of children
Compare costO(log n) per diff; O(diff) bytes transferred
Used byCassandra, Dynamo, Git, BitTorrent

Example: Merkle tree leaf hash

byte[] leafHash(byte[] key, byte[] value) {
    return Hashing.sha256()
        .newHasher()
        .putBytes(key).putBytes(value)
        .hash().asBytes();
}

byte[] internalHash(byte[] left, byte[] right) {
    return Hashing.sha256()
        .newHasher()
        .putBytes(left).putBytes(right)
        .hash().asBytes();
}

5. Implementing Hinted Handoff

StepAction
1Coordinator writes to replicas; one is down
2Store hint (target, key, value, ts) on alive replica
3Periodically retry forwarding hint to recovered node
4Bound TTL (Cassandra default 3h) to avoid unbounded growth

6. Understanding Repair Scheduling Strategies

StrategyFrequencyImpact
Full repairWeekly or after partitionHigh I/O
IncrementalContinuousLower per-cycle cost
SubrangeSlice token rangeBounded duration
Reaper-style orchestrationCoordinated across clusterAvoids overlap

7. Implementing Background Repair Jobs

AspectDetail
ThrottlingLimit MB/s and concurrent ranges
Off-peak windowsSchedule during low traffic
Idempotent restartsResume from checkpoint
ToolsCassandra Reaper, Yugabyte Anywhere

8. Handling Inconsistency Detection

MethodUse
Checksum / digest readCheap divergence check before full read
Merkle tree diffBulk reconciliation
Vector-clock compareIdentify causally divergent versions
Audit jobsPeriodic full-row checksums

9. Implementing Full vs Incremental Repair

ModeScopeWhen
FullEntire dataset re-validatedAfter major outage; periodic safety net
IncrementalOnly un-repaired SSTablesRoutine; faster cycles
CombinationIncremental + occasional fullProduction best practice

10. Understanding Repair Trade-offs

FactorTrade
More frequent repairLower divergence; higher cost
Wider parallelismFaster completion; more I/O contention
Stronger read consistencyReduces need for repair; higher read latency