Skip to contentManaged Kafka is live: Apache Kafka on AWS for half the cost of MSK

Microsoft SQL Server

Microsoft SQL Server source: high availability and failover

How Artie replicates SQL Server Always On availability groups: listeners, failover, CDC jobs, log backups and delivery guarantees.

Prerequisites

  • Artie connects through the Always On availability group (AG) listener, never an individual replica.
  • The artie login exists on every replica, with the same SID.
  • CDC or change tracking retention is longer than your longest expected outage.
  • Replicas that can become primary use synchronous commit, or you plan to backfill after a forced failover.
  • For capture instances: the CDC capture and cleanup jobs exist on every replica that can become primary.
  • For transaction log backups: every replica writes log backups to one folder with one naming prefix.

Connect through the listener

In the source connection form, enter the AG listener’s DNS name as the Host.

To read from a readable secondary, turn on Read-only application intent. Artie then connects with ApplicationIntent=ReadOnly, and the listener routes the connection to a readable secondary through read-only routing. Capture instances work on a readable secondary, because SQL Server lets the CDC functions read change tables there. SQL Server serves change tracking data only from the primary replica, so leave this setting off for change tracking.

To run backfills on a different host, turn on Use a read replica for backfill? and enter the Replica host and Replica port. Artie uses that host for backfills only. Replication keeps reading from Host.

What carries over on failover

CDC and change tracking are database settings. Their data lives in the database, so the AG copies it to every replica.

WhatCopied to the new primaryNeeds action
CDC enabled on the database and tablesYesNone. You don’t need to re-enable CDC after a failover.
Capture instances and change tablesYesNone
LSNs, and the position Artie savedYes. The new primary continues the same log.None
Change tracking settings and versionsYesNone
CDC capture and cleanup jobs (SQL Server Agent)No. Agent jobs are stored in msdb, which isn’t in the AG unless you use a contained availability group (SQL Server 2022 and later).Create them on the new primary. See Set up CDC jobs on every replica.
LoginsNoCreate the artie login on every replica with the same SID.

What Artie does during a failover

  1. The AG fails over, and the connection to the old primary drops.
  2. The pipeline’s current query fails and the reader process exits. If the error isn’t a dropped connection, Artie sends the replication.error webhook. A dropped connection (a TCP reset) restarts the reader without one.
  3. The reader restarts and reconnects through the listener, which now points to the new primary.
  4. Artie resumes from the last position it saved. Changes after that position are read from the new primary.

Artie saves its position only after the changes it read are written to its Kafka buffer. Changes Artie read but hadn’t saved a position for are read again. The destination merges on the primary key, so the result is the same.

Synchronous and asynchronous replicas

A failover to a synchronized synchronous-commit replica doesn’t lose committed transactions, so Artie replicates every committed change.

A forced failover to an asynchronous-commit replica can lose transactions that the old primary committed but hadn’t sent yet. Those transactions don’t exist on the new primary, so Artie can’t replicate them. If Artie had already read them, they stay in the destination.

After a forced failover with possible data loss, trigger a backfill of the replicated tables. The backfill brings the destination back in line with the new primary.

Set up CDC jobs on every replica

SQL Server creates the CDC capture and cleanup jobs on the replica where you enabled CDC on the database’s first table with sys.sp_cdc_enable_table. After a failover, the new primary has no capture job unless you create one.

Without a capture job, sys.fn_cdc_get_max_lsn() stops moving. Artie sees no new changes and the pipeline keeps running without errors. No changes are lost while the job is missing, because SQL Server keeps them in the log. They’re replicated once the job runs.

Microsoft’s guidance is to create the jobs on the new primary after a failover. The first time a replica becomes primary, run this on it:

SQL
USE [ANALYTICS];
EXEC sys.sp_cdc_add_job @job_type = N'capture';
EXEC sys.sp_cdc_add_job @job_type = N'cleanup', @retention = 10080; -- minutes, 7 days
GO

Replace ANALYTICS with your database name. To confirm both jobs exist, run EXEC sys.sp_cdc_help_jobs; in the same database.

When the old primary becomes a secondary, disable its CDC jobs with msdb.dbo.sp_update_job and @enabled = 0. If it becomes primary again, turn them back on with @enabled = 1.

For how the jobs behave on secondary replicas, see Microsoft’s guide to CDC with availability groups.

Log backups in an availability group

With transaction log backups, Artie reads files in name order from one folder. It skips any file whose name sorts before the last file it read.

The log is the same on every replica, so Artie can read backups taken on any replica, as long as every replica:

  • writes to the same folder, without per-server subfolders
  • uses the same file name prefix, for example the AG name instead of the server name

With Ola Hallengren’s maintenance solution, set @AvailabilityGroupDirectoryStructure = NULL and keep the default @AvailabilityGroupFileName, which starts with the cluster and AG name.

Set the AG’s backup preference so that one replica takes log backups at a time.

Delivery guarantees

  • At least once, for all three replication methods. Artie saves its position only after the changes it read are written to its Kafka buffer. A restart, crash or failover replays from the saved position, so changes can arrive twice but aren’t skipped. The destination merges on the primary key. Tables in history mode append rows instead, so a replayed change can appear twice.
  • Committed changes only. Capture instances and transaction log backups publish committed transactions in commit order. Transaction log backups drop rolled-back transactions.
  • Retention. If the pipeline is stopped for longer than the CDC or change tracking retention period, SQL Server deletes changes Artie hadn’t read. Artie continues from the oldest change still available and logs a warning. Backfill the affected tables.

Was this page helpful?