Learn Labs
Connect & Stream Processing

Kafka Connect & CDC

Moving data in and out without code, and streaming Postgres changes with Debezium

Most data in Kafka comes from somewhere else, and most of it goes somewhere else. Kafka Connect is a framework for those integrations: you configure a connector in JSON, and Connect's workers run it, track its progress, restart it on failure, and scale it out. No producer or consumer code. The book's Building Data Pipelines chapter covers the design.

Concepts

workera Connect JVM process; several form a Connect cluster (group.id)connectora configured integration: "Postgres -> Kafka" (source) or "Kafka -> S3" (sink)taska unit of parallel work the connector splits into (tasks.max)converterbytes <-> Connect's data model: JSON, Avro (with Schema Registry), ProtobufSMTsingle message transform: rename fields, route topics, mask values, in-flight

Connect stores its own state in three compacted topics (_connect_configs, _connect_offsets, _connect_status in this stack), so workers are stateless and a crashed worker's tasks move to another.

Change data capture with Debezium

Change data capture (CDC) turns every insert, update, and delete in a database into an event. Debezium does it by reading the database's replication log (for Postgres, logical decoding of the WAL, the same mechanism replicas use), so it captures every change, in commit order, without polling or triggers.

WALprogresscdc-postgreswal_level=logicalreplication slotpgoutputDebeziumConnect workercdc.public.orderscdc.public.customers_connect_* topicsoffsets, config, status

The connect profile starts a dedicated Postgres (with wal_level=logical, and customers and orders tables seeded from config/cdc-init.sql) and a Debezium Connect worker:

docker compose -f compose_prod.yaml --profile connect up -d
curl localhost:8083/
{"version":"4.3.1","commit":"26b251a451ce941d","kafka_cluster_id":"4L6g3nShT-eMCtK--X86sw"}

curl -X POST -H "Content-Type: application/json" \
--data @config/connectors/postgres-cdc.json localhost:8083/connectors

curl localhost:8083/connectors/postgres-cdc/status
{"name":"postgres-cdc","connector":{"state":"RUNNING",...},"tasks":[{"id":0,"state":"RUNNING",...}]}
"connector.class": "io.debezium.connector.postgresql.PostgresConnector",
"database.hostname": "cdc-postgres",
"topic.prefix": "cdc",                          # topics: cdc.<schema>.<table>
"plugin.name": "pgoutput",                      # Postgres's built-in logical decoding plugin
"slot.name": "debezium",                        # the replication slot Debezium reads from
"table.include.list": "public.customers,public.orders",
"decimal.handling.mode": "string",              # NUMERIC as "89.00", not base64 bytes

The events

Change a row, and read the topic:

docker exec cdc-postgres psql -U admin -d shop -c "UPDATE orders SET status='refunded' WHERE id=1"

kafka-console-consumer.sh ... --topic cdc.public.orders --from-beginning
op=r  before=None     after=pending    # snapshot rows ("read") from the initial load
op=u  before=pending  after=shipped    # an UPDATE: full before AND after row
op=u  before=paid     after=refunded   amount=89.00

Each event has op (c create, u update, d delete, r snapshot read), before and after row images, and a source block with the transaction id, LSN, and timestamp. On first start Debezium snapshots the existing rows, then switches to streaming.

REPLICA IDENTITY FULL (set in cdc-init.sql) is what makes before contain the whole old row. With the default, before holds only the primary key.

Operating Connect

GET /connectorslistGET /connectors/<name>/statusconnector and task state, with stack traces on failurePUT /connectors/<name>/configcreate or updatePOST /connectors/<name>/restart?includeTasks=truePUT /connectors/<name>/pausestop consuming/producing, keep offsetsDELETE /connectors/<name>

Sinks

The other direction: sink connectors read topics and write elsewhere: JDBC (to Postgres), S3, Elasticsearch, ClickHouse, Redis. A sink commits consumer offsets after writing, so it's at-least-once unless the connector supports idempotent writes (upserts by key), which most good sinks do.

Lab

  1. Start the connect profile, register the connector, and consume cdc.public.customers.
  2. INSERT, UPDATE, and DELETE a customer. Find all three events, and the tombstone that follows the delete (a null-value record with the same key, so compaction can remove the row).
  3. Check the slot in Postgres: docker exec cdc-postgres psql -U admin -d shop -c "SELECT slot_name, active, confirmed_flush_lsn FROM pg_replication_slots".
  4. Pause the connector, make 1,000 changes, and watch the slot's retained WAL grow. Resume it and watch Debezium catch up.

On this page