Course contents 20 / 35

20 / 35 · Delivering Changes

Change data capture

Align a source snapshot with a change-log position, then trace updates, deletions and replay after a restart.

The initial export reads order 101 at 700 cents. The checkout raises its amount to 900 while the export is still copying other orders. If we start watching changes only after the export finishes, the destination can keep 700 forever. The copy needs a matching position from which later changes are available.

Couple the snapshot and the log#

Change data capture (CDC) exposes database changes as records a consumer can apply. With log-based CDC, the reader interprets a database log rather than comparing successive table extracts. PostgreSQL logical decoding translates WAL information into an ordered stream of logical changes for a client. MySQL uses its binary log for a related purpose. Its statement-based and row-based formats record different kinds of information. CDC connectors must understand the source’s representation. PostgreSQL 18, §47.2 MySQL 8.4, §7.4.4

An initial snapshot establishes the starting contents. A source protocol couples the snapshot with the log position from which the consumer should resume. With the default initial snapshot workflow, Debezium’s PostgreSQL connector records a position and reads the relevant rows in a consistent snapshot. It then streams changes from the corresponding point. The protocol closes the gap in the opening example. Taking a snapshot and independently reading the current WAL end afterward would give no such guarantee. Debezium, Snapshots

Use three illustrative positions in one source history. At the snapshot boundary 20, order 101 has amount 700. A committed update at 21 gives amount 900. A committed deletion at 22 removes the order. After loading the snapshot and applying the log in source order, the destination first shows 700, then 900, then no order 101.

Figure

A snapshot and its matching log boundary

A snapshot and its matching log boundarySnapshot at 20 · 101 → 700. Update at 21 · 101 → 900. Delete at 22 · 101 → ∅Snapshot at 20101 → 700Update at 21101 → 900Delete at 22101 → ∅
The initial copy supplies the old value. Later log positions advance it. Positions are illustrative.

Apply each operation by its meaning#

The snapshot record says the row exists at the snapshot boundary. An update can replace the target row by key with its new values. A delete tells the consumer to remove that key or record its deletion according to the target’s model. In a Debezium PostgreSQL change record, operation codes distinguish snapshot reads, inserts, updates and deletes. Old values depend on the source’s replica identity and connector configuration. Do not assume that every delete carries a complete old row. Debezium, Data change events and Replica identity

Debezium can also emit a Kafka tombstone after a delete. The delete describes a source mutation. A tombstone is a record with the same key and a null value, used by Kafka’s log compaction. The consumer needs a rule for both forms if they are present.

A source position tells the connector where to continue within a particular source history. One transaction can produce several row changes. To deduplicate individual changes, use an identity that distinguishes those changes within the source history. The identity can require a source identifier and a within-transaction order in addition to a transaction or log position.

Keep replay possible#

A PostgreSQL logical replication slot can retain WAL required by a consumer. A stalled consumer can therefore consume disk space indirectly through retained log segments. When the required history is removed or the slot is invalidated, an old cursor alone cannot recover it. The connector then needs a new consistent start or another retained source for the missing interval. PostgreSQL 18, §47.2

A logical slot can resume from an earlier position after a source crash. An independent failure can also occur after a connector publishes a change and before it saves progress. Assume recent changes can arrive again unless the complete connector path supplies a stronger guarantee. Replaying the update at 21 immediately on top of the same version can leave 900 unchanged. Applying that older update after the deletion would recreate the row. A consumer that allows reordered retries needs to retain enough version information, including deletion state, to reject obsolete changes. PostgreSQL 18, §47.2 Debezium, Behavior when things go wrong

Next#

CDC now produces events with an order at the source. Queues and event logs examines what order survives transport, where consumers store their progress and how they read the same history independently.

References#

  1. PostgreSQL Global Development Group. PostgreSQL 18 documentation. Logical Decoding Concepts, §47.2. Describes decoding, slots, exported snapshots and restart behavior.
  2. Debezium Project. Debezium connector for PostgreSQL. Sections on snapshots, events, replica identity and errors specify the connector behavior.
  3. Oracle. MySQL 8.4 Reference Manual. The Binary Log, §7.4.4. Explains change history, replication and logging formats.