NOTE
4.1 Distributed-System Replication Architecture: Leader-Leader Replication
Multi-leader replication allows several leaders to accept writes, each potentially with followers. These notes examine the architecture in multi-datacenter scenarios.
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Is Leader-Leader?
- There are multiple Leaders, and each Leader has multiple Followers.
2. Leader-Leader Use Cases
- Multiple data centers.
- Applications need to keep working while disconnected from the network.
3. How Leader-Leader Works
3.1. Leader Election
3.2. Leaders Synchronize Data to Leaders
- Writes are handled by a Leader. After the Leader saves the data locally, it performs two operations:
- It sends the data change to Followers in the same data center, which replay the change.
- It also sends the change to Leaders in other data centers, which replay it and replicate it to their Followers.
3.2.1. Write-Conflict Problem
- Multiple clients modify the same key at the same time.
- With a single Leader, the second write blocks and waits for the first write to complete.
- With multiple Leaders, after writing to the Leader in data center 1, the data is asynchronously replicated to the Leader in another data center, so both writes succeed.
3.2.1.1. How to Solve Write Conflicts
3.2.1.1.1. Synchronous Waiting
- The client requests leader1 to write. leader1 replicates the write to the other Leaders before returning success to the client.
- Disadvantage: synchronous replication has low availability (low performance).
3.2.1.1.2. Avoid Conflicts
- Route writes from the same user to the same Leader.
- Disadvantage: if routing changes, conflicts can still occur.
3.2.1.1.3. Resolve Conflicts
- LWW:
- Assign a timestamp to each write. Keep the write with the higher timestamp and discard the other writes.
- Generally, the server adds a version field to each record. The client first reads the data and increments version by 1 when modifying it. The server checks whether the client’s version is greater than the data’s version; if so, modify it, otherwise reject it.
- Merge these values together.
- Custom resolution:
- Execute on write: once a write conflict is detected, call a conflict handler to process it.
- Execute on read: when a conflict is detected, all conflicting writes are stored. The next time the data is read, these multiple versions are returned to the application. The application may prompt the user or automatically resolve the conflict and write the result back to the database.
3.3. Failure Handling
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub