Skip to content

Lesson 4 of 9 · 25m

Replication Mechanics and Topologies

Binary logs, GTIDs, row-based replication, read scaling, and the practical differences between simple replicas and accidental distributed systems.

Replication is deceptively easy to start and remarkably easy to misunderstand. The source writes changes to its binary log, replicas copy those events, and then everyone assumes the replica is a perfectly current clone. It is not. It is a separate server applying a stream of committed history, usually later than the source and sometimes in a different failure state.

The pipeline: binlog, IO thread, relay log, SQL thread

The source records committed changes in the binary log. A replica connects, downloads events into its relay log, and applies them. In classic terminology those were the IO and SQL threads; current MySQL documentation calls them receiver and applier threads. MariaDB still uses CHANGE MASTER TO and older terminology, but the concepts match even where syntax and status columns differ.

-- source
CREATE USER 'repl'@'10.20.%' IDENTIFIED BY 'a-long-random-password';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.20.%';

-- replica, MySQL 8.0.23+
CHANGE REPLICATION SOURCE TO
  SOURCE_HOST='db1.prod.sudosecurity.org',
  SOURCE_USER='repl',
  SOURCE_PASSWORD='a-long-random-password',
  SOURCE_AUTO_POSITION=1;
START REPLICA;

Use GTIDs if your topology is not trivial

GTIDs give each transaction a stable identity. SOURCE_AUTO_POSITION=1 lets a replica find its place without manually matching binlog file names and positions. They are close to mandatory once failover, chains, or multiple replicas enter the picture.

[mysqld]
gtid_mode = ON
enforce_gtid_consistency = ON
binlog_format = ROW

MariaDB implements GTIDs differently from MySQL, including domain IDs. Do not assume a failover procedure written for MySQL GTID works unchanged on MariaDB.

Row-based replication is the default for a reason

Statement-based replication replays SQL text. Row-based replication ships the actual row images. Row events are generally more deterministic, especially with non-deterministic functions, triggers, and updates that depend on execution context.

SHOW VARIABLES LIKE 'binlog_format';
SHOW REPLICA STATUS\G

The status output tells you whether receiver and applier threads are running, the executed GTID position, replication filters, and lag. On MySQL 8 use Seconds_Behind_Source; older releases and MariaDB commonly show Seconds_Behind_Master.

Topologies: pick the failure behavior first

A single source with replicas is the common shape. Replicas serve reads, backups, analytics, and failover candidates. A chain (source -> intermediate -> leaf) reduces source load but makes failure analysis harder. Multi-source replication exists, especially in MariaDB, but it is not a substitute for application-level sharding or conflict design.

Circular replication and dual-writer setups are where careers get scar tissue. Unless the application has hard partitioning rules, two writers eventually update the same row, create key conflicts, or diverge in a way no automation can safely reconcile.

Replicas are not consistency magic

If a user writes on the source and immediately reads from a replica, they may not see their write. Route read-after-write operations to the source or use replication-aware consistency logic. Semi-synchronous replication can reduce the window of loss by waiting for a replica to acknowledge receipt, but receipt is not the same thing as fully applied execution.

Field notes

  • Set replicas read-only with read_only=ON and super_read_only=ON where supported; stray writes are the most common cause of divergence.
  • Replication filters are sharp knives. A filtered replica is not a universal failover candidate unless you understand exactly what it excludes.
  • Time zones, SQL modes, and character sets should match deliberately across topology members.
  • Replication can be healthy and still be useless for failover if the replica is hours behind.

Was this lesson helpful?

Sign in to rate