Video in progress

You want to do CDC from PostgreSQL with Debezium. What risks come with a logical replication slot, and how do you manage them?

A slot doesn't let WAL be deleted until the consumer has read it. If Debezium stops, WAL grows until the disk is full and the primary goes down. So slot lag monitoring, a max_slot_wal_keep_size limit, and a plan for recovering from a lost slot are mandatory.

📖 Answer

Logical decoding emits row-level changes from the WAL. The slot stores a position saying "this is how far I've read", and the server keeps the WAL segments after that point.

A case I've seen on prod: the Kafka Connect cluster went down over the weekend, and on Monday the primary's disk was at 100%, with the pg_wal folder at 400 GB. The database stopped writing.

The protection:

-- how much WAL each slot is holding
SELECT slot_name, active,
       pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained
FROM pg_replication_slots;
  • Alert: retained > 10-20 GB, or active = false for more than 10 minutes.
  • max_slot_wal_keep_size (PG 13+): if the limit is exceeded, the slot becomes invalid while the database stays alive. It's a deliberate choice: re-snapshotting CDC is better than the database going down.
  • On a database with few changes, a slot may fail to move forward; Debezium's heartbeat setting fixes that.

Other risks:

  • Failover. Before PG 17, logical slots weren't carried over to the replica: after a failover the slot is gone and Debezium has to take a new snapshot. PG 17 added failover slots, but they need configuring.
  • Schema changes. If a column is dropped or its type changes, consumers can fail. That's why I prefer doing CDC on an outbox table rather than directly on the tables: the table structure stays an internal detail, and the event format is the contract.
  • REPLICA IDENTITY: by default only the PK is emitted on UPDATE/DELETE; if you need the old values, use FULL, but that makes the WAL bigger.