Try
↑ ↓ move↵ openesc close
In this lesson · topic 5 of 7
  1. 1. One copy is not enough
  2. 2. Three ways to say yes
  3. 3. Slow the network and kill the leader
  4. 4. When a leader crash loses acknowledged writes
  5. 5. What the replication log actually contains
  6. 6. How a follower falls hours behind
  7. 7. Check the failover and lag cases

Topic 5

What the replication log actually contains

6 min read

So far the log was a list of writes. Real databases have to pick a format for those entries, and the choice decides what can go wrong on a follower.

Ship the statement

The simplest log is the SQL text itself. The leader runs UPDATE accounts SET balance = balance - 20 WHERE id = 7 and sends the same text to each follower. It is compact, and it reads well in a debugger.

It breaks whenever a statement is not deterministic. INSERT INTO sessions VALUES (UUID(), ...) gives each follower a different id. UPDATE jobs SET state = 'taken' LIMIT 1 with no ORDER BY can pick a different row on each machine. A statement that reads a value another transaction is still changing can produce different results depending on timing. A trigger or stored procedure with side effects runs again on every follower.

The followers now hold different data, and nothing reports it. MySQL still supports statement-based logging but flags such statements as unsafe, and its default has been row-based since version 5.7.7.

Ship the disk changes

The second option is the storage engine’s own write-ahead log. Before a database changes a page on disk, it appends a description of the change to a sequential log, so it can recover after a crash. That log already exists, so the leader can stream it to followers. PostgreSQL’s streaming replication does this.

A follower that replays it ends up with a byte-for-byte copy of the leader’s pages. There is no ambiguity about what ran. The price is coupling. The log describes physical pages, so the follower must run a compatible version of the same storage engine. Upgrading means taking followers and leader through a careful dance rather than upgrading the followers first and promoting one. You also cannot replicate into a different database, or only some tables.

Ship the row changes

The third option is a logical log: for each changed row, the table, the key, and the new values. An update becomes “row 7 in accounts now has balance 480”. It does not depend on how the engine lays out pages, so followers can run a different version, and an external system can read the same stream to fill a search index or a cache. PostgreSQL calls its version logical decoding, and MySQL’s row-based binary log works the same way.

It is deterministic, because the values were computed once on the leader. The cost is size.

Predict first

One statement, `UPDATE orders SET status = 'archived' WHERE created < '2020-01-01'`, changes 2 million rows of about 100 bytes each. A row-based log records roughly 200 bytes per row, with before and after values. The link to the follower carries 12.5 MB per second. About how long does shipping this one statement take under each format?

That arithmetic is why one bulk update can put a row-based follower tens of seconds behind while the leader finishes it in one pass. Because followers apply entries in order, every ordinary write queued behind that batch waits too. The lag you saw in the lab can come from one large transaction, not just from a slow network.

Which one to pick

Mixed formats exist too. MySQL’s mixed mode uses statements until it meets one it considers unsafe, then falls back to rows for that statement.

Whatever the format, the order guarantee is the same: followers apply the log in the order the leader wrote it. The format decides only how faithfully each entry can be replayed.

Sources

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms