MySQL deep-dive · Part 10 of 10. Previous: MySQL from application code. Back to the series guide.
Everything in this series so far happened inside one server. But almost no production MySQL runs alone: there’s a replica for reads, a standby for failover, a pipeline feeding the data warehouse, maybe a Debezium connector feeding Kafka. Every one of those consumers hangs off a single mechanism — the binary log — and the guarantees you think you have (no lost writes on failover, replicas that converge, backups you can roll forward) depend on settings most teams have never read.
The mental model for this part: the binlog is a logical, ordered record of every committed change, written by the server; replication is just another process reading that record and replaying it elsewhere. Hold that, and replicas, GTIDs, semi-sync, and CDC all become variations on “who reads the log, and how far behind are they?”
Two logs, two jobs
MySQL writes two logs on the write path, and conflating them causes real confusion.
The redo log belongs to InnoDB. It’s physical — “page 4,214 changed in this way” — and it exists for exactly one purpose: crash recovery of this instance. After a power cut, InnoDB replays the redo log to bring its pages back to the last committed state. The redo log never leaves the machine and is useless to any other server.
The binlog belongs to the MySQL server layer, above the storage engine.
It’s logical — “this row in orders changed from X to Y” — and it’s written
once per transaction, at commit, in commit order. It’s the record other systems
can consume: replicas, point-in-time recovery, CDC pipelines.
A committed transaction must survive in both logs, and they must agree — a transaction in the binlog that InnoDB lost (or vice versa) means a replica that has data the primary doesn’t. MySQL coordinates the two with an internal two-phase commit: prepare in InnoDB, write the binlog, commit in InnoDB.
Row vs statement: what actually goes in the log
The binlog has formats, and the choice decides what “replaying the log” means.
Statement-based (SBR) logs the SQL text: UPDATE accounts SET balance = balance - 100 WHERE id = 42. Compact, human-readable — and haunted by
non-determinism. UPDATE sessions SET expired = 1 ORDER BY last_seen LIMIT 100 can match different rows on the replica. NOW(), UUID(), RAND(),
user-defined functions — anything that doesn’t evaluate identically on the
replica makes the replica silently diverge. No error. Just different data,
discovered months later.
Row-based (RBR) — the default since 5.7 — logs the effect: the before and
after images of each changed row. Deterministic by construction. The cost is
volume: an UPDATE touching a million rows is one statement in SBR but a
million row images in RBR.
-- See what's in the binlog (row events decoded)
SHOW BINARY LOGS;
mysqlbinlog --base64-output=decode-rows -vv binlog.000042 | less
-- Current format
SHOW VARIABLES LIKE 'binlog_format'; -- ROW
SHOW VARIABLES LIKE 'binlog_row_image'; -- full | minimal
binlog_row_image = minimal logs only changed columns plus the key —
dramatically smaller for wide tables (remember Part 2:
row width is a decision), but some CDC consumers want full images. Check what
your pipeline needs before shrinking it.
How a replica actually replicates
Replication is two independent jobs on the replica, and the split explains most lag mysteries:
- An I/O thread connects to the primary, streams binlog events, and appends them to a local relay log. This is cheap — network and sequential writes.
- Applier threads read the relay log and re-execute the changes against the replica’s own InnoDB — full price: index maintenance (Part 3), locking (Part 7), the works.
By default all of this is asynchronous. The primary commits, acknowledges your write, and then replicas pull the event — milliseconds later on a good day. The primary does not wait, and does not know or care whether any replica is alive.
That default has a sharp edge: if the primary dies after acknowledging a commit but before any replica fetched it, the write is gone when you promote a replica. Acknowledged ≠ replicated.
GTIDs: naming transactions instead of file offsets
Classic replication coordinates by file and position: “I’ve read up to
binlog.000042, offset 193841.” Those coordinates are meaningful only on one
server. After a failover, every other replica holds a bookmark into a file that
no longer exists — repointing them at the new primary was historically a manual,
error-prone archaeology exercise.
A GTID (global transaction identifier) names the transaction itself:
source_uuid:sequence, e.g. 3E11FA47-...:23. Every server that applies the
transaction records the same GTID. A replica connecting with
MASTER_AUTO_POSITION = 1 just says “here’s the set of GTIDs I have” and the
new primary sends everything it’s missing — no file names, no offsets, no
archaeology.
-- gtid_mode = ON, enforce_gtid_consistency = ON in my.cnf
SELECT @@gtid_executed\G
CHANGE REPLICATION SOURCE TO SOURCE_HOST = 'new-primary',
SOURCE_AUTO_POSITION = 1;
Semi-sync: shrinking the loss window, not closing it
Semi-synchronous replication makes the primary wait, at commit, until at least one replica acknowledges the transaction — then responds to the client. Two prints in small type:
First, the replica acknowledges receipt into its relay log, not application. The data is on a second machine’s disk, which is the point — but a freshly-promoted replica may still need seconds to apply its relay-log backlog before it reflects everything it acknowledged.
Second, if no replica responds within rpl_semi_sync_source_timeout
(default 10s), the primary silently falls back to async and keeps
accepting writes. Availability wins over durability by default. The status
variable Rpl_semi_sync_source_status tells you which mode you’re actually
in — alert on it, because the failure mode is “we thought we had semi-sync
during the exact incident where it mattered.”
Use the AFTER_SYNC wait point (the default, sometimes called lossless
semi-sync): the primary waits for the replica ack before committing in the
engine, so a crashed primary can’t expose data to clients that no replica ever
received.
Lag: where it comes from, how it lies
Replica lag is almost never the network. The I/O thread keeps up; it’s the appliers that fall behind, because for years they were a single thread replaying — serially — a workload the primary executed with dozens of concurrent connections.
Modern MySQL parallelizes the apply (replica_parallel_workers with
WRITESET dependency tracking), which lets non-conflicting transactions
replay concurrently and removes most steady-state lag. What parallelism cannot
fix:
- Huge transactions. A one-million-row
UPDATEis one binlog event group; the replica replays it alone, serially, while lag climbs. The batching discipline from Part 8 — thousands of rows per transaction, not millions — is a replication rule as much as a locking one. - DDL. An
ALTERthat took an hour on the primary takes an hour on each replica, during which that replica applies nothing else.
Lag also breaks read-your-own-writes: the user saves, the next page reads
a replica that hasn’t applied the save, and their edit “vanishes.” The fixes
are routing, not tuning — send post-write reads to the primary, or pin a
session to the primary for a few seconds after it writes. (The same trap
appears in MongoDB’s nearest read preference
— it’s a property of async replication, not of any one database.)
The binlog as a product: CDC
The final reframe: the binlog isn’t just for replicas. It’s a totally-ordered
stream of committed row changes — which is exactly what a change-data-capture
pipeline wants. Debezium connects to MySQL as if it were a replica,
streams row events, and publishes them to Kafka. gh-ost from
Part 8 tails it to keep a shadow table in sync.
Cache invalidation, search-index sync (the database→Elasticsearch sync from
the ES series), audit trails — all of
them are “another replica” to MySQL.
That’s why the binlog settings are worth respecting even on a server with no
replicas today: binlog_format = ROW, GTIDs on, sensible retention
(binlog_expire_logs_seconds). The day you need a replica, a warehouse feed,
or an online migration, the log is already the shape every tool expects.
That closes the series loop: one server’s guarantees (Parts 1–9) plus one honest log — and from that log, every multi-server property MySQL offers is derived.
Recall · check yourself
Before you go
Answer from memory first, then reveal. These cards also live in the review queue — come back in a few days.
-
R01 MySQL has two logs on the write path — what does each one do?
InnoDB's redo log is physical and exists for crash recovery of the local instance. The binlog is a server-level, logical record of committed changes — replication, PITR, and CDC all read it. A write isn't durable *and* replicable until both are flushed, coordinated by an internal two-phase commit.
-
R02 Why is statement-based replication dangerous, and what replaced it?
Statements like
UPDATE ... LIMIT 1without ORDER BY, or ones usingNOW()/UUID(), are non-deterministic — replicas can diverge silently. Row-based replication (the default since 5.7) ships the actual row changes instead of the SQL. -
R03 What problem do GTIDs solve over file + position coordinates?
A binlog file/offset is only meaningful on one server — after failover, replicas can't find their place on the new primary. A GTID names the transaction itself, globally, so a replica can auto-position against any server that has it.
-
R04 Does semi-synchronous replication mean the replica has applied your write?
No — the primary waits only for a replica to acknowledge *receiving* the event into its relay log, not applying it. And on timeout it silently degrades to async. It shrinks the data-loss window; it doesn't close it.
-
R05 Why does
Seconds_Behind_Sourceread 0 moments before a replica falls minutes behind?It measures the delay of the event currently being applied — a single huge transaction arrives as one event, so lag reads ~0 until the replica starts replaying it, then jumps. Monitor GTID gap or heartbeat tables instead; keep transactions small.