shuffleet
Home
Practice
Problems
Lessons
Plans
Compete
Challenges
Interviews
Leaderboard
Progress
Skills
Analytics
Submissions
Account
Settings

Change data capture

Advanced · 4 min read

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:

lsnopcustomer_idemail
101c (create)c1a@x.com
102cc2b@x.com
103u (update)c1a@new.com
104d (delete)c2
105uc1a@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.

The change log in commit order: for each key only its last change counts, so c1 ends at lsn 105 and c2 is deleted

In SQL

Take the last change per key by log order, then apply it:

SQL
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)
deletesinvisible: the row is just gonearrive as d events
several changes between runsonly the last stateevery change
needsan updated_at everyone maintainsaccess to the database log
load on the sourcea query per runreads the log, not the tables

When to use it

A log-based CDC pipeline: database, its log, connector, queue, raw change table, current table, with an initial snapshot

situationapproach
small table (up to a few million rows), daily freshnessfull reload every run: simplest, catches every delete
larger table, daily or hourly freshness, no hard deletes (rows are soft-deleted), a reliable updated_atquery-based incremental (updated_at >= last watermark) + MERGE
hard deletes matter, or every intermediate change matters (audit, fraud), or minutes of freshnesslog-based CDC (Debezium, a cloud DMS, a managed connector)
you can't add query load to the production databaselog-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 whose updated_at someone 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.