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
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.
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 bytesThe 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.00Each 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.
The first run of this lab showed amounts as "Dzw=": Debezium encodes
Postgres NUMERIC as base64 bytes by default, to keep exact precision.
Setting decimal.handling.mode=string (applied with a PUT to the
connector's /config) makes new events readable, but events already in
the topic keep the old encoding. Choose converters and handling modes
before production data flows. Changing them later means consumers must
handle both.
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
A Postgres replication slot keeps WAL until the consumer confirms
it. If the Debezium connector stops (paused, failed, deleted without
dropping the slot), Postgres retains WAL forever and its disk fills.
Monitor pg_replication_slots (pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn)) and drop slots for connectors you remove. This
is the most common way CDC takes down a production database.
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
- Start the
connectprofile, register the connector, and consumecdc.public.customers. INSERT,UPDATE, andDELETEa 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).- 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". - Pause the connector, make 1,000 changes, and watch the slot's retained WAL grow. Resume it and watch Debezium catch up.