Data building blocks
Multi-leader replication
When several nodes accept writes at once, why that needs a conflict plan, and the four ways to settle a conflict, from avoiding it to merging with domain rules.
In multi-leader replication, more than one node accepts writes. Each leader sends its changes to the others, usually asynchronously. A write is local and fast, and it reaches the other leaders later.
Multi-leader earns its complexity in three situations: multi-region deployments, offline-capable clients and collaborative editing. A phone app with a local database is effectively a leader that syncs when it reconnects. Calendar and notes apps work this way, and so do shared document editors.
The big idea
The cost is write conflicts. Two leaders accept different writes to the same record at about the same time, and neither knows about the other. Something has to decide which write wins, or how to combine them.
Walking through it
A conflict within the replication delay:
t=0 ms US leader: set price = 10
t=0 ms EU leader: set price = 12
t=80 ms each leader sends its change to the other
Both leaders now hold two different values for the same record.
A last-write-wins rule keeps the one with the later timestamp, here 12.
The write of 10 disappears, and no error is raised.There are four common ways to handle it.
- Avoid conflicts. Send all writes for a record to its home leader, such as the region where that user lives. This is the most common real-world answer. It makes each record behave as if it had a single leader.
- Last write wins (LWW). Attach a timestamp and keep the highest. It is simple, but it silently discards one write. With skewed clocks, the "last" write may actually be earlier.
- Merge with domain rules. For a shopping cart, take the union of the items. For a counter, add the deltas.
- Use data types that merge automatically. CRDTs and operational transforms are designed for this. They are covered in conflict resolution.
Tip
If someone proposes multi-leader replication across regions, ask: "What happens when the same row is updated in two regions within the replication delay?" If there is no clear answer, the design probably needs single-leader replication with a home region for each record.
Common mistakes
- Going multi-leader without a conflict plan. Conflicts then get resolved by accident.
- Trusting clocks for LWW. Skewed clocks can keep the older write.
- Letting two regions write the same record. A home-region rule removes most conflicts.
Check yourself
-
What is the simplest way to avoid write conflicts?
Answer: route every write for a record to one home leader, so the record behaves like single-leader.
-
Why is last write wins risky when clocks are skewed?
Answer: the write with the later timestamp may have happened earlier, so the newer write can be the one discarded.
Key takeaways
- Multi-leader lets several nodes accept writes, which suits multiple regions and offline clients.
- Concurrent writes to one record are conflicts, and they need a plan.
- A home leader per record avoids most conflicts.
- Last write wins is simple, but it discards writes silently and depends on clocks.
Further learning
Next, see how leaderless stores handle writes without any leader, in leaderless quorums.
Related
Conflict resolution and CRDTs
What to do when replicas accept concurrent writes. The options run from last-write-wins and siblings to CRDTs, operational transformation and the sync engines used by collaborative editors.
Multi-region architecture
Active-passive, active-active and cell-based designs for latency, disaster tolerance and data residency, with the recovery, consistency and cost trade-offs each one carries.
Replication
Keeping copies of the same data on several machines, the three topologies that databases use, and the anomalies that replication lag causes. Start here for the replication map.
Time, clocks and ordering
Why wall clocks lie across machines, how skew silently loses writes, and how Lamport clocks, vector clocks, hybrid logical clocks and TrueTime order events you can't time directly.