MySQL Replication Lag Soars to 10 seconds? Three Parallel‑Replication Tricks to Fix It
When MySQL replication latency jumps from milliseconds to 10 seconds, blindly raising replica_parallel_workers won’t help; the article walks through diagnosing the delay, then applies three concrete parallel‑replication optimizations—enabling WRITESET on the source, configuring LOGICAL_CLOCK with appropriate applier workers, and optionally preserving commit order—while showing the required SQL commands, monitoring queries, and rollback steps.
Understanding the 10‑second Lag
Seconds_Behind_Source is an approximate metric calculated from event timestamps and can be unreliable when network interruptions, I/O thread stops, long transactions, clock skew, replication filters, worker blockage, or manual SQL‑thread stops occur. Effective troubleshooting must examine I/O, SQL/applier, GTID sets, relay logs, and worker status together.
Step 1 – Verify Environment and Baseline
Confirm MySQL version, binary‑log format, GTID mode, and parallel‑replication variables. All queries in the guide should be run on the source or replica with roles recorded in a change‑ticket. Use a controlled login‑path to avoid exposing credentials.
# Record version and replication‑related variables
mysql --login-path=<replica_login_path> --batch --raw -e "
SELECT @@version AS mysql_version,
@@server_uuid AS server_uuid,
@@read_only AS read_only,
@@super_read_only AS super_read_only,
@@gtid_mode AS gtid_mode,
@@binlog_format AS binlog_format;"Check SHOW REPLICA STATUS (or SHOW SLAVE STATUS on older versions) and note Replica_IO_Running, Replica_SQL_Running, Seconds_Behind_Source, Last_IO_Error, Last_SQL_Error, Relay_Log_Space, and GTID sets. Any non‑empty error field must be resolved before increasing parallelism.
Step 2 – Eliminate Causes That Parallel Replication Cannot Fix
Parallel replication only speeds up the application of independent transactions. It cannot split a 30‑second DDL, a large batch UPDATE, a long‑running transaction, or a hotspot that serialises writes. First examine the source for long‑running transactions and write hotspots:
# List current InnoDB transactions on the source
SELECT trx_id, trx_started, trx_state, trx_rows_modified, trx_mysql_thread_id
FROM information_schema.innodb_trx
ORDER BY trx_started
LIMIT 20; # Identify heavy statements by digest
SELECT DIGEST_TEXT, COUNT_STAR, SUM_ROWS_AFFECTED, SUM_TIMER_WAIT
FROM performance_schema.events_statements_summary_by_digest
WHERE DIGEST_TEXT IS NOT NULL
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 20;If long transactions are found, investigate the originating application, slow‑query log, or audit records before killing sessions, as abrupt termination can cause roll‑backs and worsen latency.
Step 3 – The Three Parallel‑Replication “Killer Tricks”
Trick 1 – Make the Source Emit Dependency Information (WRITESET)
Enable transaction_write_set_extraction (commonly XXHASH64) and set binlog_transaction_dependency_tracking=WRITESET in the source’s [mysqld] section. Preserve existing variables, then persist the new settings:
# Backup current variables
SHOW GLOBAL VARIABLES WHERE Variable_name IN (
'binlog_format','gtid_mode','enforce_gtid_consistency',
'transaction_write_set_extraction','binlog_transaction_dependency_tracking');
# Persist the new settings
SET PERSIST transaction_write_set_extraction='XXHASH64';
SET PERSIST binlog_transaction_dependency_tracking='WRITESET';These changes affect only new binlog events; existing relay logs remain unchanged. Verify the new values with
SELECT @@global.binlog_transaction_dependency_tracking, @@global.transaction_write_set_extraction;.
Trick 2 – Enable Parallel Applier Workers on the Replica
After the source emits WRITESET information, configure the replica:
# Record current replica settings
SELECT @@global.replica_parallel_type AS parallel_type,
@@global.replica_parallel_workers AS parallel_workers,
@@global.replica_preserve_commit_order AS preserve_commit_order,
@@global.replica_pending_jobs_size_max AS pending_jobs_max;
# Change settings (stop SQL thread first)
STOP REPLICA SQL_THREAD FOR CHANNEL '<channel_name>';
SET PERSIST replica_parallel_type='LOGICAL_CLOCK';
SET PERSIST replica_parallel_workers=<worker_count>;
SET PERSIST replica_preserve_commit_order=ON; -- optional, see Trick 3
START REPLICA SQL_THREAD FOR CHANNEL '<channel_name>';Choose <worker_count> based on CPU, memory, and I/O capacity; start with a modest number and increase only after observing reduced latency without excessive scheduling overhead.
Trick 3 – Preserve Commit Order When Needed
Set replica_preserve_commit_order=ON so that parallel workers commit in the source’s original order. This is essential when read‑after‑write consistency matters, but it can reduce parallel gains because workers may wait for earlier transactions.
# Verify final configuration
SELECT @@global.replica_parallel_type AS parallel_type,
@@global.replica_parallel_workers AS parallel_workers,
@@global.replica_preserve_commit_order AS preserve_commit_order;
SHOW REPLICA STATUS FOR CHANNEL '<channel_name>'\GMeasure impact across a full business window: monitor Seconds_Behind_Source, relay‑log growth, worker error counts, CPU, disk I/O, and application query latency. If latency drops but other resources saturate, the bottleneck has simply moved.
Step 4 – Continuous, Repeatable Monitoring
Collect replication health metrics at regular intervals to compare before‑and‑after performance:
# Simple bash loop to sample replica status every 10 seconds
while true; do
date --iso-8601=seconds
mysql --login-path=<replica_login_path> --batch --raw -e "
SHOW REPLICA STATUS FOR CHANNEL '<channel_name>';"
sleep 10
doneExported metrics (e.g., mysql_slave_status_seconds_behind_master or mysql_slave_status_sql_running) should be used in Prometheus alerts rather than manual eyeballing.
Step 5 – Safe Rollback Procedure
Before any change, back up the three replica variables (parallel type, worker count, commit‑order flag). To revert:
# Restore original settings
STOP REPLICA SQL_THREAD FOR CHANNEL '<channel_name>';
SET PERSIST replica_parallel_workers=<old_workers>;
SET PERSIST replica_parallel_type='<old_parallel_type>';
SET PERSIST replica_preserve_commit_order=<old_commit_order>;
START REPLICA SQL_THREAD FOR CHANNEL '<channel_name>';
SHOW REPLICA STATUS FOR CHANNEL '<channel_name>';After rollback, re‑verify worker states, latency, and error logs to ensure the issue was not caused by unrelated resource constraints.
Key Takeaways
Parallel replication helps only when the source produces many fine‑grained ROW transactions.
It cannot overcome ultra‑long transactions, hotspot rows, DDL locks, I/O saturation, or network failures.
Follow the three‑step chain – enable WRITESET on the source, configure LOGICAL_CLOCK with appropriate workers on the replica, and optionally preserve commit order – while validating each step with the provided SQL queries.
Continuous monitoring and a documented rollback path are essential for safe production changes.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
