Lesson 2 of 4 Change Detection and Event-Driven Integration

Change Data Capture

This lesson explains how CDC captures before and after values, how to read changes and how it compares with Change Tracking.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how CDC captures changes from the log.
  • Distinguish between all changes and net changes.
  • Choose between CDC and Change Tracking.

How CDC works

CDC is enabled for the database with sys.sp_cdc_enable_db and for each table with sys.sp_cdc_enable_table, which creates a capture instance and a change table in the cdc schema. A capture process reads the transaction log asynchronously and writes each change as rows with the column values: one row for an insert or delete, and two rows (before and after) for an update. A cleanup job removes changes older than the retention period (three days by default). In Azure SQL Database, a built-in scheduler runs capture and cleanup instead of SQL Server Agent, and CDC is available on vCore tiers and DTU S3 and above.

EXEC sys.sp_cdc_enable_db;
EXEC sys.sp_cdc_enable_table
     @source_schema = N'Catalog', @source_name = N'Product',
     @role_name = NULL, @supports_net_changes = 1;
True or false

How CDC works

Exam skills 2.4.7

Decide whether each statement about change data capture is true.

Log in to try this activity Interactive activities are free with an account.

Reading changes

Changes are read by log sequence number (LSN) range. cdc.fn_cdc_get_all_changes_<capture_instance> returns every change in the range, and cdc.fn_cdc_get_net_changes_<capture_instance> returns one row per changed row with its final state, which requires a primary key or unique index and @supports_net_changes = 1. The __$operation column is 1 (delete), 2 (insert), 3 (value before update) or 4 (value after update).

DECLARE @from binary(10) = sys.fn_cdc_get_min_lsn('Catalog_Product'),
        @to   binary(10) = sys.fn_cdc_get_max_lsn();
SELECT __$operation, ProductID, Name, ListPrice
FROM cdc.fn_cdc_get_all_changes_Catalog_Product(@from, @to, N'all');

CDC compared with Change Tracking

The two features answer different questions, as the following comparison shows.

Change Tracking
RecordsKeys, operation, optional updated-column mask
MechanismSynchronous with the DML statement
HistoryNet fact that a row changed
Overhead and storageLow
Choose when Synchronization and 'what changed since'
Change data capture
RecordsFull column values before and after each change
MechanismAsynchronous log reader
HistoryEvery intermediate change
Overhead and storageHigher: change tables hold copies of data
Choose when Auditing, ETL and data warehouse loads

In serverless Azure SQL Database, CDC prevents auto-pause, which matters for the free offer, and change event streaming cannot be enabled on a database that uses CDC.

The following diagram shows what each feature records for the same updates.

Change Tracking compared with change data capture for a row whose price changes three times in a day. Change Tracking records synchronously that the row changed, with its key, the operation and an optional column mask, so a sync job learns only the net fact and reads the current values itself. Change data capture's asynchronous log reader writes every intermediate change with full before and after values to change tables, which suits auditing and warehouse loads but costs more storage.
Prices and times are an example.
Sort into groups

Change Tracking or CDC?

Exam skills 2.4.7

Choose the feature that fits each requirement.

Log in to try this activity Interactive activities are free with an account.
Worked example

Loading a warehouse incrementally

A nightly ETL must load every price change of the day into a data warehouse, including the old and new price, even when a price changed several times. Which feature should the source database use?

Use change data capture on the Product table and read the day's changes with fn_cdc_get_all_changes, which returns every update with before and after values. Change Tracking reports only that a row changed and its current state, so intermediate prices and old values would be lost, and a temporal table could provide history but is not designed as an incremental change feed.

Check yourself

What do __$operation values 3 and 4 mean in a CDC change table?

Show answer

3 is the row before an update and 4 is the row after the update.

Exam Tips

Remember for the exam

  • Old and new values of every change: CDC.
  • Every change: fn_cdc_get_all_changes; final state per row: fn_cdc_get_net_changes.
  • CDC reads the log asynchronously; default retention is three days.
  • CDC prevents serverless auto-pause.