MySQL :: MySQL 8.4 Reference Manual :: 19.4.11 Delayed Replication (original) (raw)

19.4.11 Delayed Replication

MySQL supports delayed replication such that a replica server deliberately executes transactions later than the source by at least a specified amount of time. This section describes how to configure a replication delay on a replica, and how to monitor replication delay.

In MySQL 8.4, the method of delaying replication depends on two timestamps,immediate_commit_timestamp andoriginal_commit_timestamp (seeReplication Delay Timestamps); delayed replication is measured using these timestamps. If either the immediate source or replica is not using these timestamps, the implementation of delayed replication from MySQL 5.7 is used (seeDelayed Replication). This section describes delayed replication between servers which are all using these timestamps.

The default replication delay is 0 seconds. Use aCHANGE REPLICATION SOURCE TO SOURCE_DELAY=N statement to set the delay to N seconds. A transaction received from the source is not executed until at least N seconds later than its commit on the immediate source. The delay happens per transaction (not event as in previous MySQL versions) and the actual delay is imposed only on gtid_log_event oranonymous_gtid_log_event. The other events in the transaction always follow these events without any waiting time imposed on them.

The replication_applier_configuration Performance Schema table contains theDESIRED_DELAY column which shows the delay configured using the SOURCE_DELAY option. Thereplication_applier_status Performance Schema table contains theREMAINING_DELAY column which shows the number of delay seconds remaining.

Delayed replication can be used for several purposes:

Replication Delay Timestamps

MySQL 8.4 provides a new method for measuring delay (also referred to as replication lag) in replication topologies that depends on the following timestamps associated with the GTID of each transaction (instead of each event) written to the binary log.

The output of mysqlbinlog displays these timestamps in two formats, microseconds from epoch and alsoTIMESTAMP format, which is based on the user defined time zone for better readability. For example:

#170404 10:48:05 server id 1  end_log_pos 233 CRC32 0x016ce647     GTID    last_committed=0
\ sequence_number=1    original_committed_timestamp=1491299285661130    immediate_commit_timestamp=1491299285843771
# original_commit_timestamp=1491299285661130 (2017-04-04 10:48:05.661130 WEST)
# immediate_commit_timestamp=1491299285843771 (2017-04-04 10:48:05.843771 WEST)
 /*!80001 SET @@SESSION.original_commit_timestamp=1491299285661130*//*!*/;
   SET @@SESSION.GTID_NEXT= 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1'/*!*/;
# at 233

As a rule, the original_commit_timestamp is always the same on all replicas where the transaction is applied. In source-replica replication, theoriginal_commit_timestamp of a transaction in the (original) source’s binary log is always the same as itsimmediate_commit_timestamp. In the replica’s relay log, theoriginal_commit_timestamp andimmediate_commit_timestamp of the transaction are the same as in the source’s binary log; whereas in its own binary log, the transaction’simmediate_commit_timestamp corresponds to when the replica committed the transaction.

In a Group Replication setup, when the original source is a member of a group, theoriginal_commit_timestamp is generated when the transaction is ready to be committed. In other words, when it finished executing on the original source and its write set is ready to be sent to all members of the group for certification. When the original source is a server outside the group, the original_commit_timestamp is preserved. The same original_commit_timestamp for a particular transaction is replicated to all servers in the group, and to any replica outside the group that is replicating from a member. Each recipient of the transaction also stores the local commit time in its binary log usingimmediate_commit_timestamp.

View change events, which are exclusive to Group Replication, are a special case. Transactions containing these events are generated by each group member but share the same GTID (so, they are not first executed in a source and then replicated to the group, but all members of the group execute and apply the same transaction). Group members set local timestamp values for transactions associated with view change events.

Monitoring Replication Delay

One of the most common ways to monitor replication delay (lag) in previous MySQL versions was by relying on theSeconds_Behind_Master field in the output ofSHOW REPLICA STATUS. However, this metric is not suitable when using replication topologies more complex than the traditional source-replica setup, such as Group Replication. The addition ofimmediate_commit_timestamp andoriginal_commit_timestamp to MySQL 8 provides a much finer degree of information about replication delay. The recommended method to monitor replication delay in a topology that supports these timestamps is using the following Performance Schema tables.

Using these tables you can monitor information about the last transaction the corresponding thread processed and the transaction that thread is currently processing. This information comprises:

In addition to the Performance Schema tables, the output ofSHOW REPLICA STATUS has three fields that show:

When the replication SQL thread is waiting for the delay to elapse before executing an event, SHOW PROCESSLIST displays its State value as Waiting until SOURCE_DELAY seconds after master executed event.