Asynchronous replication is MySQL’s default and most flexible topology — the primary writes a binary log, replicas read it and apply it. It is also the topology where everything from a slow query on the replica to a wrong binlog format to a missing index manifests as growing lag. This article is the tuning we apply to async replication in production: the variables we set, the limits we monitor, and the operational discipline that keeps lag in seconds, not hours.
How to verify
A healthy replica reports zero or single-digit seconds of lag, an IO and SQL thread both running, and a binlog position that advances every second.
mysql -e "SHOW REPLICA STATUS\G" | grep -E '(Running|Seconds_Behind|Source_Log|Relay)'
mysql -e "SHOW GLOBAL STATUS LIKE 'Binlog_cache%';"
mysql -e "SHOW GLOBAL VARIABLES LIKE 'binlog_%';"
mysql -e "SHOW GLOBAL VARIABLES LIKE 'sync_binlog';"
mysql -e "SHOW GLOBAL VARIABLES LIKE 'innodb_flush_log_at_trx_commit';"
mysql -e "SHOW PROCESSLIST" | grep -i replica
Replica_IO_Running=Yes, Replica_SQL_Running=Yes, Seconds_Behind_Source=0 is the steady state. The Last_*_Error fields must be empty; a non-empty error means replication is stopped at that statement.
What’s happening
Asynchronous replication has two threads on the replica. The IO thread connects to the primary, reads binlog events, and writes them to the local relay log. The SQL thread (or worker threads, with parallel replication) reads from the relay log and applies the events to the data. Lag can grow on either side — IO thread lag means the network is slow or the primary is producing events faster than the replica can pull them; SQL thread lag means apply is the bottleneck.
The binlog format decides what gets replicated. ROW is the production default: every changed row is logged. It is deterministic, safe with non-deterministic statements, and necessary for some features (GTID auto-positioning, semi-sync). STATEMENT logs the SQL itself; smaller binlogs, but breaks on NOW(), UUID(), and any statement whose result depends on context. MIXED picks per statement; we leave it on ROW and accept the binlog volume.
Parallel replication (replica_parallel_workers > 1) is the lever that closes the gap between primary write throughput and single-threaded apply. Two flavours: schema-based (transactions in different databases apply in parallel) and logical-clock (transactions that committed concurrently on the primary apply concurrently on the replica). Modern MySQL uses LOGICAL_CLOCK, which is dramatically better.
The durability variables — sync_binlog, innodb_flush_log_at_trx_commit — decide what survives a primary crash. sync_binlog=1 fsyncs the binlog at every commit; innodb_flush_log_at_trx_commit=1 fsyncs the InnoDB redo log at every commit. Both at 1 is the durable setting and what we run in production.
The procedure
-
Set the primary’s binlog and durability variables in
my.cnf.[mysqld] server_id = 1 log_bin = /var/lib/mysql/binlog binlog_format = ROW binlog_row_image = FULL sync_binlog = 1 innodb_flush_log_at_trx_commit = 1 binlog_expire_logs_seconds = 604800 max_binlog_size = 1G gtid_mode = ON enforce_gtid_consistency = ON -
On the replica, set parallel apply and a few of the variables that absorb hiccups without lag.
[mysqld] server_id = 2 relay_log = /var/lib/mysql/relay relay_log_recovery = ON replica_parallel_workers = 8 replica_parallel_type = LOGICAL_CLOCK replica_preserve_commit_order = ON replica_compressed_protocol = ON read_only = ON super_read_only = ON skip_replica_start = OFFreplica_preserve_commit_order=ONis the safety belt — with parallel apply, the order in which workers commit can differ from the primary unless you tell MySQL to preserve it. Applications that read their own writes will see causal anomalies otherwise. -
Bring the replica up against a backup taken with
xtrabackupormariabackup. Readxtrabackup_binlog_infofor the GTID set.SET GLOBAL gtid_purged = '<gtid-set from xtrabackup_binlog_info>'; CHANGE REPLICATION SOURCE TO SOURCE_HOST = '10.0.0.11', SOURCE_USER = 'repl', SOURCE_PASSWORD = 'REPL_PASS', SOURCE_AUTO_POSITION = 1, SOURCE_SSL = 1; START REPLICA; SHOW REPLICA STATUS\G -
Confirm parallel apply is engaging by inspecting the worker threads. The replica should show multiple
Worker_*threads inSHOW PROCESSLISTduring apply.mysql -e "SELECT * FROM performance_schema.replication_applier_status_by_worker;" -
Watch lag with the actual measurement, not
Seconds_Behind_Source. That column reports the timestamp gap of the currently-applying event, which can read zero between bursts even when the replica is hours behind. Usept-heartbeator a custom heartbeat row updated on the primary every second.pt-heartbeat --update --replace --interval 1 --database percona --table heartbeat \ --user=repl --password=REPL_PASS h=primary & pt-heartbeat --monitor --database percona --table heartbeat \ --user=repl --password=REPL_PASS h=replica -
If lag persists, find the slow query on the replica. The
events_statements_summary_by_digestshows what the SQL thread is spending time on;Innodb_rows_insertedrate on the primary vs the replica shows whether apply throughput keeps up.
Common pitfalls
binlog_format=STATEMENTwith non-deterministic statements —INSERT ... SELECTordering,UPDATE ... LIMITwithoutORDER BY, time functions all replicate differently.ROWis the only safe production choice unless you have measured the binlog volume cost and accept it.replica_preserve_commit_order=OFFwith parallel apply — application reads on the replica can see writes out of order; the primary’s commit order is not preserved.Seconds_Behind_Source=0as the only lag metric — that field reports the timestamp gap of the event being applied; it can show 0 when the replica is idle and there is no event to read but events are still queued in the relay log. Use a heartbeat-based metric.- Replication user with too many grants —
replneedsREPLICATION SLAVE(andREPLICATION CLIENTfor monitoring). GrantingSUPERorALLmakes it a footgun. - No
binlog_expire_logs_seconds— binlogs accumulate forever and fill the disk. Set retention to cover the longest acceptable replica downtime plus a buffer.
Async replication holds up well when the tuning is right; it is fragile when the binlog format is wrong, the parallel workers are misconfigured, or the durability settings have been quietly relaxed for “speed.” In the engagements we run, replication tuning is part of the host’s MySQL build, lag is monitored with pt-heartbeat, and the replicas are part of the failover plan that ProxySQL or MaxScale executes. For the broader pattern of operating MySQL replication topologies, see /en/solutions/clustered-environments/.