Greetings. I want to start tracking, and eventually alert on excessive AG latency. I've taken most of the query from this link and also modified it a bit to come up with the query below. However, it's not behaving quite as I'd expected. To test this I shut down the Secondary node in my AG. While the syncState and syncHealth values do change immediately when I rerun the query, no other values do. So my "seconds behind primary" values never gets above 0, and I also expected to see a discrepancy between the last_sent_time and last_recieved_time values, but they're always the same. My end goal is to be able to insert these values into a table and alert on the values, but I first need to make sure that my collection is correct. Should I simply rely on syncState and syncHealth for what I'm after, or is there better info to alert on?
Thanks!
SELECT AGS.NAME AS AGGroupName ,AR.replica_server_name AS InstanceName ,HARS.role_desc ,Db_name(DRS.database_id) AS DBName ,AR.availability_mode_desc AS SyncMode ,DRS.synchronization_state_desc AS SyncState ,DRS.synchronization_health_desc as SyncHealth ,DRS.last_hardened_time ,DRS.last_redone_time ,DRS.log_send_queue_size ,DRS.redo_rate ,DRS.redo_queue_size AS 'Redo_Queue_Size(KB)' ,last_sent_time, last_received_time /* if the last_hardened_lsn from the primary server == last_hardened_lsn from secondary server then there is NO LATENCY */ ,'seconds behind primary' = CASE WHEN EXISTS ( SELECT DRS.last_hardened_lsn FROM ( ( sys.availability_groups AS AGS INNER JOIN sys.availability_replicas AS AR ON AGS.group_id = AR.group_id ) INNER JOIN sys.dm_hadr_availability_replica_states AS HARS ON AR.replica_id = HARS.replica_id ) INNER JOIN sys.dm_hadr_database_replica_states DRS ON AGS.group_id = DRS.group_id AND DRS.replica_id = HARS.replica_id WHERE HARS.role_desc = 'PRIMARY' AND DRS.last_hardened_lsn = DRS.last_hardened_lsn ) THEN 0 ELSE datediff(s, last_hardened_time, getdate()) end FROM sys.dm_hadr_database_replica_states DRS LEFT JOIN sys.availability_replicas AR ON DRS.replica_id = AR.replica_id LEFT JOIN sys.availability_groups AGS ON AR.group_id = AGS.group_id LEFT JOIN sys.dm_hadr_availability_replica_states HARS ON AR.group_id = HARS.group_id AND AR.replica_id = HARS.replica_id ORDER BY Db_name(DRS.database_id)
Thanks in advance! ChrisRDBA