LOAD-BEARINGfree
High-level designTier 7 · unit 6 of 7 · 15 min read

Change data capture: the log as an integration point

Every lesson in this tier assumes a copy exists somewhere — the warehouse, the search index, the cache, the other service's read model. None of them has said how the data gets there. That question has three answers in common use, and two of them are wrong in ways that take months to notice.

The two ways that quietly diverge

Dual writes from application code. The service writes the row, then publishes to Kafka or updates the search index. It reads as one operation and is two, with no transaction across them. The broker times out after the commit and one write survives; the process is killed between the two lines and one write survives. Neither failure raises anything, because nothing failed — each system did what it was told. You discover it a quarter later when someone notices the search index is missing four thousand records, and there is no way to tell which four thousand without comparing the whole thing.

Polling a timestamp column. Select everything where updated_at is greater than the last cursor, every thirty seconds. Better, and still broken in three specific ways. It cannot see deletes, because the row it needed to tell you about is gone. It cannot see intermediate states — a row updated twice between polls yields one event, and if your consumer cares about the transition it just lost it. And it races transaction visibility: a long transaction stamps updated_at when it writes but becomes visible when it commits, so a row can appear with a timestamp your cursor has already passed, and you will never select it again. Add a full-table scan on the primary every thirty seconds and you have paid for all three.

The database already keeps the record you want

Lesson 3.01's write-ahead log exists so the engine can recover from a crash: an ordered, durable, complete account of every change the database has ever made, including the deletes, in commit order. That is not a lesser version of the change stream you were trying to build by polling. It is a better one, and it is already there, already correct, already paid for.

Change data capture tails it — the same mechanism a read replica uses (Lesson 4.04), pointed at your pipeline instead of another Postgres. What you get is worth listing plainly, because each item is a bug the other two approaches have: every change including deletes, in commit order per key, with no application code involved, and replayable from any retained position.

What it costs, stated honestly

  • You are now coupled to the physical schema. The previous lesson spent nine hundred words explaining that a table is not a contract. CDC subscribes downstream consumers to exactly that, so a column rename lands in the warehouse as a schema change nobody agreed to.
  • The replication slot is a loaded gun aimed at your primary. The database must retain WAL segments until the slowest consumer has read them. A consumer that stops does not degrade the pipeline gracefully — it makes the primary's disk grow without bound, and the operational database that runs your product goes down because an analytics job died on Friday. This is the failure that matters and it is covered again at the bottom of this lesson.
  • Snapshot and stream have a seam. A new consumer needs the history it missed, so it reads a consistent snapshot and then switches to the live stream, and the handover has to overlap rather than abut — which means downstream sees some changes twice and must not care.

The outbox, which fixes the first cost

Write the business change and a deliberately shaped event row to an outbox table in the same transaction, then point CDC at the outbox rather than at your whole schema. Lesson 6.05 introduced this as a way to publish reliably; read it again in this tier's terms and it is doing something else as well. The event is atomic with the change, which is what dual writes could not manage, and its shape is chosen rather than leaked, which is the contract Lesson 7.05 asked for. One table, both problems.

The cost is that somebody must write and maintain those event definitions, and a field the outbox does not carry is a field no consumer can have. That is the same cost as any interface, and the same benefit.

What downstream still owes

The stream is at-least-once — a consumer restart replays from the last committed position, so every consumer must be idempotent (Lesson 6.07), and for CDC the natural shape is an upsert keyed on the primary key rather than an insert. Ordering is guaranteed per key and not across keys, so any consumer that needs a cross-table invariant has to be told that it is not going to get one. And deletes arrive as events, which means every downstream copy needs a policy for them — a search index that ignores delete events is a privacy incident waiting for a subject access request (Lesson 12.07).

The trap

Nobody alerts on replication slot lag until the first time it takes production down. The pipeline consumer dies quietly on a Friday, the slot holds WAL for it out of politeness, and the primary's disk fills over the weekend — so the checkout database stops accepting writes because of a downstream job whose entire purpose was to be optional. The blast radius runs backwards, from the least important system to the most, and no amount of care in the consumer prevents it. Alert on slot lag in bytes with a threshold well under free disk, and decide in advance — in writing, before the incident — that the correct action at 3am is to drop the slot and re-snapshot later. Losing a day of warehouse freshness is recoverable. Losing the primary is not.

The tradeoff sheet

Problem solved
Getting operational changes into a search index, cache or warehouse without dual writes that silently diverge or polling that misses deletes.
Rejected, and why
Dual writes are simpler and lose data at every partial failure. Timestamp polling is simpler still and cannot see deletes, intermediate states, or late-committing transactions.
The trade
Downstream consumers coupled to your physical schema unless you add an outbox, plus a replication slot whose backpressure lands on your primary.
First to break at 10x
The slowest consumer. At 10x, a consumer that cannot keep up holds WAL on the primary, and the operational database degrades because of an analytics job.
What it does to the bill
A connector fleet and the broker behind it. Cheap relative to what it replaces, with the caveat that WAL retention is disk on your most expensive instance.
Day-2 operations
Alert on replication slot lag in bytes against free disk, and decide in advance that dropping the slot beats losing the primary. Re-snapshotting is recoverable; a full disk is not.

The nuance

“The database already keeps the record you want.”

It keeps a record shaped for its own recovery, not for you. Logical decoding has to reconstruct row-level changes from physical log entries, which is why the feature has engine-specific limits — a column with no replica identity yields a delete event with no key, TOASTed values may arrive unchanged rather than as their new value, and DDL usually does not appear at all. The log is complete about what the engine needed; the gaps are exactly where your needs and its needs differ.

Going deeper

  • Turning the Database Inside-Out Martin Kleppmann, 2015
    The argument that a change log is the primary construct and the tables are the derived views, rather than the other way round. Read it after this lesson, when the idea has somewhere to land.