A MySQL replica is lagging far behind. What could cause it and what do you do?

IntermediateTroubleshootingMySQL
Answer

Short answer

The replica can't apply changes as fast as the source produces them. Common causes: a big transaction (huge UPDATE/DELETE or ALTER TABLE), a weaker replica instance, single-threaded apply, heavy read queries on the replica, missing primary keys (row-based replication then scans tables), or slow disk.

Investigate

SHOW REPLICA STATUS\G           -- Seconds_Behind_Source, Last_SQL_Error, which binlog event
SHOW PROCESSLIST;               -- what the applier is doing; long-running read queries

Also check replica CPU, disk IOPS and the source's write rate (Performance Insights / CloudWatch ReplicaLag).

Fixes

Cause Fix
One huge transaction Break bulk operations into batches
Big ALTER TABLE Use online schema tools (gh-ost, pt-online-schema-change)
Single-threaded apply Enable parallel replication: replica_parallel_workers=8, replica_parallel_type=LOGICAL_CLOCK
Tables without PK Add primary keys
Replica undersized / slow disk Same instance class as source, faster storage
Heavy reporting queries Separate reporting replica, or optimise queries

Impact to explain

Apps reading from a lagging replica see stale data; failing over to it would lose the unapplied changes.

What to say in the interview

"Lag means the applier can't keep up. I check SHOW REPLICA STATUS and processlist for a large transaction or long queries, then fix the root cause: batch large writes, use online schema change tools, enable parallel apply, add missing primary keys or right-size the replica."

All Databases (MySQL, PostgreSQL, MongoDB) questions Roadmap: MySQL basics

More on MySQL