Understanding Master‑Slave Replication in Databases: Principles, Benefits, and Interview Questions
This article explains the concept, advantages, and inner workings of master‑slave database replication, outlines the key threads and step‑by‑step process, and provides common interview questions with concise answers for candidates preparing for database engineering roles.
1. What is Master‑Slave Replication?
Master‑slave replication creates a slave database that is an exact copy of the master, which typically serves as a near‑real‑time business database.
2. Benefits of Replication (Why Use It?)
Hot standby for disaster recovery: if the master fails, the slave can take over, preventing data loss.
Architectural scaling: distributes I/O load across multiple machines, improving overall I/O performance.
Read/write separation: allows the master to handle writes while slaves serve read‑heavy workloads such as reporting, reducing lock contention.
3. How Replication Works (Interview Essentials)
The master records every SQL statement in a binary log (binlog). The slave connects to the master, receives the binlog, writes it to a relay log, and re‑executes the statements on the slave.
Three threads are involved in a replication connection:
Binlog output thread on the master, which streams binlog data to each connected slave.
Slave I/O thread, which fetches the binlog from the master and writes it to the local relay log.
Slave SQL thread, which reads events from the relay log and executes them on the slave database.
Step‑by‑step process:
Updates (INSERT/UPDATE/DELETE) on the master are written to the binlog.
The slave initiates a connection to the master.
The master creates a binlog dump thread to stream the binlog to the slave.
The slave I/O thread receives the binlog and stores it in the relay log.
The slave SQL thread reads the relay log from Exec_Master_Log_Pos and replays the events, applying the changes to the slave's data.
4. Common Interview Questions and Quick Answers
What are the advantages of master‑slave replication? (See section 2 above.)
Explain the replication principle. (See section 3 above.)
How do you handle read latency on the slave? (Refer to external article linked in the original source.)
What should you do if the master server crashes? (Switch to the slave as the new master; see linked article for detailed steps.)
If replication reaches a write‑performance bottleneck, how would you solve it? Use sharding or partitioning to split tables, or split the database into multiple instances to distribute write load.
Images illustrating the replication architecture:
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.