Replication slot — the bookmark that fills disks
A replication slot is a server-side cursor identified by an LSN that pins WAL recycling at restart_lsn — so a dead or stalled connector means WAL accumulates until pg_wal/ fills the disk and the database goes read-only.
Debezium tails the log and emits one change event per row change — but if it crashes, the database needs to know where it left off so the WAL can be safely recycled. Postgres provides exactly this bookmark; it's called a replication slot, and the position it remembers is an LSN.
Scene 05
Replication slot — the bookmark that fills disks
- Watch
- Try it
- Predict
- Capture
Debezium needs to remember how far it's read so a restart doesn't lose its place. Postgres provides exactly this bookmark — a replication slot — pinned to an LSN. Watch restart_lsn slide forward in step with the write head while the connector is running. The disk stays green because Postgres can recycle WAL bytes behind the slot.
Highlighted lines are the ones running in the diagram right now.
def advance_restart_lsn(slot, new_lsn):if not slot.active:# consumer gone -> restart_lsn frozenreturnif new_lsn <= slot.restart_lsn:return # never moves backwardslot.restart_lsn = new_lsn# only NOW may pg_wal/ GC bytes < restart_lsnwake_checkpointer()
def maybe_recycle_wal(current_lsn):pin = min(s.restart_lsn for s in pg_replication_slots)for seg in pg_wal_segments():if seg.end_lsn < pin:recycle(seg) # safe: no slot needs itelse:keep(seg) # slot pins this WALif disk_used(pg_wal) >= READONLY_THRESHOLD:# cannot durably commit without WAL spaceset_database_read_only()refuse_further_writes()
def confirm_flushed_lsn():while connector.running:flushed = kafka_producer.last_acked_lsn()# standby status update messagereplication_stream.send_status(write_lsn=flushed,flush_lsn=flushed,apply_lsn=flushed,)sleep(status_interval_ms)
Where this sits in Build a CDC pipeline (Debezium + outbox)
Scene 05 of 12. A replication slot is a server-side cursor identified by an LSN that stops Postgres from recycling WAL the connector hasn't read — and an inactive slot is the #1 Debezium production failure.
Up next. The slot tracks where the connector left off in the WAL — but the WAL only goes back so far, so a connector attaching to a database with a billion existing rows cannot replay history from the log alone.
All 12 scenes in Build a CDC pipeline (Debezium + outbox) · Every curriculum