How to Monitor MySQL Replication Lag

Monitoring replication lag between a MySQL source and its replica is crucial for ensuring data consistency, high availability, and timely backups. This article provides a straightforward guide on how database administrators can measure and monitor this lag using built-in MySQL commands, the Performance Schema, heartbeat mechanisms, and external monitoring tools.

1. Using the SHOW REPLICA STATUS Command

The most common and immediate way to check replication lag is by querying the replica directly.

For MySQL 8.0.22 and newer, log into the replica database and run:

SHOW REPLICA STATUS\G

(Note: For older MySQL versions, use SHOW SLAVE STATUS\G).

Look for the following key fields in the output:

Limitations of Seconds_Behind_Source

While convenient, Seconds_Behind_Source can be misleading. It calculates lag by comparing the timestamp of the last transaction executed on the replica with the timestamp when that transaction was recorded on the source. If the network is slow or replication is completely blocked, the metric may temporarily report 0 even if the replica is not up to date.


2. Precision Monitoring with Percona’s pt-heartbeat

To overcome the limitations of Seconds_Behind_Source, administrators often use pt-heartbeat, a tool from the open-source Percona Toolkit.

This tool works by constantly updating a timestamp on a dedicated table on the source database. The replica replicates this table, and pt-heartbeat compares the replicated timestamp with the replica’s system clock.


3. Querying the Performance Schema

For modern MySQL deployments, the Performance Schema provides detailed replication tables that offer more granular insights than SHOW REPLICA STATUS.

To check the replication status and delay, you can query the replication_connection_status and replication_applier_status_by_worker tables. For example:

SELECT 
    channel_name, 
    last_queued_transaction, 
    last_queued_transaction_original_commit_timestamp,
    last_queued_transaction_immediate_commit_timestamp
FROM performance_schema.replication_connection_status;

By comparing the commit timestamps in these tables to the current system time, you can programmatically calculate highly accurate lag metrics.


4. Setting Up Automated Alerts and Dashboards

Manual checks are insufficient for production environments. Database administrators should integrate MySQL replication metrics into centralized monitoring systems: