Skip to main content

CDC Sink for SQL Server: Overview

Connection string

Use a SqlConnectionString with FactoryName set to "Microsoft.Data.SqlClient":

store.Maintenance.Send(new PutConnectionStringOperation<SqlConnectionString>(
new SqlConnectionString
{
Name = "SqlServerConnection",
FactoryName = "Microsoft.Data.SqlClient",
ConnectionString =
"Server=myserver;Database=mydb;" +
"User Id=ravendb_cdc;Password=...;" +
"TrustServerCertificate=True;"
}));

Microsoft.Data.SqlClient defaults to Encrypt=True.
Unless SQL Server presents a certificate trusted by the RavenDB server, add TrustServerCertificate=True
(as above) or Encrypt=False to the connection string. Otherwise, the CDC Sink task cannot connect.

Prerequisites

Before CDC Sink can capture changes,
SQL Server CDC must be enabled on the source database and on every source table configured in the task.

If the SQL Server user in the CDC Sink connection string has permission to enable CDC,
RavenDB enables it automatically when the task starts. Otherwise, ask a database administrator to run:

-- Enable CDC on the database
EXEC sys.sp_cdc_enable_db;

-- Enable CDC on a table
EXEC sys.sp_cdc_enable_table
@source_schema = N'dbo',
@source_name = N'orders',
@role_name = NULL;

The SQL Server Agent service must be running for CDC change capture to work.
The Agent runs the capture jobs that populate the change tables.

On-premises, start the SQL Server Agent service; in Docker containers, start with -e 'MSSQL_AGENT_ENABLED=true'.
If the Agent is stopped, the task can still start and raise a warning, but no CDC changes are delivered until the Agent is running.

Azure SQL Database

Azure SQL Database has no classic SQL Server Agent.
Its CDC capture runs on a managed scheduler, so this requirement does not apply there.

Polling behavior

Unlike PostgreSQL and MySQL, SQL Server CDC Sink uses polling rather than a streaming replication connection.
The CdcSink.SqlServer.PollIntervalInSec server configuration key controls how frequently the task polls for new change rows.

Lower values reduce latency, but increase load on the source database.
See Server Configuration.

Task configuration

SQL Server CDC Sink uses the same CdcSinkConfiguration table, mapping, patching, and delete-handling settings as the other source engines. SQL Server does not use the PostgreSQL-specific Postgres settings.

See Configuration Reference for full details.

When SourceTableSchema is omitted, SQL Server uses "dbo" as the default schema.

In this article