Skip to the page

Streaming and CDC · senior

Change data capture with Debezium

Log-based CDC reads the database change log, so it sees every insert, update and delete in order. Apply changes by log position, never by deduplicating on a key.

The notes

What you must know

  • Log-based CDC reads the MySQL binlog (row format) or the Postgres WAL through logical decoding and a replication slot. It sees every change in commit order with little load.
  • A query for updated_at after the last run misses deletes and in-between changes, misses rows that commit late with an older updated_at, needs an index and adds load.
  • An event has before, after, op (c create, u update, d delete, r snapshot read), a source block (database, table, binlog position or LSN, timestamp) and ts_ms.
  • Initial load is a consistent snapshot, then streaming from the log. Incremental snapshots (started by a signal) copy big tables in chunks without long locks.
  • A delete is a d event followed by a tombstone (a null value) so a compacted topic can forget the key. The key is the primary key, so all changes to one row go to one partition in order.
  • Runs as a Kafka Connect source connector, or as Debezium Server.
  • Source needs: MySQL binlog_format=ROW, full row images and enough binlog retention; Postgres wal_level=logical and a slot. A slot keeps WAL while the connector is down, so monitor it.
  • Apply changes with a MERGE ordered by log position. Within a batch keep the latest change per key by log position (binlog file and position, or LSN), not by timestamp, which can tie. In the MERGE update only if the incoming position is newer than the stored one.
  • dropDuplicates on CDC data keeps an arbitrary row, so an older status can win. Repair by replaying from Kafka (within retention) or from bronze, or re-snapshot the affected rows.
  • Prevent: a daily reconciliation of counts by status against a source replica, and a test with several changes per key in one batch.

The red pen

Common traps: mistake → fix

  • Mistake: CDC means database triggers

    Fix: log-based CDC reads the transaction log.

  • Mistake: Order by ts_ms is enough

    Fix: timestamps tie; use log position.

  • Mistake: dropDuplicates is a safe fix for MERGE multiple-match errors

    Fix: it can keep an old change.

Easy to hard

Questions interviewers ask, easy to hard

  1. How does log-based CDC with Debezium work?
  2. Why is it better than polling for updated_at?
  3. Delivered orders show an older status in the lake. How do you find the cause?
  4. What must the source database have for Debezium?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview