Change Data Capture (CDC)

Change Data Capture (CDC) is a technique for observing and capturing changes made to a database and delivering them as real-time events to downstream systems. Unlike polling-based methods, modern CDC is log-based, directly reading the database's internal transaction logs (e.g., PostgreSQL WAL, MySQL Binlog).

Why Log-Based CDC?

  1. Low Latency: Changes are captured near-instantly after a commit.
  2. Zero Impact on Schema: No need for last_modified columns or triggers that slow down production writes.
  3. Capture Deletes: Polling cannot detect hard deletes; log-based CDC captures the DELETE event from the transaction log.
  4. Consistency: Captures every state change, ensuring no intermediate updates are missed (critical for financial audit trails).

The Debezium Architecture

Debezium is the industry-standard open-source platform for CDC. It typically runs as a set of connectors within Kafka Connect.

Concrete Example: Debezium PostgreSQL Connector Config

To capture changes from a PostgreSQL database, you must set wal_level = logical in postgresql.conf and provide a connector configuration.

JSON Configuration for Kafka Connect:

{
  "name": "inventory-connector",
  "config": {
    "connector.class": "io.debezium.connector.postgresql.PostgresConnector",
    "tasks.max": "1",
    "database.hostname": "postgres-db",
    "database.port": "5432",
    "database.user": "debezium",
    "database.password": "dbz",
    "database.dbname": "inventory",
    "database.server.name": "dbserver1",
    "table.include.list": "public.orders,public.customers",
    "plugin.name": "pgoutput",
    "publication.autocreate.mode": "filtered",
    "slot.name": "debezium_fulfillment_slot",
    "snapshot.mode": "initial"
  }
}

The Payload Structure

A Debezium event contains a before and after block.

{
  "op": "u",
  "before": { "id": 1, "status": "PENDING" },
  "after":  { "id": 1, "status": "SHIPPED" },
  "source": { "ts_ms": 1716200000000, "snapshot": "false" }
}

Advanced Patterns

Summary of Technical implementation added