Replication
Replication
A technique that copies data to multiple nodes to improve availability and fault tolerance. Master-Slave and Multi-Master approaches.
Replication
A technique that copies data to multiple nodes to improve availability and fault tolerance. Master-Slave and Multi-Master approaches.
Replication is a technology that copies a database's data to multiple servers. It is used for high availability (HA), read load balancing, and disaster recovery (DR). It is common for the Primary (Master) server to handle writes and for the Replica (Slave) servers to handle reads.
In asynchronous replication, the Primary responds immediately after committing a transaction and propagates the changes to the Replica asynchronously. Performance is good, but data may be lost if the Primary fails. Synchronous replication commits after confirmation from the Replica, which provides higher data safety but introduces latency.
Semi-synchronous replication commits after confirming that at least one Replica has received the data. It balances data safety and performance, and is widely used in MySQL. Group replication allows multiple nodes to handle writes on an equal footing.
Replication lag is the difference in data between the Primary and the Replica. A read right after a write may not show the latest data, so important reads should be done on the Primary, or the lag should be monitored.
-- MySQL 레플리케이션 상태 확인 (Replica에서)
SHOW REPLICA STATUS\G
-- 복제 지연 확인
SHOW REPLICA STATUS\G
-- Seconds_Behind_Source: 0 이면 동기화 완료
-- PostgreSQL 스트리밍 레플리케이션 설정 (Primary)
-- postgresql.conf
wal_level = replica
max_wal_senders = 3
synchronous_standby_names = 'replica1'
-- Python에서 읽기/쓰기 분리
class DatabaseRouter:
def __init__(self):
self.primary = create_engine('mysql://primary/db')
self.replicas = [
create_engine('mysql://replica1/db'),
create_engine('mysql://replica2/db'),
]
self._replica_idx = 0
def get_write_connection(self):
return self.primary.connect()
def get_read_connection(self):
# 라운드 로빈으로 Replica 선택
conn = self.replicas[self._replica_idx].connect()
self._replica_idx = (self._replica_idx + 1) % len(self.replicas)
return conn
# 사용
router = DatabaseRouter()
with router.get_write_connection() as conn:
conn.execute("INSERT INTO users (name) VALUES (%s)", ('Alice',))
with router.get_read_connection() as conn:
result = conn.execute("SELECT * FROM users")
Senior: The Primary DB CPU is at 80%. Most of the queries are reads, so let's distribute them to the Replica.
Junior: If we send reads to the Replica, won't the data be inconsistent?
Senior: If the replication lag is under 1 second, it's mostly fine. However, reads right after a write must be done on the Primary.
Interviewer: Please explain the difference between synchronous and asynchronous replication.
Candidate: Synchronous replication commits after confirming that the Replica has received the data, so there is no data loss, but latency occurs. Asynchronous replication commits immediately and is fast, but if the Primary fails, data that has not yet been replicated may be lost.
Interviewer: How do you resolve replication lag?
Candidate: You improve the Replica's performance or enable parallel replication. In the application, you do reads after writes on the Primary, or read after a minimum delay.
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Reviewer: A "user not found" error occasionally occurs at login right after sign-up.
Author: Is it because of replication lag, since reads are done on the Replica?
Reviewer: Yes. Do reads right after sign-up on the Primary, or use session-based routing to send reads from the same session to the Primary.