MySQL Replica Lag: What Really Slows a Replica Down

Chart concept of MySQL replica lag growing behind a source server

MySQL replica lag usually isn’t a network or disk problem. In a set of tests on MySQL 8.4.13, a source and replica sitting 0.1 ms apart on one laptop still drifted far apart. Sixteen sessions inserting 64,000 single-row transactions needed 26 seconds on the source but 92 on the replica, taking the median of three runs. A single UPDATE that ran in 0.27 seconds on the source cost the replica 82.

Lag has concrete costs. Readers hitting the replica get stale data, and a failover must wait for the replica to catch up before promotion. In November 2020, abnormal lag in a GitHub MySQL cluster made requests that read from lagging replicas fail.

Why the MySQL replica lag number misleads

The Seconds_Behind_Source field in SHOW REPLICA STATUS reports the age of the transaction currently being applied. It measures the applier against the receiver, not the source, so if the receiver is also behind the field can sit at 0.

In the test, 16 sessions wrote for 20 seconds. At the stop, the replica trailed by 43,113 transactions yet the field showed 16. Clearing the backlog took 68 more seconds, and the field rose to 67 along the way. It gauges staleness of the newest visible row, not the size of the queue. Comparing Retrieved_Gtid_Set with Executed_Gtid_Set, the per-worker tables in performance_schema, or a heartbeat table (pt-heartbeat) gives a better answer.

Small transactions and big ones both hurt

The source uses group commit, letting sixteen commits split a single disk sync. The replica applies with four workers by default and commits every transaction again, in order. One session writing a row at a time never caused lag: 10,000 commits took the source 33.7 seconds and the replica stayed within 0.13 seconds. Trouble comes from concurrent small transactions, so batching helps.

Batching has a ceiling, because a transaction reaches the binary log only at commit. In one run a session inserted 1,000,000 rows at once. The replica had to wait for the commit, spent five seconds applying, and a tiny commit right behind it waited 4.9 seconds. An hour-long batch job in one transaction leaves replicas an hour behind. Between 1,000 and 10,000 rows per transaction worked here, though row size changes that.

Missing primary keys and schema changes

With row-based replication, the replica must find every changed row. A primary key makes that an index lookup. Without one, the replica scans the table for each row event. An UPDATE of 50,000 rows spread through a table took the replica about five times longer when the table was five times larger, while the source barely changed. Setting sql_generate_invisible_primary_key = ON adds a hidden key, and sql_require_primary_key = ON refuses tables without one.

Schema changes can’t run in parallel on the replica either. An ALTER TABLE that finished in 17 milliseconds on the source stalled every following commit for 28 seconds. A rebuild lasting an hour leaves the replica an hour late, so gh-ost and pt-online-schema-change copy tables in small throttled chunks.

The replica applies rows, not queries

With row-based replication the replica never evaluates the WHERE clause. Two UPDATEs that touched the same 11,266 rows, one picking them with a heavy aggregate and one with a key range, wrote identical binary logs and cost the replica the same time. Query tuning helps the source and leaves the replica alone. An UPDATE that changes no values sends nothing, but a job stamping updated_at = NOW() on each visited row turns those no-ops into real work.

Unused indexes cost more. A 64,000-row burst into a table of 2,000,000 rows carrying indexes nobody queried cost about two seconds with one 42 MB index and over twenty with three. Once the indexes outgrew the 128 MB buffer pool, disk reads on the replica rose from 148 to 99,363. sys.schema_unused_indexes lists candidates. Check every replica before dropping, and make an index invisible first to test.

A hot row wasn’t a cause. Sixty-four thousand updates to one counter row took the source about 155 seconds and the replica about 165, because the source was already serialized on the row lock.

Reads, locks and settings

Reads compete with the applier. With sysbench driving 16 read threads on a single CPU, about 32,000 queries a second, the replica barely applied anything and trailed by nearly all of the 64,000 rows at worst. Real hardware gives the applier a larger share, but a reporting replica is the likeliest to lag. Options: a separate reporting replica, MAX_EXECUTION_TIME limits, or more CPU than the source.

An idle transaction on the replica can stall everything. A SELECT left open in a transaction held a metadata lock, so an instant ALTER that took 0.017 seconds on the source blocked every later commit on the replica for up to 28 seconds. A short wait_timeout for reporting accounts, or checking information_schema.innodb_trx before an ALTER, prevents it.

Relaxed durability (innodb_flush_log_at_trx_commit = 2 and sync_binlog = 0) removed the lag entirely, but can lose up to a second of applied work after a crash, so it fits only an emergency catch-up on a replica kept out of failover. On 8.4, binlog_group_commit_sync_delay just throttles the source.

Semi-synchronous replication doesn’t reduce lag: a 1,000,000-row UPDATE committed slower with it, and the replica still lagged the change by 3 to 5 seconds. Group Replication’s flow control can bound lag by slowing the primary, but its default threshold of 25,000 transactions was never reached in the test, and it rejected a 351 MB transaction over its 150,000,000-byte default limit.

Where to start with MySQL replica lag

On a quiet source, look at the replica itself: long or idle transactions, a growing history list length, threads waiting on table metadata locks and read load. On a busy source, look at commit rate, batch transaction size, recent ALTERs and tables without primary keys. Then tune replica_parallel_workers. Settings raise the ceiling. The workload is what usually hits it: heavy row-changing statements, unused indexes and reports on the replica.

Source: EXPLAIN ANALYZE

Written by

Saksham Bhargava

Saksham Bhargava runs Alteroid and has been writing about consumer technology for over five years, covering iPhone, Android, Windows, AI tools and gaming. With an MCA and six years of hands-on web development experience, he tests the features he writes about on his own devices and focuses on clear, practical guides that solve real problems. Away from the screen, he enjoys travelling to places most tourists skip.