Change data capture in SQL Server, explained

What change data capture records, how it differs from a watermark and from change tracking, and how to read the inserts, updates and deletes it keeps.

Short answer: change data capture (CDC) reads the transaction log of a SQL Server or Azure SQL database and writes every insert, update and delete on the tables you choose into change tables. A load then asks for the changes between two log positions and gets each row with what happened to it, including deletes, which a watermark on a modified date never sees.

A watermark works until someone deletes a row or updates a table without touching its date column. CDC does not depend on the table's columns; it reads what the database itself recorded.

How does it compare?

WayFinds deletesKeeps the valuesNeeds a column in the table
Watermark on a datenocurrent row onlyyes, a reliable modified date
Change trackingyeskeys only, read the row yourselfno
Change data captureyesevery change, before and afterno
Three ways to find what changed

How do you turn it on and read it?

EXEC sys.sp_cdc_enable_db;

EXEC sys.sp_cdc_enable_table
    @source_schema = N'dbo',
    @source_name   = N'Orders',
    @role_name     = NULL;

DECLARE @from binary(10) = sys.fn_cdc_get_min_lsn('dbo_Orders'),
        @to   binary(10) = sys.fn_cdc_get_max_lsn();

SELECT __$operation, OrderID, Status, Amount
FROM cdc.fn_cdc_get_all_changes_dbo_Orders(@from, @to, N'all');
__$operationChange
1delete
2insert
3update, the row before
4update, the row after
What __$operation means

With N'all', an update comes back once, as operation 4; N'all update old' returns the before row as well.

How does a load use it?

  1. 1
    Last positionthe log position the last run reached
  2. 2
    Read changesfrom that position to the newest
  3. 3
    Applydelete, insert or update by key
  4. 4
    Save positiononly when the apply succeeded
An incremental load with CDC

It is the watermark pattern again, with a log position instead of a date. Save the position only after the target has the changes, and a failed run picks them up next time.

What goes wrong?

  • Changes expire. A cleanup job removes change rows after a retention period (three days by default). A load that stops for longer misses changes and needs a full reload.
  • The table's columns change. A new column is not captured until a new capture instance is made for the table.
  • SQL Server Agent is off. On SQL Server, capture and cleanup run as Agent jobs. Azure SQL Database runs them with its own scheduler.

Where can you check this?

Where do you start?

Load only the rows that changed, with a watermark is the same load with a date, and says when that is enough. The open course Upserts into Azure SQL builds the merge by key the changes are applied with.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.