Q: How do you diagnose and resolve severe replication lag in GTID multi-threaded MariaDB replicas?
MariaDB interview questions on replication lag: read SHOW REPLICA STATUS, tune slave_parallel_threads and mode, fix large transactions and missing primary keys.
Want to master this scenario in a live sandbox? KodeKloud's PostgreSQL Database Administration & High Availability Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: Replication lag is a favourite topic in mariadb interview questions because the symptom, Seconds_Behind_Master climbing, has several unrelated causes. I split the problem first: is the replica's IO thread behind (network or primary binlog volume) or is the SQL thread behind (apply speed)? Then I look at what the apply threads are doing. Most severe cases I have seen come down to a single large transaction, a table without a primary key on row-based replication, or parallel replication that is configured but not actually running in parallel.
Read the replica status and separate IO lag from SQL lag
On MariaDB 10.5 and later I can use SHOW REPLICA STATUS; older versions use SHOW SLAVE STATUS and the output field names stay the same. Seconds_Behind_Master only measures the SQL thread against the event timestamp, so I also compare Gtid_IO_Pos, which is what the IO thread has received, with the replica's applied position. A large gap between the primary's binlog position and Gtid_IO_Pos points to network or IO thread trouble.
mariadb -e "SHOW REPLICA STATUS\G" | grep -E 'Slave_IO_Running|Slave_SQL_Running|Seconds_Behind_Master|Using_Gtid|Gtid_IO_Pos|Slave_SQL_Running_State|Last_.*Error'
# Slave_IO_Running: Yes
# Slave_SQL_Running: Yes
# Seconds_Behind_Master: 3412
# Using_Gtid: Slave_Pos
# Gtid_IO_Pos: 0-1-9182734
mariadb -e "SELECT @@gtid_slave_pos, @@gtid_current_pos;"
# run on the primary: SELECT @@gtid_binlog_pos;
Look at what the applier threads are doing
SHOW PROCESSLIST on the replica shows the SQL thread and the parallel worker threads. If one worker runs a single statement for minutes while others wait with 'Waiting for prior transaction to commit', I have a large or slow transaction blocking in-order commit. If every worker sits idle, parallelism is not configured or the workload is not parallelisable.
mariadb -e "SELECT ID, USER, TIME, STATE, LEFT(INFO,80) AS info FROM information_schema.PROCESSLIST WHERE USER='system user' ORDER BY TIME DESC;"
# 12 system user 1840 Updating UPDATE orders SET ...
# 14 system user 1838 Waiting for prior transaction to commit
# 15 system user 1838 Waiting for prior transaction to commit
Enable and tune parallel replication
The variable slave_parallel_threads defaults to 0, which means serial apply. I stop the replica, set a thread count around the number of CPU cores I can spare, and choose slave_parallel_mode. Conservative only runs transactions in parallel when they committed together on the primary, which is safe. Optimistic tries to run nearly everything in parallel and rolls back on conflicts. I raise the primary's binlog_commit_wait_count and binlog_commit_wait_usec slightly so more transactions share a commit group.
STOP REPLICA;
SET GLOBAL slave_parallel_threads = 8;
SET GLOBAL slave_parallel_mode = 'optimistic';
START REPLICA;
-- Persist in my.cnf
-- [mysqld]
-- slave_parallel_threads = 8
-- slave_parallel_mode = optimistic
-- On the primary, widen group commit a little
SET GLOBAL binlog_commit_wait_count = 8;
SET GLOBAL binlog_commit_wait_usec = 1000;
Fix large transactions and tables without primary keys
With binlog_format=ROW, a table that lacks a primary key forces the replica to scan the table for every changed row, which turns one bulk update into hours of apply time. I find those tables and add keys. Large single transactions also serialise apply, so I ask application owners to batch deletes and updates into chunks of a few thousand rows.
SELECT t.TABLE_SCHEMA, t.TABLE_NAME
FROM information_schema.TABLES t
LEFT JOIN information_schema.TABLE_CONSTRAINTS c
ON c.TABLE_SCHEMA=t.TABLE_SCHEMA AND c.TABLE_NAME=t.TABLE_NAME AND c.CONSTRAINT_TYPE='PRIMARY KEY'
WHERE t.TABLE_TYPE='BASE TABLE' AND c.CONSTRAINT_NAME IS NULL
AND t.TABLE_SCHEMA NOT IN ('mysql','information_schema','performance_schema','sys');
-- Chunked delete instead of one giant transaction
DELETE FROM events WHERE created_at < '2026-01-01' LIMIT 5000;
Confirm recovery with GTID positions and a trend, not one reading
I watch Seconds_Behind_Master over several minutes and confirm the replica's applied GTID approaches the primary's binlog GTID. If lag keeps growing despite idle workers, I check disk IO on the replica and whether it is also serving heavy reads. A replica that cannot catch up at all may need a rebuild from a fresh backup rather than more tuning.
watch -n 10 "mariadb -e 'SHOW REPLICA STATUS\G' | grep -E 'Seconds_Behind_Master|Gtid_IO_Pos'"
# Seconds_Behind_Master: 3412 -> 2290 -> 1105 -> 0
mariadb -e "SELECT @@gtid_slave_pos;"
# should match the primary's @@gtid_binlog_pos when caught up
- Seconds_Behind_Master alone is not enough, so I compare Gtid_IO_Pos with the applied slave position to see whether the IO thread or SQL thread is behind.
- I check the processlist for a single long transaction or workers waiting for prior commits.
- slave_parallel_threads defaults to 0, so I enable it with conservative or optimistic mode after stopping the replica.
- Missing primary keys on row-based tables and giant transactions are the two most common root causes I fix first.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Setting slave_parallel_threads on a running replica The variable can only be changed while the replica threads are stopped, so a live change errors out or is ignored until restart.
- Jumping to optimistic mode without testing the workload Conflict-heavy workloads cause frequent rollbacks and retries, which can make lag worse than conservative mode.
- Ignoring tables without primary keys Row-based replication scans the whole table for each row event, so one bulk update can stall the applier for hours.
- Restarting the replica repeatedly to clear lag Each restart discards worker state and does nothing about the transaction that is actually blocking apply.