Multi-Leader Replication

Several nodes accept writes and replicate to each other. Why you would want that, why it creates write conflicts, and the ways to avoid or resolve them.

Overview

In multi-leader replication, more than one node accepts writes. Each leader applies a write locally, confirms it, and then sends it to the other leaders in the background. It is also called active-active or master-master replication.

This buys local writes in several places and writes that keep working when one site is cut off. It costs something single-leader replication never has: two leaders can change the same data at the same time, and the system has to decide what the result is.

When one leader is not enough

A second leader earns its cost in four situations.

  • Users in several regions: each region writes to its own leader, so writes skip the long round trip to one far-away primary.
  • A region or its link goes down: other regions keep accepting writes, and changes sync when the link returns.
  • Offline-capable apps on a phone or laptop: each device is a leader for its local copy and syncs later.
  • Real-time collaborative editing: each user's edits apply at once locally and merge with everyone else's.

In the first case, the layout looks like this.

Inside each region, the leader usually has its own followers, so each region looks like a normal single-leader setup.

Key concepts

  • Multi-leader (active-active)
  • Write conflict
  • Home leader
  • Last write wins (LWW)
  • Siblings
  • CRDT
  • Replication topology
  • Version vector

Interview questions

  • How do you handle write conflicts in a multi-leader database?
  • Why is last write wins dangerous?
  • When would you choose multi-leader over single-leader replication?
  • Explain a CRDT to someone who has not seen one.
  • What ordering problem appears with all-to-all replication, and how is it handled?

Sign up free to read the full topic

More DDIA topics

Start free on Cariara · Coding problems · System design studio