Tech Interview RoomStudy guides

MySQL

How does MySQL replication work, and what are the HA options?

Advanced · 2 min read · Lesson 4 of 5 in MySQL

Binary log replication

The primary writes every change to the binary log. Each replica's I/O thread copies it into a relay log, and SQL applier threads replay it (in parallel with replica_parallel_workers). Use row-based binlog format (the default), and GTIDs (global transaction ids) so replicas can be repointed after failover without file and position bookkeeping.

Durability modes

  • Asynchronous (default): fastest, but a primary crash can lose the last transactions.
  • Semi-synchronous: the primary waits until at least one replica has received the transaction before acknowledging the commit.
  • Group Replication / InnoDB Cluster: Paxos-based consensus among 3+ members with automatic failover; MySQL Router sends clients to the primary. InnoDB ClusterSet adds disaster recovery across regions.

Managed options

Cloud SQL HA (a standby in another zone with synchronous disk replication), Amazon RDS Multi-AZ, and Aurora MySQL, which separates compute from a distributed storage layer replicated six ways across three zones, with fast failover and replicas sharing the same storage.

Scaling out

Read replicas for reads; Vitess (used by PlanetScale and large companies) for sharding with a MySQL-compatible proxy; ProxySQL for query routing and connection pooling.

Watch for

Replication lag (Seconds_Behind_Source in SHOW REPLICA STATUS), read-after-write consistency, and long transactions that delay replicas.

Example

sql
-- On the replica (MySQL 8.0.23+ syntax)
CHANGE REPLICATION SOURCE TO
  SOURCE_HOST = 'mysql-primary',
  SOURCE_USER = 'repl',
  SOURCE_PASSWORD = '***',
  SOURCE_AUTO_POSITION = 1,        -- use GTIDs
  SOURCE_SSL = 1;
START REPLICA;

SHOW REPLICA STATUS\G             -- Replica_IO_Running, Replica_SQL_Running, Seconds_Behind_Source
properties
# my.cnf essentials on every server
gtid_mode=ON
enforce_gtid_consistency=ON
binlog_format=ROW
sync_binlog=1
innodb_flush_log_at_trx_commit=1

Follow-up questions interviewers ask

Try answering each one before you open it.

What do sync_binlog=1 and innodb_flush_log_at_trx_commit=1 guarantee?

Every commit flushes the redo log and the binary log to disk, so a crash loses no committed transactions and the binlog stays consistent with the data. Relaxing them (2 or 0) improves write throughput but can lose up to about a second of transactions on an OS crash or power loss.