How does MySQL replication work?

IntermediateConceptMySQL
Answer

Short answer

The source (primary) writes every change to its binary log. Each replica connects to the source, copies those events into its own relay log (I/O thread), and replays them (SQL/applier threads), so it stays a near-real-time copy. Replicas are used for read scaling, backups and failover.

Flow

App writes ──▶ SOURCE ── binlog ──▶ REPLICA (I/O thread → relay log → applier) ◀── read-only queries

Setup outline (GTID-based)

-- on the source
CREATE USER 'repl'@'10.0.%' IDENTIFIED BY '***';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.0.%';
-- on the replica (after loading a backup of the source)
CHANGE REPLICATION SOURCE TO SOURCE_HOST='10.0.21.10', SOURCE_USER='repl', SOURCE_PASSWORD='***', SOURCE_AUTO_POSITION=1;
START REPLICA;
SHOW REPLICA STATUS\G

What to check

  • Replica_IO_Running: Yes and Replica_SQL_Running: Yes
  • Seconds_Behind_Source — the replication lag
  • Last_Error — why it stopped

Asynchronous by default

The source doesn't wait for replicas, so a replica can be slightly behind; reading your own write from a replica may show old data. Semi-synchronous replication waits for at least one replica to acknowledge.

What to say in the interview

"The source writes changes to its binlog, replicas pull them into relay logs and apply them. It's asynchronous by default, so I monitor Seconds_Behind_Source and both replication threads. We use replicas for reads, backups and failover, which managed services like RDS automate."

All Databases (MySQL, PostgreSQL, MongoDB) questions Roadmap: MySQL basics

More on MySQL