Question presented to candidate: "Our product catalogue lives in Postgres. Search runs on Elasticsearch and product pages read from Redis. Today the API writes to all three and they drift apart. How would you use change data capture with Debezium to keep search and the cache in sync? Walk me through how Debezium reads changes from Postgres, what happens on the first start and after a crash, and what can still go wrong."
What a strong answer should cover:
- Log-based CDC: Postgres writes every committed change to the WAL; with
wal_level=logical, a logical replication slot and a publication, Debezium (via thepgoutputplugin) streams row changes in commit order into Kafka topics - First start: a consistent initial snapshot (events with
op=r) then streaming from the recorded LSN; incremental snapshots for backfills later - The change event envelope:
before,after,op(c, u, d, r),sourcewith the LSN and transaction id; tombstones after deletes for compacted topics;REPLICA IDENTITY FULLfor full before-images - Delivery is at-least-once: after a crash the connector resumes from its last committed offset, so sinks must be idempotent โ upsert by primary key with the LSN as an external version, delete on
op=d - Ordering per row by keying messages on the primary key; the cache race (a reader re-filling a stale value after an invalidation) and how a versioned write or short TTL fixes it
- Operations: a stalled slot makes the primary retain WAL until the disk fills; heartbeats,
max_slot_wal_keep_size, slot failover, schema changes and monitoring lag
Clarifying questions expected:
- How fresh must search and cache be: sub-second, or are a few seconds fine?
- What is the write rate on the catalogue tables, and do deletes and updates both need to propagate?
- Do we already run Kafka and Kafka Connect, and can we get replication privileges on the Postgres primary?
Code / implementation expected: Optional โ connector configuration and a consumer sketch; the example simulates snapshot, streaming, a connector replay, an LSN-versioned index and the cache race.