Multi-Master Replication
Multi-Master Replication
Writes are allowed on multiple nodes. Conflict resolution is required.
Multi-Master Replication
Writes are allowed on multiple nodes. Conflict resolution is required.
Multi-Master Replication is a database replication method in which multiple nodes can accept write operations simultaneously. Unlike Primary-Replica Replication, all nodes act as masters, distributing the write bottleneck.
The biggest challenge is Conflict Resolution. When the same data is modified on multiple nodes at the same time, a conflict occurs. Resolution approaches include Last Write Wins (based on timestamps), Custom Resolution (application-defined logic), and CRDT (Conflict-free Replicated Data Types).
It is useful in geographically distributed services where each region needs to work without write latency. It is supported by CockroachDB, YugabyteDB, Galera Cluster (MySQL), and others.
-- Galera Cluster (MySQL Multi-Master) 설정 예시
-- wsrep.cnf
[mysqld]
wsrep_on=ON
wsrep_cluster_name="my_cluster"
wsrep_cluster_address="gcomm://node1,node2,node3"
wsrep_node_name="node1"
wsrep_provider=/usr/lib/libgalera_smm.so
-- 모든 노드에서 쓰기 가능
-- Node 1에서
INSERT INTO users (name) VALUES ('Alice');
-- Node 2에서 (동시)
INSERT INTO users (name) VALUES ('Bob');
-- 충돌 발생 시나리오
-- Node 1: UPDATE products SET stock = 10 WHERE id = 1;
-- Node 2: UPDATE products SET stock = 5 WHERE id = 1;
-- Last Write Wins 또는 애플리케이션 정의 로직 필요
# Python: 충돌 감지 및 재시도 패턴
def update_with_conflict_retry(product_id, new_stock, max_retries=3):
for attempt in range(max_retries):
try:
with db.begin():
product = db.query("SELECT * FROM products WHERE id = %s FOR UPDATE", product_id)
# 낙관적 락: version 체크
db.execute("""
UPDATE products
SET stock = %s, version = version + 1
WHERE id = %s AND version = %s
""", (new_stock, product_id, product['version']))
if db.rowcount == 0:
raise ConflictError("Version mismatch")
return True
except ConflictError:
time.sleep(0.1 * (2 ** attempt))
raise Exception("Max retries exceeded")
Architect: "We need to serve users in three regions — the US, Europe, and Asia — without write latency."
DBA: "We'll need Multi-Master Replication. Shall we look at CockroachDB or YugabyteDB?"
Architect: "How do we handle conflicts?"
DBA: "Last Write Wins is enough for things like user profiles, and for inventory or payments, we can maintain strong consistency with distributed transactions."
Interviewer: "Please explain the differences between Multi-Master and Primary-Replica approaches and the criteria for choosing between them."
Candidate: "Primary-Replica is simple because it has a single write point, but it creates a write bottleneck. Multi-Master allows writes on multiple nodes, so it is highly scalable, but conflict resolution is complex. I choose Multi-Master when a global service or high availability is required."
Interviewer: "Do you have experience with conflict resolution?"
Candidate: "I handled user settings with LWW, inventory with the CRDT Counter type, and orders with distributed transactions."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Reviewer: "Looking at this logic, if the same coupon is used on two nodes at the same time, both could succeed."
Developer: "Ah, because it's Multi-Master, each node processes it locally..."
Reviewer: "Right. Coupon usage needs a distributed lock or a global sequence. Or there's also the option of routing so that specific coupons are processed only on specific nodes."
Developer: "I'll handle it with a distributed lock and add a timeout setting."