Course contents

05. Distributed Databases Lesson 5 of 10

Multi-Leader Replication

Run a leader in each region: watch local writes get fast, regions see each other late, concurrent writes conflict, and LWW or CRDTs resolve them.

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

Summary

  • Multi-leader replication lets several nodes (usually one per region) accept writes and replicate them to each other asynchronously.
  • Local writes are fast and every region stays writable during a partition.
  • Regions see each other’s writes late, so reads across regions can be stale.
  • Two leaders can accept conflicting writes to the same key; without a resolution rule replicas diverge.
  • Last write wins converges by discarding writes; CRDTs and application merges converge without losing them.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

In multi-leader replication (also called multi-master or active–active) more than one node accepts writes. Each leader applies writes from its own clients and forwards them to the other leaders, which apply them as if they were followers. The typical layout is one leader per region or data centre; other forms are offline-first apps (every device is a leader that syncs later) and collaborative editors (every open document is a replica that accepts edits).

Why it exists

With a single leader, every write has to reach that one node. For users in another region that means a cross-region round trip on every write, often 100–200 ms or more, and if the link between regions breaks, writes from the far side fail. Read replicas in the far region help reads, not writes.

A leader in each region removes both problems: writes are acknowledged locally, and each region can keep working when it cannot reach the others.

How it works

Local writes, asynchronous replication. A leader commits a client's write locally and answers. It then ships the change to the other leaders, usually asynchronously; waiting for a remote leader would bring back the latency that multi-leader is meant to remove. In the lesson the link is 70 ms one way, and the other region sees each write about 70 ms after it was acknowledged.

Eventual consistency across regions. Until a change arrives, the other region serves the old value. Within a region things look normal; across regions there is no point in time at which all leaders agree, only convergence once writes stop.

Conflicts. Two leaders can accept writes to the same key before either has seen the other's. Such writes are concurrent: neither happened after the other in any causal sense. If each leader simply applies writes in arrival order, the leaders end with different values and stay that way.

Conflict resolution. Every multi-leader system needs a rule that makes all replicas end up with the same value:

  • Last write wins (LWW). Each write carries a timestamp; the highest wins everywhere. Simple and convergent, but every other concurrent write is discarded even though its client was told it succeeded. Timestamps come from clocks on different machines, so skew decides which write survives. (Hybrid logical clocks reduce, but do not remove, that arbitrariness.)
  • CRDTs. Conflict-free replicated data types (counters, sets, maps, registers, sequences for text) define a merge that is commutative, associative and idempotent, so replicas that have seen the same updates converge regardless of order, and no update is lost. A shopping cart as an observed-remove set keeps both "book" and "lamp" when two regions add them at once.
  • Application merge or siblings. The database keeps all concurrent versions and hands them to the application (or user) on the next read. Riak's siblings and CouchDB's conflict revisions work this way.
  • Custom resolvers. Some databases run a resolver function when they detect a conflict (for example a stored procedure in Azure Cosmos DB, whose default policy is LWW on a timestamp).

Topology. With more than two leaders, changes travel all-to-all, in a ring or a star. Ring and star topologies need loop prevention (each change carries the IDs of nodes it has passed) and break if one node fails; all-to-all can deliver changes out of causal order, which version vectors detect.

When to use it

  • Writes from several regions where local write latency matters and the data tolerates eventual consistency (user preferences, carts, content edits, social interactions).
  • Regions that must stay writable during a partition between them.
  • Offline-first and collaborative applications, where each device or session must accept edits without a network.

When not to

  • Data with invariants across records or replicas: balances that must not go negative, unique usernames, seat or stock reservations. Two leaders can each approve the last seat. Use a single leader (or consensus) for that data.
  • When one region does almost all writes. A single leader there plus read replicas elsewhere is simpler and loses nothing.
  • When the team cannot define a resolution rule for every kind of data. Multi-leader without a deliberate conflict strategy means LWW by default, and silent data loss.

Trade-offs

Choice You gain You pay
Single leader, replicas elsewhere No write conflicts, simple reasoning Cross-region latency on every remote write; remote writes fail during a partition
Multi-leader with LWW Local writes, always writable, convergence for free Concurrent writes silently lost; clock skew picks the survivor
Multi-leader with CRDTs Local writes, no lost updates Only for data that fits a CRDT; metadata overhead (tombstones, version vectors)
Multi-leader with siblings / app merge Nothing lost, merge logic of your choice Every reader must handle conflicts; merge code is easy to get wrong
Home region per record Local writes for most users, no conflicts Writes for a record from another region pay the trip to its home; rehoming is work

