The following is the second part of SQL interview questions series, focusing mostly on the replication monitoring, most common SQL Server Replication errors and different replication scenarios.
SQL Interview Questions 1: How to monitor latency in replication?There are three methods:
Replication monitor Replication commands Tracer Tokens Replication MonitorIn replication monitor from the list of all subscriptions, double-click on the desired subscription. There we find three tabs:
Publisher to Distributor History Distributor to Subscriber History Undistributed commands Replication Commands Publisher.SP_ReplTran Checks the pending transactions at p Distributor.MSReplCommands and MSReplTransactions Gives the transactions and commands details. Actual T_SQL data is in binary format. From the entry time, we can estimate the latency. Distributor.SP_BrowseReplCmds It shows the eaxct_seqno along with the corresponding T-SQL command sp_replmonitorsubscriptionpendingcmds It shows the total number of pending commands to be applied at Subscriber along with the estimated time. Tracer TokensTracer tokens are nothing but transactions which are written into Publisher’s transaction log. It is read by Log reader agent and put into the Distribution database, then it is written to the Subscriber. All these records can be seen on the replication monitor.
Below is the T-SQL code to use Tracer tokens to troubleshoot the latency issues.
--A SQL Agent JOB to insert a new Tracer Token in the publication database. USE [AdventureWorks] Go EXEC sys.sp_posttracertoken @publication = <PublicationName> Go --Token Tracking Tables USE Distribution Go --publisher_commit SELECT Top 20 * FROM MStracer_tokens Order by tracer_id desc --subscriber_commit SELECT Top 20 * FROM MStracer_history Order by parent_tracer_id SQL Interview Questions 2: What are the various ways to monitor replication? Replication Monitor T-SQL Commands & system CATALOGS Tracer Tokens Performance Monitor Extended Events Verbose Logging SQL Interview Questions 3: Which is the best way to monitor replication? Have you ever implemented automations in replication monitoring? Replication Monitor: One of the best ways to monitor but not all the time. It showcases the current situation to answer the question “How are things right now?” but it doesn’t baseline. Also, replication monitor internally runs the commands to get the required details. On a busy topology it might be a performance intensive T-SQL Commands & system CATALOGS: We absolutely suggest this as it directly gets only the required things. It allows us to baseline and customizing the monitoring Tracer Tokens: We don’t suggest this. We have seen some scenarios where it took a lot of time to respond Performance Monitor: This is also a resource intensive method, but sometimes it might be helpful. Even for the performance counters we would suggest using T-SQL scripts with the required DMV Extended Events: We never used this method. Moreover, in SQL Server 2012 Books Online Microsoft mentioned that this method is for customer support engineers (Microsoft FTE) to collect the information for troubleshooting Verbose Logging: This method may not be useful for monitoring replication but it’s helpful at the time of troubleshooting. In this method, we just enable Agent History profile for the detailed logging and it helps us understanding the detailed progress and error message. Make sure we are using this only for the troubleshooting as it’s also a performance intensiveWe suggest using a customized solution
Create a list of tables to capture the replication agents, latency and pending transactions/commands details using T-SQL code Define baselines for parameters Create scripts to capture the replication health details and store it on predesigned Tables Create scripts to compare the captured values with the baseline values and to send a health check report to the DBA team if there are any issues or delays SQL Interview Questions 4: What are the things that we need to monitor periodically to know the replication health?There are basically three things that helps us to understand the health of replication:
Replication Agent status Latency Publisher-Distributor-Subscriber Outstanding commands to be subscribed SQL Server Interview Questions 5: In transactional replication setup, Distribution agent failed with the error message “Cannot insert duplicate key row in object ‘XXXX’ with unique index ‘PK_XXXXX’. The statement has been terminated.” Have you ever seen this kind of error? If yes, can you explain how to resolve?We may get this error when a row on the Subscriber already exists on the Subscriber database. We can fix this error by skipping failed transactions or deleting already existing row at Subscriber.
SQL Server Interview Questions 6: In transactional replication setup Distribution agent failed with the error message “The row was not found at the Subscriber when applying the replicated command.” Have you ever encountered this kind of error? If yes, can you explain how to resolve?This error occurs when row on which we are requesting transaction does not exist at Subscriber. We can skip failed error to move forward.
SQL Interview Questions 7: How to skip Distributed Agent errors in transactional replication?By default, when the Distribution Agent encounters an error, the agent stops. If you use the -SkipErrors parameter and specify expected errors or errors that you do not want to interfere with the replication, the agent will log the error information and then continue running.
For example, if you want to specify that the Distribution Agent should log duplicate key violations, but continue to process subsequent transactions, specify that the agent should skip errors 2601 (Cannot insert duplicate key row in object ‘%.*ls’ with unique index ‘%.*ls’.) and 2627 (Violation of %ls constraint ‘%.*ls’. Cannot insert duplicate key in object ‘%.*ls’.):
-SkipErrors 2601:2627
The most common way to use the
-SkipErrors
parameter is to use the Distribution Agent profile titled Continue on Data Consistency Errors. The Distribution Agent will then skip errors 2601, 2627, and 20598 (The row was not found at the Subscriber when applying the replicated command).
SQL Interview Questions 8: What are counters to monitor for transactional replication?Object: SQL Server: Replication Log Reader Counter:
Log Reader: Delivered Cmds/Sec and Tx/Sec
These two counters are repeated for each publication; they display the commands or transactions read per second and indicate how many commands or transactions are read by the Log Reader per second. If this counter increases, it indicates that the Tx rate at the Publisher has increased.
Object: SQL Server: Replication Dist. Counter:
Dist: Deli