In two sentences
Every database keeps a log of each insert, update and delete it commits, in order. Change data capture (CDC) reads that log and replays it somewhere else, so the copy can catch up without re-reading whole tables, and deletes are carried over too.
See it
The change log for the customers table:
| lsn | op | customer_id | |
|---|---|---|---|
| 101 | c (create) | c1 | a@x.com |
| 102 | c | c2 | b@x.com |
| 103 | u (update) | c1 | a@new.com |
| 104 | d (delete) | c2 | |
| 105 | u | c1 | a@final.com |
Apply it and the copy should hold one row: c1, a@final.com. c2 was created and deleted.
The lsn (log sequence number) is the order the database committed the changes. It is the only safe order.
Two changes can share a timestamp, and clocks drift; the log position never ties.
In SQL
Take the last change per key by log order, then apply it:
MERGE INTO customers t
USING (
SELECT * FROM (
SELECT *, row_number() OVER (PARTITION BY customer_id ORDER BY lsn DESC) AS rn
FROM customer_changes
) WHERE rn = 1
) s
ON t.customer_id = s.customer_id
WHEN MATCHED AND s.lsn > t.lsn AND s.op = 'd' THEN DELETE
WHEN MATCHED AND s.lsn > t.lsn THEN
UPDATE SET email = s.email, lsn = s.lsn
WHEN NOT MATCHED AND s.op <> 'd' THEN
INSERT (customer_id, email, lsn) VALUES (s.customer_id, s.email, s.lsn);Storing lsn on the target makes the load idempotent: replaying yesterday's log changes nothing.
Log-based vs query-based
query-based (WHERE updated_at > last_run) | log-based (Debezium and similar) | |
|---|---|---|
| deletes | invisible: the row is just gone | arrive as d events |
| several changes between runs | only the last state | every change |
| needs | an updated_at everyone maintains | access to the database log |
| load on the source | a query per run | reads the log, not the tables |
When to use it
| situation | approach |
|---|---|
| small table (up to a few million rows), daily freshness | full reload every run: simplest, catches every delete |
larger table, daily or hourly freshness, no hard deletes (rows are soft-deleted), a reliable updated_at | query-based incremental (updated_at >= last watermark) + MERGE |
| hard deletes matter, or every intermediate change matters (audit, fraud), or minutes of freshness | log-based CDC (Debezium, a cloud DMS, a managed connector) |
| you can't add query load to the production database | log-based CDC: it reads the log, not the tables |
Log-based CDC is the most capable and the most to operate: connectors, a queue, offsets, schema changes. Choose it when the rows above actually need it, not by default.
Best practices
- Land the change events untouched, append-only, and build the current-state table from them. If the apply logic has a bug, fix it and replay; nothing is lost.
- Store the log position on the target and guard every update with it; replays then change nothing.
- Decide what a delete means downstream. Remove the row (current state), or keep it with
deleted_at(history, audits). Facts that point at a deleted customer still need to join to something. - Plan for schema changes. A column added at the source arrives in the events; the apply job must either handle it or fail loudly, not drop it.
- Monitor lag (how far behind the log the copy is), and keep the source's log retention longer than your longest possible outage.
Common mistakes
- Ordering by timestamp. Two changes in the same millisecond, or a clock skew, and the older one wins. Order by the log position.
- Applying every change in turn instead of the last. Correct but slow, and wrong if the batch isn't sorted. Collapse to the last change per key first.
- Inserting a key whose last change is a delete. A key created and deleted inside one batch must not appear.
- Forgetting the initial snapshot. The log only holds recent changes; CDC starts from a full copy, then follows the log from that point.
What an interviewer asks next
- "Why not just poll
updated_at?" Deletes, intermediate states, and rows whoseupdated_atsomeone forgot to set. - "The consumer was down for a day. What happens?" It resumes from its last stored log position, as long as the source kept the log that long.
Go further
Kleppmann's talk is the best explanation of why the log is the real source of truth. Debezium's docs show what a real change event looks like.
Practise it
Apply a CDC Change Log, then Upserts That Leave Delete Files Behind to see what deletes cost in a lake.