A team replicates by shipping SQL statements. After a month, a row-count check shows the leader and a follower differ by a few rows, and no errors were logged. What is the likely cause, and what would you change?
Sketch your own answer first, then compare it with the one below.
A strong answer
Statement shipping replays the same SQL text on each machine, so any statement whose result depends on something other than the data will diverge silently. Typical culprits are UUID() or RAND() in an insert, an UPDATE ... LIMIT 1 with no ORDER BY, which can pick a different row on each machine, and triggers or functions with side effects. A few rows differing, with no errors, fits that pattern: each statement succeeds everywhere, just with different results.
First find which statements are responsible. MySQL flags statements it considers unsafe for statement-based logging in its error log, and that list is a good start. A checksum tool that compares tables chunk by chunk between leader and follower will locate the drifted rows.
The durable fix is to change the log format. Row-based logging records the values the leader actually wrote, so every follower applies the same thing. MySQL has defaulted to row-based since 5.7.7. The cost is larger logs for bulk updates, because one statement touching a million rows becomes a million row events, which can add lag.
After switching, repair the drift by rebuilding the follower from a fresh snapshot of the leader. Patching a few rows by hand leaves you guessing about the ones you missed.
Sources
- Oracle (2024). MySQL reference manual: replication formats.
- Martin Kleppmann (2017). Designing Data-Intensive Applications.