0%
Distributed Systems: Consensus, Clocks, and Replication
Foundations
Time and Order
Consistency Models
Consensus
Transactions and Operations
Multi Leader and Leaderless Replication
The previous lesson described replication with one leader. It is simple and avoids conflicts, but every write must reach that one leader, and when the leader fails, writes stop until failover completes. This lesson covers the two main alternatives. Multi leader replication allows several replicas to accept writes and exchange them with each other. Leaderless replication, popularized by Amazon Dynamo, lets any replica accept writes and has clients or coordinators talk to several replicas directly. Both trade the simplicity of a single order for better availability and latency, and both must handle the consequence: concurrent conflicting writes.
Multiple data centers
The classic motivation for multi leader replication is operating in several regions. With a single leader in one region, a user on another continent pays an intercontinental round trip on every write, perhaps 150 milliseconds, and if the leader region has an outage, writes stop everywhere. With a leader in each region, each user writes to the local leader with low latency, and leaders replicate to each other asynchronously in the background. A regional outage stops only the users routed there, and they can be redirected to another region whose leader is already accepting writes. Network problems between regions do not block writes, since inter region replication is asynchronous.
The cost is that two users in different regions can modify the same data at the same time, each through their own leader, and neither leader knows about the other write until replication delivers it. Both writes succeed locally, and the conflict is only discovered later, when it is too late to reject either one. Every multi leader design must decide what to do then.
Clients that work offline
A second motivation is clients that must keep working without a network connection. A calendar app on a phone, a note taking app on a laptop or a field service app in a basement must let users read and modify data while offline and sync when they reconnect. Each device effectively holds a local leader: it accepts writes immediately and replicates them to the server and other devices later. From the replication perspective this is multi leader replication with extreme lag, minutes, hours or days, and potentially thousands of leaders. Tools such as CouchDB with PouchDB, Realm, Firebase offline persistence and local first frameworks are built around this model.
Collaborative editing
A third motivation is real time collaboration, such as several people editing the same document. If every keystroke had to go through a single leader and wait for confirmation, typing would feel sluggish. Instead, each user edits a local copy, sees changes instantly and exchanges edits with others continuously. Concurrent edits to the same document are normal, not exceptional, so the system needs a way to merge them automatically. Google Docs uses operational transformation, and newer tools use conflict free replicated data types, which the CRDT lesson covers in depth.
The common thread
In all three cases, the system accepts writes at several places without coordinating first, because coordination would cost too much latency or would be impossible during disconnection. This means the system gives up a single order of writes. Two replicas can apply the same set of writes in different orders, and unless the system resolves conflicts deterministically, replicas can end up with different data permanently. The central problem of this lesson is therefore convergence: making sure that once all writes have been delivered everywhere, all replicas hold the same value.
Within a single data center
Multi leader replication within a single data center is rarely worth it. Inside one region, network latency between replicas is low, so a single leader with fast failover gives good write latency and high availability without the complexity of conflicts. Most experts advise avoiding multi leader setups unless the latency or offline requirements genuinely demand them. Many databases offer multi leader features as add ons, such as BDR for PostgreSQL, Tungsten Replicator or group replication for MySQL and active active features in commercial products, and they often come with subtle limitations around auto incrementing keys, triggers and integrity constraints.
Where integrity constraints break
Constraints that are easy with a single leader become hard with several. A uniqueness constraint, such as one account per username, cannot be enforced locally if two leaders can each accept a registration for the same name at the same time; both accept, and the conflict is discovered later. The same applies to constraints like keeping an account balance non negative or not double booking a meeting room. Systems either route such writes to a single designated leader, accepting the latency, or detect violations after the fact and compensate, for example by asking one of the users to choose another username. Knowing which invariants need coordination is a key design skill, and the consistency lessons later in this course return to it.
Latency numbers that motivate it
The latency argument is easiest to see with numbers. Light in fiber travels about 200 kilometers per millisecond, so a round trip between Frankfurt and Virginia, roughly 6,500 kilometers each way, cannot take less than about 65 milliseconds, and in practice takes 80 to 100. Between Sydney and Virginia it is closer to 200. A web page that performs three sequential writes to a leader in Virginia makes a Sydney user wait more than half a second for those writes alone. With a local leader, each write takes a few milliseconds. No amount of engineering can beat the speed of light, so if users across the world must write with low latency, writes must be accepted near them.
Availability during regional failures
The availability argument is just as strong. Cloud regions do fail: power events, network misconfigurations and control plane bugs have taken whole regions offline for hours. With a single leader, failing over to another region typically means promoting an asynchronous replica, losing the writes that had not crossed the ocean and spending minutes or more on the switch, often with a human approving it. With a leader in every region, the other regions never stopped accepting writes, so the impact is limited to the users in the failed region, who can be rerouted. The trade off is that writes made in the failed region just before the outage may not have reached the others, and when the region returns, its writes must be reconciled with everything that happened elsewhere meanwhile.
Offline sync in detail
Offline capable apps illustrate conflicts vividly. Imagine a shared shopping list on two phones. One person, in a store with no signal, checks off milk and adds eggs. The other, at home, removes milk and adds bread. When both phones reconnect, the system must merge two divergent histories that may span hours. Removing milk on one side and checking it off on the other is a genuine conflict that needs a rule, while adding eggs and adding bread merge trivially. Designs that handle this well model data as operations or as CRDTs rather than as whole values to overwrite, so that independent changes combine and only real conflicts need decisions.
How replication between leaders works
Each leader in a multi leader setup behaves like a single leader to its local clients and like a follower to the other leaders. It records local writes in its log and streams them to the other leaders, which apply them as if they were replicated writes. To avoid loops, each write carries the identifier of the leader where it originated, and leaders do not send a write back to its origin. Applying a remote write is where conflict detection happens: if the remote write touches a record that was also modified locally since the last common version, the replica has found a conflict and must invoke its resolution rule.
Multi leader and leaderless replication accept writes at several replicas without coordinating, giving lower latency, offline operation and better availability. The price is concurrent conflicting writes, so the system must guarantee convergence, and invariants such as uniqueness need special handling.