Databases Bug Report

After a failover, the new MySQL primary had writes the old one never saw

A planned switchover went fine until the old primary was re-attached as a replica and replication stopped with a duplicate key error. The promoted replica had local transactions of its own: errant GTIDs from a maintenance script. How we found them, repaired the topology and enforced read_only.

Mateusz Kowalczyk Mateusz Kowalczyk @mateuszkowalczyk · November 24, 2026 · 8 min read · 44 Views
Banner: errant GTID transactions on a MySQL replica after failover

We did a planned switchover of a MySQL cluster to patch the primary's host: promote replica db-b to primary, point the application at it, patch db-a, then re-attach db-a as a replica of db-b. The first three steps went smoothly. Two minutes after re-attaching db-a, its replication SQL thread stopped:

mysql> SHOW REPLICA STATUS\G
...
       Replica_IO_Running: Yes
      Replica_SQL_Running: No
           Last_SQL_Errno: 1062
           Last_SQL_Error: Coordinator stopped because there were error(s) in the worker(s).
                           The most recent failure being: Worker 1 failed executing transaction
                           '7c1e4b52-8e2f-11ef-9a51-0242ac120003:418' at source log binlog.000231,
                           end_log_pos 18822. Error 'Duplicate entry 'feature_flags-3' for key
                           'settings.PRIMARY'' on query.

db-a was applying a transaction from db-b and found a row already present. But db-a had been the primary until a few minutes before; it should have had every row db-b had, and nothing more.

The transaction's GTID was the clue. Its source UUID, 7c1e4b52-..., was not db-a's server UUID. It was db-b's own. In a correctly working replica, every transaction in its history should come from the primary. A transaction with the replica's own UUID means somebody wrote directly to the replica.

Finding the errant transactions

Every MySQL server with GTIDs enabled records the set of transactions it has executed in gtid_executed. We compared the two servers:

-- on db-a (old primary)
SELECT @@global.gtid_executed\G
-- 3f9d0a10-8e2f-11ef-86b3-0242ac120002:1-88412093

-- on db-b (new primary)
SELECT @@global.gtid_executed\G
-- 3f9d0a10-8e2f-11ef-86b3-0242ac120002:1-88412093,
-- 7c1e4b52-8e2f-11ef-9a51-0242ac120003:1-431

GTID_SUBTRACT gives the transactions present on one server and not the other:

SELECT GTID_SUBTRACT(
  '3f9d0a10-8e2f-11ef-86b3-0242ac120002:1-88412093,7c1e4b52-8e2f-11ef-9a51-0242ac120003:1-431',
  '3f9d0a10-8e2f-11ef-86b3-0242ac120002:1-88412093'
) AS errant;
-- 7c1e4b52-8e2f-11ef-9a51-0242ac120003:1-431

431 transactions executed on db-b while it was still a replica. When db-a was re-attached to db-b, GTID auto-positioning did exactly what it is designed to do: it sent db-a every transaction db-b had that db-a did not. That included all 431 local writes, which db-a had never seen and which conflicted with its data.

Where they came from

Using mysqlbinlog on db-b's binary logs, we decoded the errant transactions:

mysqlbinlog --include-gtids='7c1e4b52-8e2f-11ef-9a51-0242ac120003:1-431' \
  --base64-output=DECODE-ROWS -v binlog.000198 binlog.000199 | grep -E '^### (INSERT|UPDATE|DELETE)' | sort | uniq -c
    412 ### DELETE FROM `app`.`session_cache`
     18 ### UPDATE `app`.`settings`
      1 ### INSERT INTO `app`.`settings`

A cleanup cron job, configured months earlier, deleted expired rows from session_cache. It had been pointed at the read replica "to keep load off the primary". A developer had also once updated a few settings rows on the replica while debugging, believing the replica was a disposable copy.

The deletes had quietly made db-b's session_cache differ from the primary's. The settings writes made settings differ. None of it caused visible problems while db-b was a replica, because the application only read from it.

Why replicas accept writes at all

A MySQL replica is a normal server that also applies changes from a primary. Unless told otherwise, it accepts writes from any client with the right privileges. Those writes are binary-logged with the replica's own server UUID and become part of its gtid_executed.

read_only = ON blocks writes from ordinary users, but users with the CONNECTION_ADMIN or SUPER privilege can still write. super_read_only = ON blocks those too. Replication's own applier threads are exempt from both, so replication keeps working.

Our replicas had read_only = ON. The cleanup job and the debugging session both used an administrative account with CONNECTION_ADMIN, so read_only did not stop them.

Why errant GTIDs hurt at failover, not before

While db-b was a replica, its extra transactions were invisible: nothing read session_cache from the replica, and nothing replicated from it. The moment it became the primary, two things happened:

  1. Its extra history became the official history. Any replica attached to it receives the errant transactions.
  2. Its divergent data became production data. The settings changes were now live.

