Master-master replication

You may have noticed two issues with the master-replica setup:

  • The master is used extensively for database writes, and hence is under constant duress
  • The issue of reads has been solved with replicas but the single point of failure for writes is still present

Master-master replication tries to solve these issues by making every database a master. It can be explained as follows:

  1. We take a cluster of databases:
Cluster of databases
  1. We designate every database as a master:
All databases are designated as a master
  1. Reads can be performed from any of the masters:
Reads performed on masters
  1. Writes can be performed to any of the masters:
Writes made to DB-1 and DB-3
  1. Every master updates every other master with the writes:
Database state synchronized across the masters
  1. Hence, the state is maintained across all the databases:
DB-1 failure, successful reads and writes

It may seem like this strategy works fine, but it has its own limitations and challenges; the major one being conflict resolution between writes. Here's a simple example.

We have two master-master databases DB-1 and DB-2, and both have the latest state of the database system:

Latest state of DB-1 and DB-2

We have two simultaneous write operations to perform, so we send "Bob" to DB-1 and "Alice" to DB-2.

Write "Bob" to DB-1 and Write "Alice" to DB-2

Now that both databases have written the data to their tables, they need to update the other master with its own latest state:

States before DB synchronization

This will lead to conflict because in both tables, ID# 3 is populated with Bob for DB-1 and Alice for DB-2:

Conflict while updating DB-1 and DB-2 states because ID# 3 is already populated.

In reality, the master-master strategy would have in-built mechanisms to deal with these kinds of issues, but they may induce a performance penalty or other challenges. This is a complex subject and we have to decide on what trade-offs are worth making if we want to use master-master replication.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset