Course contents

05. Distributed Databases Lesson 6 of 10

Leaderless Replication

Dynamo-style replication: writes go to every replica, survive failures without failover, and converge through hinted handoff, read repair and anti-entropy.

14 minAdvanced8 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • Leaderless stores send every write to all N replicas in parallel; any replica accepts writes.
  • A write succeeds once W replicas confirm, so a failed replica needs no failover, but it falls behind.
  • Hinted handoff parks writes for a down replica on another node and delivers them when it returns.
  • Read repair fixes stale replicas a read touches; Merkle-tree anti-entropy finds and copies differences in the background.
  • Without a leader nothing orders writes: concurrent writes become siblings to merge, or LWW drops one.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

Leaderless replication drops the idea of a leader altogether. Each key is stored on N replicas (chosen by a consistent-hash ring), and any of them accepts reads and writes. A coordinator, either the client's driver or whichever node received the request, sends each write to all N replicas in parallel and each read to several of them. The design comes from Amazon's 2007 Dynamo paper and is used by Cassandra, ScyllaDB and Riak.

Why it exists

Leader-based systems have a special node: when it fails, writes stop until failover elects or promotes a new one, which takes seconds to minutes and can lose or duplicate writes if done carelessly. Leaderless replication has no such node. A replica failure is routine: writes go to the others and succeed as long as enough replicas answer. It was built for "always writable" workloads such as shopping carts.

How it works

Writes go everywhere. The coordinator sends the write to all N replicas and reports success once W have confirmed (the quorum lesson covers choosing W and R). Each value carries a version (a timestamp or a version vector) so that replicas and readers can tell newer from older.

A down replica falls behind. With N = 3 and W = 2, a write succeeds while one replica is down, but that replica misses it. When it returns it holds old values. Three mechanisms catch it up, each with a different scope.