Alternatives

  • Single leader with read replicas (see database replication) when writes come mostly from one place.
  • Home region per record: every user or account has one region whose leader takes its writes. Most writes are local, and there is never more than one leader per record.
  • Leaderless replication with quorums (Dynamo-style) accepts writes on any replica, with the same need to version and merge concurrent writes.
  • Consensus-based distributed SQL (Spanner, CockroachDB, YugabyteDB): writes are linearizable across regions, at the cost of a cross-region round trip for each write to a majority.

Failure modes

  • Silent lost updates under LWW. Both users saw "saved"; one change is gone, with no error anywhere.
  • Clock skew. A node whose clock runs ahead wins every LWW conflict, and may even shadow later writes until real time catches up.
  • Divergence from missing resolution. Without a rule, or with a rule that is not deterministic on every replica, replicas disagree permanently.
  • Constraint violations. Unique indexes and foreign keys are checked per leader; two leaders can each insert the same "unique" value.
  • Backlog after a partition. Everything written during an outage must be shipped and resolved when it heals, a burst of replication traffic and conflicts at once.
  • Causality violations. In all-to-all topologies a follower can receive an update before the insert it depends on.

In production

Examples of multi-leader or multi-region-write systems: Azure Cosmos DB with multi-region writes, DynamoDB global tables (last writer wins in the multi-active mode; a strongly consistent multi-Region mode also exists), CouchDB/PouchDB sync, Redis Enterprise Active-Active (CRDT-based) and EDB Postgres Distributed (BDR). Collaborative editors use CRDT libraries such as Automerge or Yjs.

Teams that run it well decide the resolution per data type before launch, keep must-be-unique and must-not-overdraw data on a single leader, monitor replication lag between regions, and test partitions deliberately to see how many conflicts a realistic outage produces.

Common misconceptions

  • "Multi-leader doubles write capacity." Every write is still applied on every leader. It reduces latency and adds availability, not throughput per replica.
  • "Conflicts are rare, so they don't matter." They are rare per key and certain at scale, and they cluster on the hottest keys during outages, exactly when you can least afford surprises.
  • "Last write wins means the newest write wins." It means the highest timestamp wins, from clocks that disagree.
  • "CRDTs solve every conflict." They merge data structures; they cannot enforce invariants such as "never below zero" without coordination.

Interview angle

Bring up multi-leader when a design needs low-latency writes in several regions or offline clients. Then address conflicts before the interviewer does: which data can be merged (counters, sets: CRDTs), which can tolerate LWW (last-seen timestamps), and which must stay on a single home region (payments, inventory, unique names). Mention that cross-region reads are eventually consistent and how you would hide that from users (read-your-writes by routing a user to their home region).

Model assumptions

The engine parts of this lesson are a simplified model, not a benchmark. Two regions are 70 ms apart one way; within a region a hop takes 2 ms. Each region's app tier sends 150 queries per second (Poisson arrivals), 30% writes, over the same 40 keys with a Zipf(1.0) popularity. Every database has 16 connections, a 10 ms mean read and a 20 ms mean write (lognormal, cv 0.4). With two leaders, each leader acknowledges a local write at once and ships it to the other over the 70 ms link (asynchronous replication in both directions); applying it costs nothing. A read counts as stale when the other region had already acknowledged a newer write for that key. The conflict, last-write-wins and CRDT steps are scripted illustrations: the engine tracks keys and times, not values, and does not detect or resolve conflicts. Also not modelled: shipping the backlog after a partition (the simulation simply resumes replication), clock skew, and more than two leaders. Numbers show behaviour and orders of magnitude only.

