Help Center> Relational Database Service> Troubleshooting> RDS for MySQL> Primary/Standby Replication Issues> Primary/Standby Replication Delay Scenarios and Solutions
Updated on 2023-11-03 GMT+08:00

Primary/Standby Replication Delay Scenarios and Solutions

RDS for MySQL standby instances, read replicas, self-built standby databases, and DR instances established through DRS all use MySQL Primary-Secondary Replication. Primary/standby replication can be implemented in asynchronous or semi-synchronous mode. In either mode, primary/standby replication delay is inevitable for some statements.

Symptom: The replication delay of the standby instance or read replica is too long, or even an alarm is generated for too long replication delay.

Scenario 1: Large Transaction Executed on the Primary Instance

A large transaction is a transaction containing a large quantity of data update operations, for example, a transaction including tens of thousands of DML (INSERT, UPDATE, and DELETE) operations, or an SQL statement that updates tens of thousands of rows of data in batches. The execution of a large transaction usually takes a long time (minutes). After a large transaction is executed on the primary instance, a large number of binlogs are generated. It takes much more time for the standby instance or read replica to obtain these binlogs than for a common transaction. In addition, replaying the transaction also takes much time, at least the same as the time for the transaction being executed on the primary instance. As a result, replication delay occurs on the standby node or read replica.

Troubleshooting:

  • For a large transaction that contains a large number of DML statements, run the following command to find the statement that is executed for a long time:

    select t.*,to_seconds(now())-to_seconds(t.trx_started) idle_time from INFORMATION_SCHEMA.INNODB_TRX t \G;

  • For a large transaction where an SQL statement executes a large amount of data updates, run show full processlist to check whether any DELETE or UPDATE statements are taking a long time to execute.
  • Analyze full logs or slow query logs to check whether there are any large transactions.

Solution:

  • To ensure data consistency between the primary and standby instances, wait until the large transaction is complete.
  • Split a large transaction into small transactions and execute them in batches. For example, you can use the WHERE condition or LIMIT statement to limit the amount of data to be updated each time.

Scenario 2: Table Without a Primary Key Updated

RDS for MySQL binlogs use row-based logging. When data in a row is updated, an event record in row format is generated in the binlog. For example, if an UPDATE statement updates 100 rows of data in a table, 100 rows of update records will be generated in the binlog. During playback on the standby instance or read replica, 100 single-row updates are executed.

When replaying binlog events of the primary instance, the standby instance or read replica searches for rows to be modified based on the primary key or secondary index of the table. If no primary key is created for the table, a large number of full table scans are generated. As a result, the application speed of the binlog is reduced and replication delay occurs.

Troubleshooting:

Use show create table xxx to find out which table has the problematic UPDATE or DELETE statement and check whether the table has a primary key.

Solution:

Add a primary key for the table that does not have a primary key, and a secondary index for the table that does not have a secondary index.

Scenario 3: DDL Operation Performed

A DDL operation typically takes a long time to complete, especially if the table contains a large amount of data. Generally, the time for replaying a DDL operation on the standby instance or read replica is almost the same as that for executing the DDL operation on the primary instance. Therefore, after a DDL operation is performed on a large table in the primary instance, the replication time will increase when the standby instance or read replica replays the operation.

Solution:

Wait for the DDL execution to complete, or perform DDL operations during off-peak hours.

Scenario 4: Read Replica Waiting for a Metadata Lock

When a long transaction is being executed on a table in a read replica, the DDL operation of the table synchronized from the primary instance will be blocked due to a metadata lock, and subsequent binlog playback of the table is also blocked. As a result, the replication delay increases.

Troubleshooting:

  1. Log in to the read replica and run the following command to check whether there are any transactions that are taking a long time to complete:

    select t.*,to_seconds(now())-to_seconds(t.trx_started) idle_time from INFORMATION_SCHEMA.INNODB_TRX t \G;

  2. View the metadata lock view of the read replica to check whether a metadata lock conflict occurs.

    select * from information_schema.metadata_lock_info;

    Find the blocked session based on the thread ID in the metadata lock view. For more information, see MDL Views.

Solution:

Kill the long transaction that blocks DDL operations on the read replica, or commit the long transaction on the application.

Scenario 5: Read Replica Specifications Smaller Than the Primary Instance

If the specifications of the read replica or DR instance established through DRS are smaller than those of the primary instance and the write load of the primary instance increases somewhat, the read replica or DR instance cannot replay binlogs in a timely manner due to insufficient resources. As a result, the replication delay increases.

Solution:

Scale up the specifications of the read replica or DR instance to match the specifications of the primary instance.

Scenario 6: Sudden Increase in Read Requests

In addition to synchronizing data from the primary instance, the read replica also processes read requests. If read requests soar up, the replay threads of the read replica may be affected, leading to an increase in the replication delay.