Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

What is log-based change data capture, and how does Debezium capture changes from PostgreSQL and MySQL?

Log-based change data capture reads the database's own transaction log, so it sees every committed insert, update and delete, in commit order, without polling tables or adding triggers. Debezium does this for PostgreSQL through logical decoding, using the pgoutput plugin with a replication slot and a publication, and for MySQL by reading the row-based binlog as if it were a replica with its own server id. It usually runs on Kafka Connect, takes an initial snapshot of existing rows, and then writes one event per change, with before and after images and source metadata, to a Kafka topic per table.

Why this question is asked

Sooner or later, other systems need to know when our data changes: a search index, a cache, a warehouse or another service. Polling tables misses deletes and puts load on the database, and triggers slow every write. Log-based CDC solves this by reading what the database already writes for its own recovery. Interviewers ask about it because it is now the standard way to move data out of operational databases, and because running it safely requires understanding slots, binlogs and snapshots. In this answer, we will recap why the log is the right source, see how Debezium reads PostgreSQL and MySQL, look at an event, explain the initial snapshot, and finish with what happens when the connector stops.

1Why Read the Log Instead of the Tables

Every relational database writes a log of changes before applying them, for crash recovery and replication. PostgreSQL calls it the write-ahead log (WAL), MySQL has the binary log (binlog). The log contains every committed change, including deletes, in the exact order transactions committed.

The older approaches each have a gap. Polling with WHERE updated_at > last_run misses deletes, misses writers that forget the timestamp, and can skip rows from long transactions. Triggers that copy changes into an audit table capture everything but add work to every write in the same transaction. Reading the log has neither problem: the database writes it anyway, so capturing changes adds little load, and nothing in the application has to change.

2How Debezium Reads PostgreSQL

PostgreSQL's WAL is a physical log of page changes, not directly readable as row changes. Logical decoding is the feature that turns it into a stream of row-level changes. It needs wal_level = logical, an output plugin that formats the changes, and a replication slot that remembers how far a consumer has read. Debezium is usually configured with pgoutput, the plugin built into PostgreSQL since version 10, so nothing has to be installed on the server. A publication lists which tables are streamed.

Java
-- on the PostgreSQL server
ALTER SYSTEM SET wal_level = logical; -- needs a restart
CREATE PUBLICATION dbz_orders FOR TABLE public.orders, public.order_line;
-- Debezium connector configuration (Kafka Connect, JSON)
{
"connector.class": "io.debezium.connector.postgresql.PostgresConnector",
"plugin.name": "pgoutput",
"slot.name": "orders_cdc",
"publication.name": "dbz_orders",
"database.hostname": "orders-db",
"database.dbname": "orders",
"topic.prefix": "shop"
}
SELECT slot_name, active, confirmed_flush_lsn FROM pg_replication_slots;
-- orders_cdc | t | 0/5A3C1F8
Step throughDebezium streaming one change from PostgreSQL
ApplicationPostgreSQLDebeziumKafka
UPDATE orders SET status = 'PAID'; COMMIT
write WAL record
read from replication slot via pgoutput
row change: before, after, LSN
event to topic shop.public.orders
ack
confirm LSN, slot may release WAL

7 messages in order. Play them, or step through them one at a time.

The slot is the important part to understand. It records the last position in the WAL that Debezium has confirmed, and PostgreSQL keeps all WAL after that position until the consumer confirms it. That guarantees no change is lost, and it is also the main operational risk, as we will see.

3How Debezium Reads MySQL

MySQL replicas copy data by reading the primary's binlog, and Debezium does the same. It connects with the replication protocol, presents itself as a replica with its own unique server id, and reads binlog events. The binlog must be in ROW format, so that each event contains the actual row values rather than the SQL statement; binlog_row_image = FULL gives complete before and after images. GTIDs, global transaction ids, are recommended so the connector can resume correctly after a failover.

One difference from PostgreSQL is that the MySQL binlog also contains DDL statements. Debezium parses them and keeps the history of table schemas in a separate schema history topic, so it can interpret old binlog events with the schema that was valid at the time. And MySQL has no slot: the binlog is kept for a fixed time, binlog_expire_logs_seconds (30 days by default in MySQL 8), whether or not Debezium has read it.

Properties
# my.cnf
binlog_format = ROW
binlog_row_image = FULL
gtid_mode = ON
enforce_gtid_consistency = ON
# connector: "database.server.id": "184054", unique among all replicas

4What a Change Event Looks Like