The second point was the more serious. One of the settings updates had disabled a feature flag during that old debugging session. For two minutes after the switchover, that feature was off in production.

Environment

Item Value
MySQL 8.0.39, GTID mode ON, enforce_gtid_consistency = ON, row-based replication
Topology 1 primary, 2 replicas, switchover managed by an orchestration tool
Replica config read_only = ON, super_read_only = OFF
Errant writes 431 transactions from a cron job and a manual session

1. Repair the immediate situation

We had to decide which history was correct. For session_cache, the answer was "it does not matter": expired sessions, safe to delete anywhere. For settings, the primary's (db-a's) values were correct, and the replica's debugging edits were not.

  1. Restored the correct settings rows on db-b (now primary) from db-a, after confirming with the feature owners. These new writes replicate normally.
  2. Rebuilt db-a as a fresh replica of db-b from a clone (CLONE INSTANCE FROM with the clone plugin), which avoids reconciling the 431 conflicting transactions row by row.
  3. Ran pt-table-checksum across all replicas to confirm no other tables differed.

An alternative for errant transactions that are harmless is to inject empty transactions with the same GTIDs on every other server, so all servers consider them executed without applying them. We used that for the session_cache deletes on the third replica, which still had db-a's history:

SET GTID_NEXT = '7c1e4b52-8e2f-11ef-9a51-0242ac120003:1';
BEGIN; COMMIT;
SET GTID_NEXT = 'AUTOMATIC';
-- repeated for each errant GTID, generated by a script

Empty transactions only make sense when the data effect of the errant transactions is acceptable to skip. For the settings changes it was not, which is why db-a was rebuilt instead.

2. Enforce super_read_only on every replica

# my.cnf on replicas
[mysqld]
read_only = ON
super_read_only = ON

The orchestration tool turns super_read_only off only on the server it promotes, at the moment of promotion, and on for the demoted one. We confirmed it does this for both planned switchovers and unplanned failovers.

3. Move the jobs and limit the accounts

  • The session_cache cleanup job now runs against the primary, in small batches with a short sleep, so it does not need a replica.
  • The administrative account with CONNECTION_ADMIN is used only by the orchestration tool. Engineers and jobs use accounts without it. The application's replica connections use a user with SELECT only.

4. Check for errant GTIDs before every switchover

Our orchestration tool supports a pre-switchover check, and we added our own script to monitoring:

-- run on each replica; result must be empty
SELECT GTID_SUBTRACT(@@global.gtid_executed, :primary_gtid_executed) AS errant;

Monitoring runs it every five minutes. An errant GTID now pages the database on-call within minutes of the first stray write, instead of being discovered at failover.

1. Writes on replicas are refused. On every replica, with the administrative account:

mysql> DELETE FROM app.session_cache WHERE expires_at < NOW() LIMIT 1;
ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement

2. No errant GTIDs across the fleet. The check returns an empty set on all 14 replicas in our three clusters. It found errant transactions on two replicas in another cluster on its first run, also from cleanup scripts, which we fixed the same way before they could cause trouble.

3. Switchover drill. We repeated the switchover on staging and then production with the new configuration. Re-attaching the old primary worked without errors, and pt-table-checksum reported no differences.

4. Security implications. A replica that accepts writes is a place where data can be changed without going through the application's validation or audit logging. super_read_only plus SELECT-only application users on replicas means changes can only enter through the primary, where they are audited.

Takeaways

  • Writes made directly on a replica become errant GTIDs. They are harmless until that replica is promoted.
  • read_only does not stop privileged users. Set super_read_only = ON on every replica.
  • Detect errant GTIDs continuously with GTID_SUBTRACT, not at failover time.
  • Repair by rebuilding the affected replica, or inject empty transactions only when the data effect is safe to skip.

References: MySQL replication with GTIDs, GTID functions, GTID_SUBTRACT, super_read_only system variable, MySQL clone plugin.

Add a label

#mysql #replication #failover #high-availability #gtid
Written by
Mateusz Kowalczyk @mateuszkowalczyk

I started as an application developer and drifted toward the database because that is where our incidents kept ending up. For the last eight years I have looked after PostgreSQL and MySQL clusters for e-commerce and SaaS teams, from schema reviews to late-night failovers. Relational databases are well documented, yet the same surprises keep catching good engineers: a NOT IN that returns nothing because of one NULL, an index silently skipped after a type conversion, a FOR UPDATE that locked far more than intended, a replica that disagreed with its primary after promotion. In each post I reproduce the problem on a small dataset, read the plan or lock output together with you, explain the rule behind it and show the fix with numbers before and after. Where engines differ, I run the same test on both and show the results side by side.

Advertisement

Comments

0

No comments yet. Be the first to comment!

Sign In to post a comment.