Hinted handoff. While a replica is down, the coordinator sends its copy to another healthy node together with a hint naming the intended replica. That node stores hints separately and delivers them when the replica returns, then deletes them. It is fast and cheap, but best-effort: hints are kept for a limited window (Cassandra's max_hint_window defaults to three hours), and they are lost if the node holding them fails.

Read repair. A read asks several replicas and compares versions. The coordinator returns the newest value and writes it back to any replica that answered with an older one. It costs almost nothing extra, but only repairs keys that are read; rarely read keys can stay stale indefinitely.

Anti-entropy with Merkle trees. A background process compares replicas pairwise. Each replica builds a Merkle tree over its key ranges: leaves hash the data in a small range, every parent hashes its children. Two replicas exchange root hashes; if they match, the whole range is identical. If not, they compare children and descend only into ranges that differ, so a handful of round trips locate the few keys that need copying. In Cassandra this is nodetool repair (or managed repair tooling), run regularly so replicas converge within a bounded time.

Concurrent writes. No leader orders writes. Two clients can write the same key at the same time through different coordinators, and both writes can reach a quorum. Version vectors (or dotted version vectors, as in Riak) record which versions each write was based on, which distinguishes "newer" from "concurrent". For concurrent writes the store either keeps both as siblings for the application to merge on the next read (Riak), or keeps the one with the highest timestamp (last write wins, Cassandra's per-column rule) and silently drops the other. CRDT data types merge automatically.

When to use it

  • High write availability matters more than strict ordering: carts, sessions, user activity, IoT telemetry, messaging inboxes.
  • Large, write-heavy datasets spread across many nodes, where per-node failure is constant and routine.
  • Multi-data-centre deployments with local quorums per data centre and asynchronous replication between them.

When not to

  • Operations that need one winner: uniqueness, compare-and-set, counters with limits, financial ledgers. Use a leader or consensus (Cassandra's lightweight transactions use Paxos for this, at a latency cost).
  • Workloads built on multi-row transactions and joins, which leaderless stores generally don't provide.
  • Teams that can't run repair. Without regular anti-entropy, replicas drift and deleted data can reappear.

Trade-offs

Mechanism Fixes Misses Cost
Hinted handoff Writes during short outages Outages longer than the hint window; hints on a node that also fails Hint storage on other nodes
Read repair Stale replicas a read touches Keys that are never or rarely read A little extra work on reads
Anti-entropy (Merkle trees) Every difference, eventually Nothing, if run often enough Background CPU, disk and network; must be scheduled
Siblings + merge Concurrent writes without loss — Every reader handles conflicts
Last write wins Concurrent writes, simply All but one concurrent write; clock skew picks Silent data loss

Alternatives

  • Single-leader replication: a single order of writes and simple reads, with failover as the price.
  • Multi-leader replication: a leader per region; similar conflict handling, fewer nodes accept writes.
  • Consensus-replicated stores (etcd, CockroachDB, Spanner): majorities with a leader and a log, giving linearizable operations.

Failure modes

  • Silent data loss under LWW for concurrent writes, and when a node's clock runs ahead.
  • Resurrected deletes. A delete is stored as a tombstone; if a replica misses it and tombstones are purged before repair reaches that replica, the old value comes back. Cassandra's gc_grace_seconds (10 days by default) exists so that repair runs before tombstones are dropped.
  • Hint backlogs after long outages, which then flood the returning node.
  • Repair storms: an overdue full repair saturating disks and network.
  • Stale reads when W + R ≤ N, or with sloppy quorums, until the catch-up mechanisms finish.

In production

Cassandra and ScyllaDB clusters run with replication factor 3 per data centre and LOCAL_QUORUM for most reads and writes. Operators watch hint queues, schedule incremental or full repairs well inside gc_grace_seconds, and treat a node that was down longer than the hint window as needing repair before it serves reads. Data models avoid read-modify-write on the same key from several clients, or use CRDT-style types (counters, sets) where available.

Common misconceptions

  • "No leader means no consistency." Consistency is tunable per request through W and R; with W + R > N reads overlap the latest acknowledged write.
  • "Hinted handoff makes failures invisible." Hints are temporary and best-effort; repair is still required.
  • "Read repair keeps all data fresh." Only the data that is read.
  • "Replicas converge on their own." They converge because handoff, read repair and anti-entropy run; turn them off and they drift.

Interview angle

Choose leaderless storage when the design needs writes that survive node failures without failover, and say how it converges: "RF 3, LOCAL_QUORUM; a failed node is caught up by hinted handoff for short outages and scheduled repair for long ones; reads repair what they see." Then address concurrent writes (version vectors and merges, or LWW and its losses) and name what needs a different tool (uniqueness: lightweight transactions or a separate strongly consistent store).

Model assumptions

This lesson is a scripted illustration, not a simulation: the queueing engine has no leaderless mode (no client fan-out to several databases, no versions, hints or Merkle trees), so every write, acknowledgement, hint, repair and hash exchange is a scripted packet, and replica contents are shown as annotations. One-way times are illustrative: 10 ms between the coordinator and each replica, 12 ms to Node D and between replicas, and 1 ms for a replica to answer. Real intra-data-centre hops are usually well under a millisecond; the larger numbers make the order of events visible. There is no sandbox for this lesson for the same reason.

The lesson as textEvery step of the simulation, written out.
  1. 1. Every replica accepts writes

    In leaderless replication (the design of Amazon’s Dynamo paper, used by Cassandra, ScyllaDB and Riak) there is no leader. Each key lives on N = 3 replicas, and a write goes to all three in parallel. Every replica accepts it on its own.

    Who sends it? A coordinator: the client’s driver or whichever database node received the request. It waits for enough acknowledgements (W of them), then reports success. All packets in this lesson are a scripted illustration; the simulation does not model leaderless stores.

    Scene: Scripted: the coordinator sends write v1 to Replicas A, B and C at the same time, and all three acknowledge it.

    Key idea: Leaderless replication sends each write to all N replicas at once; there is no single node that orders writes.

  2. 2. A replica is down: the write still succeeds

    Replica C crashes. The coordinator still sends v2 to all three; A and B acknowledge, C never does. With W = 2 two acknowledgements are enough, so the write succeeds. There is no failover, no leader election, no pause: that is leaderless replication’s main strength. (How W and R are chosen is the subject of the quorum lesson.)

    But C now misses v2. When it comes back it will serve stale data unless something brings it up to date. The next steps are the three mechanisms that do.

    Scene: Scripted: Replica C is down. The coordinator sends v2 to all three replicas; A and B acknowledge, so the write succeeds, while C misses v2.

    Key idea: Leaderless writes survive replica failures without failover, but the failed replica falls behind.

  3. 3. Hinted handoff: hold it for C

    Next write, v3, with C still down. This time the coordinator also sends C’s copy to Node D, a healthy node that is not one of the key’s replicas, together with a hint: “this belongs to Replica C”.

    D stores the hinted write separately and keeps checking whether C is back. The write is now on three machines even though one replica is down.

    Hints are a stopgap: they are kept for a limited window (Cassandra’s default is three hours) and are lost if D fails too.

    Scene: Scripted: with C down, the coordinator writes v3 to A and B and sends C’s copy to Node D with a hint. All three acknowledge. Nothing is sent to C.

    Key idea: Hinted handoff parks a down replica’s writes on another node until the replica returns.

  4. 4. C returns and gets its writes

    Replica C comes back. Node D notices and hands off the hinted write: it sends v3 to C, C acknowledges, and D deletes the hint.

    C is current again without anyone scanning its data. Hinted handoff is cheap and quick, but only covers writes made while a hint was being kept: if the outage outlasts the hint window, or D lost its hints, C still has holes.

    Scene: Scripted: Replica C is back. Node D sends it the hinted write v3, C acknowledges, and D deletes the hint.

    Key idea: Hinted handoff repairs short outages quickly, but it is best-effort.

  5. 5. Read repair: fix what a read finds

    A later write, v4, reached only A and B: C was briefly unreachable and no hint survived. Now a client reads the key. The coordinator asks all three replicas.

    A and B answer v4, C answers v3. Every value carries a version, so the coordinator returns the newest, v4, and then sends v4 to C: read repair.

    Read repair costs almost nothing extra, but it only fixes keys that are read. A key nobody reads can stay stale on C indefinitely.

    Scene: Scripted: a read goes to A, B and C. A and B return v4 and C returns v3. The coordinator answers with v4 and sends v4 to C to repair it.

    Key idea: Read repair compares versions on every read and writes the newest value back to stale replicas.

  6. 6. Anti-entropy: compare trees, copy differences

    For data that is rarely read, a background anti-entropy process compares replicas. Comparing every key would be far too expensive, so each replica keeps a Merkle tree: a tree of hashes where each parent hashes its children and each leaf covers a range of keys.

    A and C exchange their root hashes (the token packets). They differ, so they compare the children, and descend only into ranges whose hashes differ. A few round trips find the one range with missing keys, and only those keys are copied (the replication packet). In Cassandra this is nodetool repair, typically scheduled regularly.

    Scene: Scripted: Replicas A and C exchange Merkle tree hashes: roots first, then children. They find the one key range that differs, and A copies just those keys to C.

    Key idea: Merkle trees let replicas find their differences by exchanging a few hashes, then copy only what differs.

  7. 7. Two writers, no one to order them

    Without a leader nothing orders writes. Two clients update the same cart at the same moment: one writes x through A and B, the other y through B and C. Both get W = 2 acknowledgements.

    Replica B received both. Each write carries a version vector that records which versions it was based on; neither x nor y saw the other, so they are concurrent. B keeps both as siblings, and the next read returns both for the application to merge (Riak’s model). Cassandra instead keeps the value with the latest timestamp per column, which silently drops one of them.

    The resolution options (last write wins, merges, CRDTs) are the same as in multi-leader replication.

    Scene: Scripted: Client 1 writes x to A and B while Client 2 writes y to B and C. Both writes succeed. B holds x and y as concurrent siblings.

    Key idea: Concurrent writes in a leaderless store need versioning: either keep siblings and merge, or accept last-write-wins data loss.

  8. 8. Leaderless in one picture

    Writes go to every replica; a write succeeds once W of them confirm, so a failed replica needs no failover. Replicas that missed writes are caught up three ways: hinted handoff (fast, for short outages), read repair (on the read path, for keys that are read) and anti-entropy with Merkle trees (in the background, for everything).

    The price is ordering: concurrent writes need version vectors and a merge rule, and consistency depends on how you choose W and R.

    Scene: Three replicas and Node D, all healthy, with the coordinator connected to every replica. No packets are sent.

    Key idea: Leaderless replication trades a single source of order for availability, and relies on handoff, read repair and anti-entropy to converge.