"Explain how MySQL master-slave replication works and how you handle replication lag."
Replication is the core of MySQL high availability, and lag is a problem every production team has fought. Interviewers ask it to check that you understand the log-shipping mechanics and the read-your-own-writes anomalies that lag causes — not just "writes go to master, reads to slaves."
Walk through the mechanics: master writes every change to its binlog. A slave's IO thread connects to the master, pulls binlog events, and writes them to the slave's relay log. The slave's SQL thread replays the relay log against its own data. Three threads total: master dump thread, slave IO thread, slave SQL thread.
Cover binlog formats: STATEMENT (logs the SQL, compact but non-deterministic functions like NOW() / UUID() break replication), ROW (logs the actual row changes, safer, larger), MIXED (switches between them). Modern production almost always uses ROW.
Explain async vs. semi-sync: default async replication — master commits and returns immediately, slave catches up later, so a master crash loses committed data. Semi-sync requires at least one slave to ACK receipt of the binlog before master returns to the client, trading a bit of latency for data safety.
Mention group replication / MGR: Paxos-based multi-master or single-primary cluster with strong consistency. Heavier but gives automatic failover and row-level conflict detection.
Handle replication lag: monitor Seconds_Behind_Master and pt-heartbeat; common causes are long-running transactions, DDL, a single-threaded SQL thread hitting a hot table (use parallel replication since MySQL 5.7); fixes include read-after-write on master for critical reads, sharding writes, or forcing GTID-based read-wait for specific sessions.
MySQL replication works by shipping the binlog from master to slave. The master has one binary log of every data change; a slave has an IO thread that opens a replication connection to the master, pulls binlog events, and writes them into the slave's relay log. A second slave thread, the SQL thread, reads the relay log and replays the events against the slave's copy of the data. Three binlog formats exist. STATEMENT logs the SQL text — compact but breaks with non-deterministic functions: NOW(), UUID(), RAND(), or LIMIT without ORDER BY can produce different results on the slave. ROW logs the actual rows changed — larger binlog but deterministic. MIXED picks per-statement. Modern production uses ROW (often with binlog_row_image = MINIMAL to reduce size). Default replication is asynchronous — master commits locally, writes to binlog, returns success to the client, and the slave catches up later. This means a master crash can lose the tail of the binlog that slaves haven't received. Semi-synchronous replication changes the contract — master waits for at least one slave to ACK that it has received (not executed) the binlog before responding to the client. It trades 1-2 ms of latency for zero data loss on single-node failure. Group Replication (MGR) uses a Paxos-based protocol for multi-primary or single-primary clusters with automatic failover and conflict detection — heavier but much stronger. Replication lag is the operational pain point. Monitor it with Seconds_Behind_Master (rough) or pt-heartbeat (precise). Common causes: a long transaction on master holds the binlog so slave SQL thread stalls; a DDL statement that blocks the SQL thread; the single-threaded SQL thread is CPU-bound on hot updates. MySQL 5.7+ supports parallel replication by schema or by logical clock groups — enabling slave_parallel_workers > 0 is usually a 3-5x lag improvement. For read-your-own-writes consistency, the standard fix is to route critical reads back to master for a short window after a write, or use GTID-based session wait — MASTER_POS_WAIT / WAIT_FOR_EXECUTED_GTID_SET — to block until the slave has caught up past the write.
Mentioning the three threads (master dump, slave IO, slave SQL) signals you understand the wire protocol, not just the concept.
Knowing parallel replication (slave_parallel_workers) and GTID-based read-wait is a senior-level differentiator.
Cite STATEMENT vs ROW with a concrete example like NOW() — it's a classic gotcha.
Stuck on a command name in a live interview? HireMe AI can surface pt-heartbeat, Seconds_Behind_Master, WAIT_FOR_EXECUTED_GTID_SET on demand.
Global Transaction Identifier — a cluster-wide unique ID per committed transaction. Replaces file+position-based binlog coordinates and makes failover and replica swap much cleaner.
Yes — that's the core failover mechanism. Tools like MHA and Orchestrator automate promoting a slave when the master fails.
No — rpl_semi_sync_master_timeout (default 10s) falls back to async if no ACK arrives. This is a tradeoff between availability and durability.
HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.
Try HireMe AI Free