Debezium writes each change as an event to a Kafka topic per table, named from the topic prefix, schema and table, such as shop.public.orders. The key is the row's primary key, so all changes to one row go to the same partition and stay in order. The value is an envelope with the row before and after the change, the operation, and metadata about where the change came from.

JSON
{
"before": {"id": 501, "status": "NEW", "total": 59.98},
"after": {"id": 501, "status": "PAID", "total": 59.98},
"source": {"connector": "postgresql", "db": "orders", "table": "orders",
"lsn": 94617080, "txId": 7731, "ts_ms": 1762412400123},
"op": "u", // c = insert, u = update, d = delete, r = snapshot read
"ts_ms": 1762412400456
}

For a delete, after is null and Debezium normally follows with a tombstone, a message with the same key and an empty value, so that Kafka log compaction can remove the key. In PostgreSQL, the before image contains only the key columns unless the table has REPLICA IDENTITY FULL, so teams that need full before images set that on the tables they capture.

5The Initial Snapshot

Streaming from the log only gives changes from now on. A consumer such as a search index also needs every row that already exists. So on its first start, by default, Debezium takes a snapshot: it records the current log position, reads every row of the captured tables in a consistent view and emits them as events with op r, and then starts streaming from the recorded position. Because the position was recorded at the start, no change between the snapshot and the stream is lost.

Snapshots of large tables take hours and put read load on the database, so teams often snapshot from a replica or use incremental snapshots, which Debezium supports by reading tables in chunks while streaming continues, triggered through a signal table.

6Follow-up questions

What happens if the connector is down for a day?

With PostgreSQL, the replication slot keeps all WAL since the last confirmed position, so nothing is lost and Debezium resumes where it stopped. But the primary's disk fills with retained WAL meanwhile, which on a busy database can be hundreds of gigabytes. Since PostgreSQL 13, max_slot_wal_keep_size can cap this; if the cap is hit, the slot is invalidated and we must take a new snapshot. With MySQL, there is no slot, so the connector can resume as long as the binlog files it needs still exist; if they were purged during the outage, it fails and needs a new snapshot, for example with snapshot.mode set to when_needed. Either way we alert on lag long before a day passes.

Is Debezium delivery exactly once?

By default it is at least once. If the connector crashes after writing events but before storing its offset, it will send some events again after restart. Consumers should be idempotent, for example by upserting by primary key. Newer Kafka Connect versions support exactly-once source connectors, and Debezium supports this for some connectors; check your version.

Does CDC slow down the database?

Much less than triggers or polling, since the log is written anyway. The costs are decoding work on PostgreSQL, a replication connection, retained WAL or binlog when the consumer lags, and read load during snapshots.

7Conclusion

In this answer, we saw that log-based CDC reads the transaction log, so it captures inserts, updates and deletes in commit order without polling or triggers. Debezium reads PostgreSQL through logical decoding with the pgoutput plugin, a replication slot and a publication, and reads MySQL's row-based binlog as a replica with its own server id. It writes per-table Kafka topics with before and after images, operation and source metadata, starts with a consistent snapshot of existing rows, and resumes from its stored position after downtime as long as the log has been kept.

What to say in the interviewAbout 80 seconds aloud

Open with

Log-based CDC reads the database's transaction log, the WAL in PostgreSQL or the binlog in MySQL, instead of querying tables. The log already contains every committed change in commit order, including deletes, so we get complete data without polling, without triggers, and without touching the application.

Then, if they want more

Debezium, usually running on Kafka Connect, does this per database. For PostgreSQL it uses logical decoding: wal_level logical, the built-in pgoutput plugin, a publication that lists the tables, and a replication slot that remembers how far Debezium has confirmed.

For MySQL it connects like a replica with its own server id and reads the binlog in ROW format, keeping a schema history topic because the binlog contains DDL.

Each change becomes an event on a topic per table, keyed by primary key so a row's changes stay ordered, with before and after images, the operation, and source metadata like the LSN and transaction id. On first start it takes a consistent snapshot of existing rows, then streams from the position recorded at the start.

If the connector is down for a day, PostgreSQL keeps WAL in the slot so nothing is lost, but the primary's disk grows, so I'd monitor slot lag and set max_slot_wal_keep_size. In MySQL, it works only if the binlog hasn't been purged.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 8, PostgreSQL 13
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is log-based change data capture, and how does Debezium capture changes from...": https://scrivio-interview-library.netlify.app/databases/analytics-and-cdc/04ea69d61f/