The lesson as textEvery step of the simulation, written out.
  1. 1. One leader, two continents

    The company serves users in Europe and the US from one leader database in the EU region. Each region’s app tier sends 150 queries per second; three in ten are writes.

    For EU users a query is a short local hop. Every US query crosses the ocean twice, 70 ms each way, so US users wait about 140 ms longer than EU users for the same work. Read replicas in the US would fix the reads, but every write would still have to travel to the one leader.

    Scene: One leader database in the EU region serves both regions. EU queries take only the query time (under 30 ms); every US query crosses a 70 ms link in each direction.

    Key idea: With a single leader, every write from a far region pays the full cross-region round trip.

  2. 2. A leader in each region

    We give the US its own leader. Each app tier now reads and writes only its local leader, and both regions answer in under 30 ms, the time of the query itself.

    The leaders keep each other up to date: every write a leader accepts is acknowledged to the client right away and then shipped asynchronously to the other region (the purple replication packets, both ways). Each leader is also a follower of the other.

    That is multi-leader (or active–active) replication. Its main uses: multi-region writes, offline-first clients (each device is a leader) and collaborative editing.

    Scene: Each region has its own leader. Both app tiers get answers in under 30 ms, and each leader ships its writes to the other over the 70 ms link.

    Key idea: Multi-leader replication lets every region accept writes locally; leaders replicate to each other asynchronously.

  3. 3. Each region sees the other late

    Asynchronous replication means each region sees the other’s writes about 70 ms late, at best. A US user updates a shared document, gets "saved", and a colleague in Europe reading it a moment later still sees the old version.

    The tile counts these stale reads: reads in one region of a key the other region had already acknowledged a newer write for. Both regions write the same hot keys, so here roughly one read in seven is stale. During a network problem between regions it would last for minutes, not milliseconds.

    Scene: Both leaders serve traffic and replicate to each other. A share of reads in each region miss a write the other region already acknowledged.

    Key idea: Multi-leader is eventually consistent across regions: a write is visible elsewhere only after replication.

  4. 4. Both regions change the same row

    Stale reads are the mild problem. The real one: both leaders can accept a write to the same key at nearly the same time. This step is a scripted illustration; the simulation does not track values.

    An editor in Europe renames a product to "Lamp"; 10 ms later a colleague in the US renames it to "Light". Each leader accepts its write and says "saved" within a few milliseconds, long before the other region’s write arrives. Neither write knew about the other: they are concurrent.

    If each leader simply applies writes in the order they arrive, Europe ends with "Light" and the US with "Lamp". The two copies disagree, forever.

    Scene: Scripted: the EU leader accepts name = Lamp and the US leader accepts name = Light 10 ms later. Both acknowledge locally, then each replicates to the other. Applied in arrival order, the EU ends with Light and the US with Lamp.

    Key idea: Two leaders can accept conflicting writes to the same key. Without a resolution rule, replicas diverge.

  5. 5. Last write wins, and loses data

    The simplest rule is last write wins (LWW): every write carries a timestamp, and each leader keeps the value with the highest one. The same packets as before, but now both leaders end with "Light" (written at 10 ms). The copies agree.

    The cost: "Lamp" was acknowledged to its user and is now silently discarded. And "last" is decided by clocks on different machines. If the EU clock ran 50 ms fast, "Lamp" would carry the later timestamp and win, although it was written first.

    LWW is fine when losing a concurrent update is acceptable (a cache, a "last seen" time). For data people edit, it quietly loses work.

    Scene: Scripted: the same two concurrent writes. With last write wins both leaders keep "Light", the write with the later timestamp; the acknowledged "Lamp" write is discarded.

    Key idea: LWW makes replicas converge by throwing away all but one concurrent write, chosen by possibly skewed clocks.

  6. 6. Merge instead of pick

    Better: make concurrent writes mergeable. Here the key is a shopping cart. Europe adds a book, the US adds a lamp, at the same moment.

    If the cart is stored as a CRDT (a conflict-free replicated data type, here an observed-remove set), each leader merges the incoming change with its own: both end with {book, lamp}, no matter in which order the changes arrive. Nothing is lost and no coordination was needed.

    CRDTs exist for counters, sets, maps, registers and text (used by collaborative editors). Where no automatic merge makes sense, systems keep both versions (siblings) and let the application or the user resolve them on the next read.

    Scene: Scripted: the EU adds a book and the US adds a lamp to the same cart. With a set CRDT both leaders merge the changes and end with book and lamp.

    Key idea: Data types that merge deterministically (CRDTs) let replicas converge without losing concurrent writes.

  7. 7. The link between regions breaks

    Traffic is back. Now the link between the regions fails. With a single leader in the EU, every US write would fail until the link returns.

    With two leaders, both regions keep serving reads and writes and no request fails. That availability is the main reason to run multi-leader. But nothing crosses the ocean: every write accepted now is a potential conflict when the link comes back, and the backlog has to be shipped and resolved then.

    Scene: The link between the regions is cut. Both leaders keep accepting reads and writes with no errors, but no replication crosses between them.

    Key idea: Multi-leader keeps every region writable during a partition, and stores up conflicts for when it heals.

  8. 8. When multi-leader is worth it

    The link is back and replication flows again. In a real system the leaders would now exchange everything written during the outage and resolve each conflict; the simulation simply resumes from here.

    Multi-leader buys local write latency and writes that survive a partition. It costs conflicts: you need a rule for every kind of data (LWW where losing an update is fine, CRDTs or app-level merges where it isn’t), and nothing is consistent across regions in the moment. Many teams avoid it by giving each record a home region that takes all its writes, which keeps one leader per record.

    Scene: Both regions have a leader, serve local traffic in under 30 ms and replicate to each other again over the 70 ms link.

    Key idea: Use multi-leader when local writes or partition tolerance matter more than immediate consistency, and design conflict resolution up front.