HAMZA KAREEM / CONCEPTS hamzawi.dev

Lesson 1482 min read

Change Data Capture (Log-Based vs Polling)

your updated_at > last_run sync is quietly losing rows

System designsystem design / data integration

The scenario

Orders live in Postgres. A cron job every 5 minutes pushes changes to your search index:

SELECT * FROM orders WHERE updated_at > :last_run;
-- then: last_run = now()

Works for months. Then support says order #88412 shows pending in search but shipped in the DB. Re-run the job — still wrong. The row's updated_at is 3 hours old, so it will never be selected again.

The race you cannot see in that query

updated_at is stamped when the row is written. The row becomes visible when the transaction commits. Those are two different moments.

10:00:04  tx A: UPDATE orders ... updated_at = 10:00:04
10:00:05  cron runs, last_run := 10:00:05   (A uncommitted -> invisible)
10:00:09  tx A commits
10:05:05  cron: WHERE updated_at > 10:00:05
          A's row says 10:00:04  -> never seen again

Silent, permanent loss. A long write transaction widens the window. And deletes vanish entirely — there is no row left to carry a timestamp.

The usual patches

"I'll overlap by 30 seconds." That shrinks the window, never closes it, and re-sends rows. "I'll add triggers and a tombstone table." Now every write pays trigger cost and you own a second write path that can drift from the first.

The real fix — read the log, not the table

Your database already keeps an ordered, commit-time record of every change: the WAL (Postgres) or binlog (MySQL). Log-based CDC tails it, so you get commit order instead of clock order, plus deletes, plus before/after images.

-- Postgres: wal_level = logical
CREATE PUBLICATION orders_pub FOR TABLE orders;
SELECT pg_create_logical_replication_slot('idx','pgoutput');

Each event carries its position:

{"op":"u","lsn":"0/1A2B3C8","before":{"status":"pending"},
 "after":{"status":"shipped"},"ts_ms":1764038400123}

You checkpoint the LSN, not a timestamp. Resume means "everything after 0/1A2B3C8". No window, no clock skew, no missed commit.

The trap that pages you at 3am

A replication slot is a promise: Postgres retains WAL until your consumer confirms it. Consumer dies Friday night → WAL piles up → pg_wal fills the disk → the primary stops accepting writes. Polling degrades gracefully; CDC fails hard. Set guard rails on day one:

ALTER SYSTEM SET max_slot_wal_keep_size = '20GB';

SELECT slot_name, active, pg_size_pretty(pg_wal_lsn_diff(
         pg_current_wal_lsn(), confirmed_flush_lsn)) AS behind
FROM pg_replication_slots;

Alert on behind. Drop slots you abandoned.

When polling is still the right call

  • Append-only tables: no updates, no deletes, monotonic id — just page by id
  • Targets that tolerate "eventually, roughly": analytics rollups
  • Managed DB where you cannot get wal_level = logical — then use an outbox table written inside the same transaction, and accept the write cost

Your exercise

Take one polling sync you own and answer two questions.

  1. How long is your longest write transaction? That duration is your data-loss window:
SELECT max(now() - xact_start) FROM pg_stat_activity
WHERE state LIKE 'idle in transaction%' OR state = 'active';
  1. When a row is deleted, how does the target ever find out? If the honest answer is "it doesn't", you already have drift — go count the rows on both sides today.