We use analytics cookies.We use cookies to analyze site traffic and improve your experience.
Loading...
System Design Masterclass · Database Fundamentals
Free preview: the first 2 parts of this 13-part lesson.
Already bought the course? Sign in to read the whole lesson.
this lesson is part of
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design courseread the rest of this lesson in
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design coursein Database Fundamentals
This lesson is part of the full System Design Masterclass. Unlock it along with 769 other lessons for a one-time payment.
One-time payment by card or PayPal
Get Lifetime AccessOne-time payment: UPI, card or netbanking
Get Lifetime Access18 Foundation lessons are free, no payment needed. Try them now
It is easy and good
“It is really nice and good Please join the course”
Your application has grown. You have a primary database in Virginia and read replicas worldwide. Reads are fast everywhere. But writes are a different story.
A user in Mumbai updates their profile. That write travels 13,000 km across undersea cables to Virginia, gets processed, and the confirmation travels 13,000 km back. Round trip: 200-300ms minimum. For a simple profile update, that's noticeable. For a real-time collaborative document or a chat application, it's unacceptable.
You could put the primary in Mumbai, but then users in San Francisco get the same . You can't win with a single writer.
Master-master (also called multi-leader or multi-primary replication) solves this by allowing multiple servers to accept writes simultaneously. Each leader handles writes from nearby users, then exchanges changes with other leaders asynchronously.
It sounds ideal. And for specific use cases, it is. But it comes with a fundamental problem that doesn't exist in single-leader systems: write conflicts. Two leaders can modify the same data at the same time, and now you have two different versions of the truth.
The setup:
Example with two data centers. US-East and EU-West:
UPDATE users SET name = 'Roni' WHERE id = 42UPDATE users SET email = 'new@email.com' WHERE id = 42In this example, the writes touched different columns on the same row. No conflict. Easy.
But what if both users updated the same column?
UPDATE users SET name = 'Roni' WHERE id = 42 at timestamp T1UPDATE users SET name = 'Ronidas' WHERE id = 42 at timestamp T2Now US-East has name = 'Roni' and EU-West has name = 'Ronidas'. When they exchange changes, each leader sees a conflicting update. Which one wins?