Quick revision
The short answer
The nightly UPDATE changes 40 million rows in one transaction, so it reaches the binlog as one enormous event group only when it commits, and the replica starts applying it only then, as a single transaction that nothing else can overtake. Lag therefore grows by the whole duration of the job. We confirm it with SHOW REPLICA STATUS and the performance_schema replication tables, then split the job into small committed batches by primary key, throttled on replica lag, enable parallel replication for the many small transactions, and make sure every table has a primary key.
What to remember
In this answer, we saw that one 40 million row UPDATE reaches the replica only at commit, as one transaction that a single applier thread must replay while everything else waits, so lag grows by the full job duration. SHOW REPLICA STATUS and replication_applier_status_by_worker confirm it. The fix is to batch the job by primary key with a commit per batch and throttling on replica lag, then let parallel replication with preserved commit order apply the small transactions concurrently, and make sure every table has a primary key so row-based apply can find rows quickly.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The nightly UPDATE changes 40 million rows in one transaction, so it reaches the binlog as one enormous event group only when it commits, and the replica starts applying it only then, as a single transaction that nothing else can overtake. Lag therefore grows by the whole duration of the job. We confirm it with SHOW REPLICA STATUS and the performance_schema replication tables, then split the job into small committed batches by primary key, throttled on replica lag, enable parallel replication for the many small transactions, and make sure every table has a primary key.
Why this question is asked
Replica lag that appears every night at the same time is almost always a batch job, and the frustrating part is that the job itself looks harmless: one simple UPDATE. Interviewers ask this question because the fix depends on understanding how row-based replication applies transactions, and because the obvious fixes, like more replica threads, do not help on their own. In this answer, we will recap how a replica applies changes, see why one big transaction creates hours of lag, diagnose it with the right status views, and then fix the job, the replica configuration and the schema.
1A Quick Recap of How a Replica Applies Changes
With row-based replication, the default, the primary writes each committed transaction to its binary log as a group of row events: for every changed row, the before and after images. The replica's receiver thread copies these events into its relay log, and applier threads replay them in the replica's own InnoDB. Two rules matter here. A transaction appears in the binlog only when it commits, never while it is running. And the replica applies one transaction as one unit; it can apply different transactions in parallel when they do not touch the same rows, but it cannot split one transaction across threads.
2Why One UPDATE Creates Two Hours of Lag
Suppose the UPDATE on the primary runs for 50 minutes. During those 50 minutes nothing reaches the replica, because the transaction is not committed. At commit, roughly 40 million row images, often several gigabytes, land in the binlog at once. The replica receives them and begins a single 40 million row transaction, which takes about as long as it took on the primary, often longer because the replica has a smaller instance or is also serving reads. Every transaction that commits on the primary afterwards queues behind it, and Seconds_Behind_Source climbs for the whole time.
So the lag grows by the duration of the job, and then by the time to drain the backlog. Two hours is very plausible.
6 messages in order. Play them, or step through them one at a time.
The replica cannot start before the commit and cannot split the transaction.
3Diagnosing It
We confirm the story with the replica's own status. SHOW REPLICA STATUS gives the headline numbers, and performance_schema shows which transaction each applier thread is working on and since when.
One worker busy on one GTID for over an hour, while the others wait, points straight at a single large transaction. On the primary, mysqlbinlog on that night's binlog files or the size of the binlog written around 01:50 shows the same thing.
4Fix 1: Split the Job Into Small Batches
The main fix is to change the job so it produces many small transactions instead of one huge one. We walk the table by primary key in ranges of a few thousand rows, commit each range, and pause whenever the replica falls behind.
Each batch touches a narrow primary key range, so it uses the clustered index efficiently and holds locks only briefly. Notice the archived = 0 condition, which makes the job safe to restart after a failure. The replica now applies each 5,000 row transaction within a fraction of a second of the primary, and the throttle keeps lag bounded even on a busy night. Tools such as pt-archiver follow the same idea.
5Fix 2: Apply Independent Transactions in Parallel
Once the work arrives as many transactions, the replica can apply several at the same time. replica_parallel_workers sets the number of applier threads (4 by default in recent 8.0 releases), and replica_preserve_commit_order keeps the commit order the same as on the source, so readers never see changes out of order. To decide which transactions are independent, the source tracks the rows each one writes, its writeset; in 8.4 writeset-based dependency tracking is always used, while older 8.0 releases needed binlog_transaction_dependency_tracking=WRITESET; check your version.
Notice that parallel replication alone would not have fixed the original job. One transaction is always applied by one thread, so it helps only after the batching fix.
6Fix 3: Make Sure Every Table Has a Primary Key
With row-based replication, the replica finds each changed row by its key. If a table has no primary key and no usable unique index, the replica may have to scan the table for every row event, and a bulk change on such a table can lag for days. We check for these tables and add keys. MySQL 8.0 can refuse to create tables without one (sql_require_primary_key) or add an invisible generated key automatically (sql_generate_invisible_primary_key, since 8.0.30).
7Follow-up questions
Would binlog_row_image=MINIMAL help?
It reduces the size of each row event, because only the key and the changed column are logged instead of the full before and after rows, so the binlog for the night shrinks a lot. That speeds up transfer and relay log writes, but the replica still applies 40 million row changes in one transaction, so it is a useful addition, not the fix. It also matters if a CDC tool needs full row images.
Why not run the job directly on each replica instead?
Because then the replicas and the primary would change data independently, and replication would either conflict or replicas would drift. Data changes go through the primary; we make them friendly to replication instead.
How big should each batch be?
Large enough to be efficient and small enough that one batch applies on the replica in well under a second; a few thousand rows is a good start. We measure the time per batch and the replica lag and adjust.
8Conclusion
In this answer, we saw that one 40 million row UPDATE reaches the replica only at commit, as one transaction that a single applier thread must replay while everything else waits, so lag grows by the full job duration. SHOW REPLICA STATUS and replication_applier_status_by_worker confirm it. The fix is to batch the job by primary key with a commit per batch and throttling on replica lag, then let parallel replication with preserved commit order apply the small transactions concurrently, and make sure every table has a primary key so row-based apply can find rows quickly.
Open with
The UPDATE is one transaction over 40 million rows. With row-based replication, nothing reaches the binlog until it commits, and then the replica gets one huge group of row events and has to apply it as a single transaction, on one thread, while every newer transaction queues behind it.
Then, if they want more
So the lag grows by roughly the whole duration of the job plus the time to drain the backlog. I would confirm it with SHOW REPLICA STATUS, where Seconds_Behind_Source climbs, and with performance_schema replication_applier_status_by_worker, which would show one worker stuck on one GTID for over an hour.
The fix is in the job: walk the table by primary key in batches of a few thousand rows, commit each one, make the condition restartable, and pause when replica lag goes above a few seconds.
Once the work is many small transactions, parallel replication helps: replica_parallel_workers with replica_preserve_commit_order, and writeset dependency tracking, which is always used in 8.4 and had to be enabled on older 8.0; I would check the version. Parallel workers alone would not help the original job, because one transaction is never split.
Finally I would make sure every table has a primary key, because row-based apply on a table without one can scan the table for each row.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8.0
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- MySQL reference manual
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 "A read replica falls 2 hours behind every night when a batch job runs UPDATE events SET...": https://scrivio-interview-library.netlify.app/databases/mysql/c8a4049564/