Stackbook Logo
data-consistencyestablished · medium operational burden

Change Data Capture Pipeline

Also known as: cdc-to-warehouse, log-based-replication-pipeline

Intent

Stream committed database changes from the transaction log into analytics stores with low latency and low source impact.

Problem

Periodic full or timestamp-based extracts load operational databases, miss hard deletes and deliver data hours late. Dual writes from applications to analytics risk inconsistency.

Forces

  • •Low impact on the source database
  • •Capture inserts, updates and deletes in order
  • •Initial snapshot must join seamlessly with the stream
  • •Schema changes in the source must propagate

Solution

Overview

A change data capture pipeline turns a database's commit log into a stream of row-level change events and delivers them to analytical systems. Compared with polling for updated timestamps, log reading sees every change, including deletes, in commit order.

How it works

A connector attaches to the log, performs an initial snapshot, then tails changes. Each event holds the key, before and after images, operation type and source position.

OLTP DB --WAL--> connector --> topic db.orders (key = order_id)
                                   |--> sink: MERGE into warehouse.orders

Applying events downstream usually follows this merge, with the latest event per key winning:

MERGE INTO dw.orders t USING staged_changes s ON t.order_id = s.order_id
WHEN MATCHED AND s.op = 'd' THEN DELETE
WHEN MATCHED THEN UPDATE SET *
WHEN NOT MATCHED AND s.op <> 'd' THEN INSERT *;

Delivery is at least once, so applying by key with the source log position as a tiebreaker makes replays harmless. A history table of all events supports audit and slowly changing dimensions. See the cdc pattern for the general technique.

Pitfalls

  • Stuck replication slot: if the consumer stops, PostgreSQL retains WAL indefinitely. Alert on slot lag and set limits.
  • Schema evolution: adding or renaming columns requires a registry and compatible sinks.
  • Snapshot cutover errors cause gaps or duplicates; rely on connector-managed handoff.
  • Leaking raw table internals as an API; consider contracts or an outbox for domain events.
  • Large transactions arrive as bursts and stress consumers.

Key takeaways

  • Read the log, not the tables, for low-impact complete capture.
  • Make sinks idempotent because delivery repeats.
  • Monitor connector lag and replication slot size.
  • Treat captured schemas as a coupling risk.

✓ When to Use

  • •Near-real-time replication of operational data to analytics
  • •Need to capture deletes and every intermediate update
  • •Cannot add load of heavy extract queries to the source

✗ When Not to Use

  • •Daily snapshot is sufficient and sources are small
  • •Source database cannot expose its log
  • •You need business events, not row changes (use an outbox)

Pros

  • +Low latency with little source load
  • +Captures deletes and ordering
  • +No application code changes

Cons

  • −Exposes internal table schemas downstream
  • −Operating connectors and log retention
  • −Snapshot and schema change handling is delicate

Cost Profile

Infrastructure

Medium - connector cluster and event log

Operational

Medium - monitoring lag and replication slots

Cognitive

Medium - ordering and merge semantics

Failure Modes

  • Unconsumed replication slot fills source disk

  • Connector restart replays events causing duplicates

  • Source schema change breaks downstream parsing

Real-World Examples

Alternatives

    Related Patterns

    • cdc

    Competency Domains

    data statedistribution communication