In this lesson, you will learn how to coordinate simultaneous write operations across geographically distributed database nodes without relying on a single authoritative leader.
The Challenge of Distributed Writes
In traditional replication, a single leader manages writes to ensure consistency. However, when you need low-latency writes at multiple global locations (e.g., Tokyo, London, and New York), waiting for a round-trip to a distant leader creates unacceptable performance bottlenecks. Multi-Master replication allows any node to accept a write, but this introduces the risk of write conflicts, where the same data is modified concurrently in different regions.
Managing Conflict Resolution
Because you cannot perform a Two-Phase Commit across global distances without severe latency, you must shift from a "strong consistency" model to "eventual consistency." When two masters receive conflicting updates for the same row, the system must resolve them. Common strategies include:
- Last Write Wins (LWW): Using synchronized clocks (often via vector clocks or physical timestamps) to keep the update with the latest time.
- Conflict-free Replicated Data Types (CRDTs): Designing data structures that are mathematically commutative, ensuring that no matter the order of updates, all nodes converge to the same state.
- Application-Level Resolution: The database flags a conflict in a specific "conflict table," requiring the business logic to merge the records later.
Illustration of Conflict
Imagine a user updates their profile picture in London while simultaneously updating their bio in New York. If both updates affect the same record object:
-- London Node (10:00:01)
UPDATE users SET profile_pic = 'new_pic.jpg' WHERE user_id = 5;
-- New York Node (10:00:02)
UPDATE users SET bio = 'Updated bio' WHERE user_id = 5;
In a Multi-Master topology, the system broadcasts these updates to all other nodes. Without proper coordination, the final state might be inconsistent if the replicas arrive out of order. Sophisticated systems use Vector Clocks to track the causal history of these updates, ensuring that the system can detect when a write was performed without knowledge of a previous concurrent write, triggering a resolution process rather than blindly overwriting data.
Key Takeaway: Multi-Master replication prioritizes availability and write performance by allowing local modifications, but it necessitates robust conflict resolution strategies to handle the inevitable synchronization of concurrent writes.