CDC with Debezium

Stream row changes from PostgreSQL and Oracle with Debezium, and publish domain events through the outbox pattern.

Updated CompTech Team

Polling a table misses the update that happened between polls, and it loads the database for no reason. Change data capture reads the database log. Debezium is the connector that turns PostgreSQL WAL records, or Oracle redo, into Kafka records. Each change becomes a record other systems can consume at their own pace.

Reading every column of every table is rarely what you want to publish. This lab uses the outbox pattern: the application writes the business event in the same transaction as its own tables, and Debezium publishes that event only.

Prerequisites

  • The Connect cluster from Kafka Connect, with room for a second plugin
  • PostgreSQL with wal_level=logical. On Crunchy Postgres that is a parameter on the PostgresCluster
  • A database user that can create a publication and a replication slot
  • For the Oracle section, a database with supplemental logging and LogMiner available to the connector user

The outbox

The service inserts the payment and the event together. If the transaction rolls back, nothing is published. If it commits, the row is in the log and Debezium will see it. There is no second write to Kafka that can fail on its own.

CREATE TABLE outbox (
  id            uuid PRIMARY KEY,
  aggregateid   text NOT NULL,
  aggregatetype text NOT NULL,
  type          text NOT NULL,
  payload       jsonb NOT NULL,
  created_at    timestamptz NOT NULL DEFAULT now()
);
INSERT INTO posted_payment (payment_id, account, amount) VALUES ('p1', 'a1', 100);
INSERT INTO outbox (id, aggregateid, aggregatetype, type, payload)
VALUES (gen_random_uuid(), 'a1', 'payment', 'PaymentCreated',
        '{"id":"p1","account":"a1","amount":100}');

Add the Debezium PostgreSQL plugin to the Connect build, next to the JDBC sink, and roll the KafkaConnect so the image contains io.debezium.connector.postgresql.PostgresConnector.

apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaConnector
metadata:
  name: payments-outbox
  namespace: kafka
  labels:
    strimzi.io/cluster: connect
spec:
  class: io.debezium.connector.postgresql.PostgresConnector
  tasksMax: 1
  config:
    database.hostname: postgres.payments.svc
    database.port: "5432"
    database.user: debezium
    database.password: debezium
    database.dbname: payments
    topic.prefix: payments
    plugin.name: pgoutput
    publication.autocreate.mode: filtered
    slot.name: payments_outbox
    table.include.list: public.outbox
    transforms: outbox
    transforms.outbox.type: io.debezium.transforms.outbox.EventRouter
    transforms.outbox.route.by.field: aggregatetype
    transforms.outbox.route.topic.replacement: payments.${routedByValue}
    transforms.outbox.table.field.event.key: aggregateid
    transforms.outbox.table.field.event.payload: payload
    transforms.outbox.table.fields.additional.placement: "type:header:eventType"

plugin.name: pgoutput uses the logical decoding built into PostgreSQL. tasksMax stays 1. A Postgres connector is a single task because the slot is a single ordered stream. The EventRouter sends aggregatetype=payment to the topic payments.payment, with the account as the key and the JSON payload as the value. The header eventType is PaymentCreated, so one topic can carry more than one event type without a second table.

Move database.password to a Secret before this leaves the lab, the same way as the sink connector.

Commit a row and read the topic:

oc exec -n kafka lab-broker-0 -- bin/kafka-console-consumer.sh \
  --bootstrap-server lab-kafka-bootstrap:9092 \
  --topic payments.payment --from-beginning --max-messages 1

A consumer that inserts into its own database still needs the idempotent write from delivery guarantees. Debezium delivers at least once. The outbox gives you one event per committed transaction, not exactly-once across the network.

Delete outbox rows from the application, or let them stay and exclude them from backups you do not need. Debezium does not require the row to remain after the WAL entry is read. It does require the replication slot to exist. A slot that is not read fills the disk on the database server. Alert on slot lag. That alert belongs with the others in monitoring.

Oracle

The Oracle connector class is io.debezium.connector.oracle.OracleConnector. It reads redo through LogMiner. The database user needs LogMiner rights, and the tables you capture need supplemental logging so the redo contains the columns, not only the ones that changed.

ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;
ALTER TABLE outbox ADD SUPPLEMENTAL LOG DATA (ALL) COLUMNS;

The connector config uses database.url in the thin-driver form, log.mining.strategy, table.include.list and the same transforms.outbox block as Postgres. One task, one connector per database. The outbox table has the same columns, so the EventRouter config does not change between the two engines. What changes is the log the connector is allowed to read, and the licence and size of the Oracle host. Test the mining user against a standby before you point it at production redo. Mining on the primary is extra load. Mining on a standby is extra lag. Measure which one you have.

Do not point Debezium at every application table and call the result an API. Those records are a physical copy of the schema, and they break when someone adds a column. The outbox is a contract you version in schema registry.

What's next

  1. Shape the payload with a schema in schema registry before a second consumer decodes it.
  2. React to the event as it arrives in stream processing.
  3. Re-embed a changed document from this stream in Kafka for AI.