Scrivio Interview library
DebuggingIntermediate6 min read · updated 13 November 2026

A MySQL read replica returns about 1,200 more active subscriptions than the primary for the same query. A week later replication on that replica stops with error 1062, duplicate entry for key PRIMARY.

How can a replica drift from its source, how do you find the differing rows, and how do you prevent drift?

A replica drifts when its data changes in ways replication does not know about: writes made directly on the replica by a script or a misconfigured service, non-deterministic statements under statement-based logging, or replication errors skipped by hand. Later the source inserts a key the replica already has, and replication stops with error 1062. We find the differing rows by checksumming table chunks on the source and comparing them on the replica, for example with pt-table-checksum, repair with a sync tool or a fresh clone, and prevent drift with super_read_only=ON and read-only database users.

Why this question is asked

Replica drift is quiet and dangerous: reports and APIs read from the replica return wrong numbers for weeks, and nobody notices until replication breaks. Interviewers ask about it to see whether we know that replication only copies changes and does not check that the result matches. In this answer, we will recap what replication guarantees, look at the ways a replica can drift, connect them to the two symptoms in the overview, find the differing rows, repair them, and set up the guards that stop it happening again.

1A Quick Recap: Replication Copies Changes, Not State

MySQL replication works by shipping the source's binary log to the replica, which applies the same changes in the same order. If the replica started as an exact copy and applies exactly those changes, it stays an exact copy. Notice the two assumptions: the starting copy was identical, and nothing else changes the replica. Replication never compares the two databases; it simply trusts that both assumptions hold.

With row-based logging, the default since MySQL 5.7.7, the binlog carries the before and after images of each changed row. Applying an update or delete looks up the row on the replica. If that row is missing, or a new row's primary key already exists, the applier stops with an error such as 1032 (row not found) or 1062 (duplicate entry).

2How a Replica Drifts

The most common cause is a direct write. An admin runs a cleanup script against the wrong host, or a service is configured with the replica's address as its write endpoint. Those rows exist only on the replica. In this incident, something inserted or reactivated about 1,200 subscriptions on the replica, so the replica's count is 1,200 higher. A week later the source inserts a subscription with one of those primary keys, the replica already has it, and replication stops with error 1062.

Non-deterministic statements are the second cause. Under statement-based logging, the replica re-executes the SQL text. A statement like DELETE FROM sessions WHERE expired = 1 LIMIT 1000 without an ORDER BY can delete different rows on each server, and functions such as UUID() or RAND() produce different values. Row-based logging avoids this because it ships the rows, not the statement.

The third cause is human: when replication stops, someone skips the failing transaction to get it running again, with sql_replica_skip_counter or by injecting an empty transaction for the failing GTID. Each skip throws away a change, so the replica silently loses it and drifts further.

Causes of drift and what they look like
CauseTypical symptomPrevention
Direct write on the replicaextra rows, later 1062 duplicate entrysuper_read_only=ON, read-only users
Statement-based non-determinismdifferent rows deleted or values setbinlog_format=ROW
Skipped replication errormissing changes, later 1032 row not foundnever skip; re-clone instead
Replica built from a bad copydifferences from day onephysical clone with recorded position

3Finding the Differing Rows

Counting rows tells us there is a difference but not where. The standard approach is chunked checksums. pt-table-checksum, from Percona Toolkit, runs on the source and splits each table into chunks of, say, 1,000 rows by primary key. For each chunk it runs a statement that computes a checksum of the rows and writes the result into a checksums table. That statement replicates, so the replica computes the same checksum over its own rows for the same chunk. Comparing the two columns per chunk shows exactly which ranges differ.

Java
pt-table-checksum --replicate=percona.checksums --databases=billing h=primary.db
-- then, on the replica:
SELECT db, tbl, chunk, this_cnt, master_cnt, this_crc, master_crc
FROM percona.checksums
WHERE master_cnt <> this_cnt OR master_crc <> this_crc;
-- billing | subscriptions | 412 | 1031 | 1000 | 8f1c... | 2b7a... -> 31 extra rows in chunk 412

Notice the clever part: because the checksum statement itself replicates, the replica computes its checksum at the same logical point in the change stream as the source did, so normal replication lag does not cause false differences. Column names in the checksums table still use the tool's older naming; check your version.

