Change Data Capture (CDC): how to send database changes to other systems
Change data capture (CDC) detects inserts, updates, and deletes that a database has committed, then delivers them to other systems. In PostgreSQL, one approach is to decode the write-ahead log (WAL) logically and track the reader’s position with a replication slot; Debezium is one implementation that combines this with an initial snapshot and a Kafka Connect event stream. CDC can update a search index or analytics store by sending changes instead of repeatedly querying only current rows. But without a plan for duplicate processing, schema changes, and log retention, downstream data can diverge or the source disk can fill.
This article explains relational database CDC through PostgreSQL 18 logical decoding and the current Debezium PostgreSQL connector documentation, with Kafka as an example destination. Not every CDC product uses PostgreSQL WAL, Debezium, or Kafka; product behavior and limitations below are scoped to the cited documentation.
What problem does CDC solve?
Suppose an order database must also update a search service and an analytics store. If application code writes directly to each store, it must handle partial success: one write may succeed while another fails. Polling the entire orders table on a schedule becomes more expensive as data grows and introduces a delay equal to the polling interval. Simple polling by last-modified time can also miss intermediate updates or deletes unless the system separately preserves change history or deletion markers.
CDC tracks committed row changes and exposes them as events for consumers. A consumer can update a search document, populate a reporting table, or copy selected data to another store. Its main value is that change delivery can continue separately from the application write path. But a CDC event is first a data change, not necessarily a business event such as “the order was approved.” If a row changes several times, the consumer must interpret those changes.
How does a PostgreSQL change become an event?
PostgreSQL WAL records changes at the storage layer. Logical decoding extracts committed table changes from the WAL and converts them into an application-readable form, such as a stream of tuples. A logical replication slot tracks the position in a change stream that a client can read. PostgreSQL 18: Logical Decoding Concepts
A Debezium PostgreSQL connector can form a flow like this:
Application transaction
→ PostgreSQL tables and WAL
→ logical decoding and replication slot
→ Debezium connector / Kafka Connect
→ Kafka change-event topics by table
→ search index, analytics store, or other consumersA new connector cannot rely on old WAL to contain the database’s full history. Debezium therefore takes a consistent initial snapshot, then continues streaming from the exact WAL position associated with that snapshot. This boundary prevents it from missing changes committed while the snapshot was running. Afterward, the connector emits row-level INSERT, UPDATE, and DELETE events. PostgreSQL 10 and later include the pgoutput logical decoding plug-in, so a separate plug-in library is not needed for that option. Debezium: PostgreSQL Connector
An event commonly contains the previous row (before), the new row (after), source metadata such as database and table (source), an operation code (op), and timestamps. The exact fields and format depend on the connector, converter, and configuration. Consumers should use the key and operation to build or update the row’s current representation, or delete it, rather than blindly appending every event as a new record. When Kafka log compaction is used, a delete event can be followed by a tombstone event with the same key and a null value. Debezium: Change and Delete Events
Why must snapshots and streaming be designed together?
If a consumer subscribes only to the change stream, it does not know about rows that existed before the subscription began. If it takes only a snapshot, it misses later inserts, updates, and deletes. Initialization therefore needs a consistent baseline copy and a handoff to changes after the baseline’s position. Debezium’s initial snapshot is followed by streaming from the precise WAL position it captured. If a snapshot is interrupted, it might need to run again, depending on the connector mode; account for the initial read load and restart cost of large tables.
If you later add a table to the capture list or need to reconcile a table, a targeted snapshot can be more appropriate than replaying the entire change stream. Debezium incremental snapshots read tables in primary-key chunks while streaming continues. Its PostgreSQL connector documentation also warns that schema changes during an incremental snapshot are constrained, so check the connector version and selected snapshot mode. Debezium: PostgreSQL Incremental Snapshots
How should duplicates and ordering be handled?
A pipeline restart or fault recovery can cause an event that was already processed to be delivered again. Debezium describes at-least-once delivery: it avoids losing events in fault scenarios, but can emit duplicates while recovering. Do not assume each event arrives exactly once. Design consumers so that processing the same change more than once does not corrupt the final result. Debezium: Fault Handling and Delivery Guarantees
For a search projection, a consumer might use the order ID as a key and overwrite the document with the latest row state. A command-style operation such as “add 10 to the balance” can produce a different result when replayed; track an event ID or source position such as an LSN to detect duplicates. If order matters, verify that the same row key is routed to the same partition and that the consumer processes records in partition order. Decide how to handle reordering across multiple consumers. A table-per-topic stream alone may not provide the global transaction order a system requires.
A consumer can also stop after applying an event to its own store but before saving its progress offset, so it receives the event again after restart. If it advances its offset before applying the data and then stops, it may lose the change. Use an atomic boundary for applying the event and recording progress where available, or design idempotent writes and a replay strategy. Kafka transactions or connector delivery options do not automatically make every external database or search engine update exactly once; identify the actual boundary.
Why monitor replication slots and WAL?
A logical replication slot can retain WAL needed for changes that its consumer has not confirmed. The slot can survive while the connector is stopped, but it does not know the consumer’s state. This lets a connector resume reading after restart, yet a long outage or abandoned slot can increase pg_wal use and the burden of retaining system catalog rows. PostgreSQL exposes slot activity, restart_lsn, confirmed_flush_lsn, WAL status, and invalidation reasons in pg_replication_slots. PostgreSQL 18: pg_replication_slots View
Operational monitoring should cover more than connector health: check retained WAL per slot, free disk space, Kafka Connect offsets and errors, topic lag, and consumer lag. A slot retaining too much WAL can affect the source database. Dropping a slot without understanding the impact can prevent capture from resuming at that position and may require a data gap analysis or a new snapshot. Before removing one, confirm that it is no longer needed and determine which changes would be missed if it were recreated. PostgreSQL 18: Logical Decoding and Replication Slots
When is CDC not a substitute for business events?
Row-level CDC works well for keeping another store synchronized with database state. But an INSERT into a table is not always a business event. Intermediate transaction states, corrective updates, or one business action spread across multiple tables can be hard to infer from row changes alone. PostgreSQL logical decoding does not emit DDL changes, and Debezium documents that its PostgreSQL connector cannot report DDL change events. Coordinate schema changes separately with consumers and use a compatible deployment sequence. Debezium: PostgreSQL Connector Limitations
If a service must publish a business event atomically with a database change, consider the outbox pattern. The application writes both the business row and an outbox event row in the same database transaction. Debezium captures only the outbox table and routes those rows into the topics and format expected by consumers. This reduces the dual-write inconsistency that occurs when application code separately commits to a database and publishes to a broker. It does not remove consumer duplicate handling or schema-compatibility work; retain event IDs and design consumers to be idempotent. Debezium: Outbox Event Router
What should you check before adopting CDC?
**Destination and meaning:** Decide whether you need to copy current state, replay each row change, or publish business events. This affects whether row-level CDC, an initial snapshot, or an outbox is appropriate.
**Table keys and scope:** Specify the schemas and tables to capture and confirm that each has a primary key or stable event key. Review column filters and access controls so sensitive fields do not enter Kafka topics unintentionally.
**Initial load and recovery:** Estimate the read load and duration of the snapshot. Decide how consumers behave if the connector restarts or repeats a snapshot.
**Duplicates, order, and schemas:** Design idempotency for duplicate events, ordering by key, event-schema compatibility, and a separate procedure for DDL releases.
**Retention and observability:** Monitor slot `restart_lsn` and `confirmed_flush_lsn`, WAL use, connector offsets, topic backlog, and consumer lag. After recovery, confirm that the slot and offset represent a compatible position.
**Rebuild and security:** Prepare a procedure to rebuild a topic or destination, grant only the replication permissions the connector needs, and define access and retention rules for personal data in events.
CDC is infrastructure for continuously delivering database changes; it does not automatically provide distributed transactions or domain events. With PostgreSQL WAL-based CDC, plan the initial snapshot boundary, duplicate-tolerant consumers, slot-related WAL retention, and schema coordination together.
Scope and sources
The product examples are based on PostgreSQL 18 logical decoding and the Debezium PostgreSQL connector documentation. Kafka is an example destination; this article does not claim that an installation, performance measurement, or fault test was performed. Before implementing a connector, check the documentation for the exact releases in use because configuration and behavior can change.
[PostgreSQL 18: Logical Decoding Concepts](https://www.postgresql.org/docs/18/logicaldecoding-explanation.html)
[PostgreSQL 18: Logical Decoding Examples](https://www.postgresql.org/docs/18/logicaldecoding-example.html)
[PostgreSQL 18: WAL Runtime Configuration](https://www.postgresql.org/docs/18/runtime-config-wal.html)
[PostgreSQL 18: `pg_replication_slots` View](https://www.postgresql.org/docs/18/view-pg-replication-slots.html)
[Debezium: PostgreSQL Connector](https://debezium.io/documentation/reference/stable/connectors/postgresql.html)
[Debezium: Outbox Event Router](https://debezium.io/documentation/reference/transformations/outbox-event-router.html)
[Debezium: Kafka Connect Topic Creation](https://debezium.io/documentation/reference/stable/configuration/topic-auto-create-config.html)