4Repairing: Sync or Re-Clone

For a small number of chunks, a sync tool such as pt-table-sync can generate the statements that make the replica match the source, executed on the source so they replicate. For widespread drift, or when we cannot be sure how far it goes, the cheapest clean fix is to throw the replica away and clone it again from the source with the clone plugin or a physical backup. A clone takes hours at most; investigating and patching thousands of rows by hand can take days and still leave doubts.

What we must not do is keep skipping errors until replication runs again. Each skip discards a real change, so it trades one visible error for several invisible differences. A stopped replica is honest; a running drifted one is not.

5Preventing Drift

Most drift comes from writes on the replica, so we make them impossible. read_only=ON blocks writes from ordinary accounts but not from accounts with administrative privileges such as SUPER or CONNECTION_ADMIN, and admin scripts often run as exactly those accounts. super_read_only=ON blocks writes from them too, while the replication applier can still apply changes.

SQL
-- on every replica (and in its my.cnf so it survives restarts)
SET PERSIST super_read_only = ON; -- also turns read_only on
-- as an admin account on the replica:
INSERT INTO billing.subscriptions VALUES (...);
-- ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option
-- so it cannot execute this statement
-- read paths get their own user, with no write grants at all
CREATE USER 'reports_ro'@'%' IDENTIFIED BY '***';
GRANT SELECT ON billing.* TO 'reports_ro'@'%';

Separate read-only users add a second layer: a service accidentally pointed at the primary with a read-only user cannot write either. Add binlog_format=ROW where it is not already the setting, a scheduled checksum run, weekly for example, and an alert on any replication error, and drift becomes something we catch in days rather than discover through wrong reports.

6Follow-up questions

Why did the duplicate error appear a week after the drift began?

The extra rows sat unnoticed until the source happened to insert a row with one of the same primary keys. With an auto-increment key, that happens when the source's counter reaches the ids the replica already used, so the delay depends on the insert rate.

Is read_only not enough?

No. read_only stops ordinary users, but accounts with administrative privileges can still write, and those are the accounts admin scripts and migrations often use. super_read_only closes that gap, which is why it belongs on every replica.

Can GTIDs detect drift?

Partly. A transaction executed directly on the replica gets a GTID with the replica's own server UUID, so seeing that UUID in the replica's gtid_executed shows that someone wrote there. It does not show which rows differ, which is why we still need checksums.

7Conclusion

In this answer, we saw that replication copies changes and trusts that nothing else touches the replica. Direct writes, non-deterministic statements and skipped errors break that trust, giving extra or missing rows and later errors such as 1062. Chunked checksums that replicate, as pt-table-checksum does, show which ranges differ; a sync tool fixes a few chunks, and a fresh clone is usually the cleanest repair. super_read_only, read-only users, row-based logging and regular checksums prevent it.

What to say in the interviewAbout 90 seconds aloud

Open with

Replication only ships changes; it never checks that the replica matches the source. So a replica drifts whenever something changes it outside replication. The usual cause is a direct write: an admin script run on the wrong host or a service configured with the replica as its write endpoint, which is probably where the 1,200 extra subscriptions came from.

Then, if they want more

A week later the source inserted one of those keys, the replica already had it, and the applier stopped with 1062. Other causes are non-deterministic statements under statement-based logging, like a DELETE with LIMIT and no ORDER BY, and people skipping replication errors to get things moving, which throws changes away.

To find the differences, I would run pt-table-checksum on the source. It checksums chunks of each table, and because the checksum statement replicates, the replica computes the same chunk at the same point in the stream, so comparing the two shows exactly which ranges differ.

For a handful of chunks I would sync them from the source; for anything widespread I would simply re-clone the replica, which is usually faster and leaves no doubt.

To prevent it, I set super_read_only on every replica, because plain read_only does not stop admin accounts, give read paths their own users with only SELECT, keep row-based logging, run checksums regularly, and never skip errors.

About this answer

Last updated
13 November 2026
Versions it names
MySQL 5.7
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How can a replica drift from its source, how do you find the differing rows, and how do...": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/2607b9ea64/