Debezium WAL growth on Postgres: why the disk fills and how to fix it · Turbolytics

Turbolytics

4 min read Original article ↗

Debezium can fill a Postgres disk while Kafka Connect says the connector is running. We tested the fixes on Debezium 3.4.3. One popular fix breaks the next restart. Two others work.

Debezium says running while the disk fills

Your Debezium connector reports RUNNING. Its replication slot is connected. Meanwhile, the slot holds more and more of the Postgres write-ahead log (WAL), and the disk fills. No health check fails until Postgres runs out of space. Zalando called this their “single biggest operational issue” with Debezium. Below, Control flags it on a connector that Kafka Connect reports as healthy.

Control's Verdicts page. Under "Holding now", two rows read "Holding WAL" for slots dbz_orders and dbz_r6_a on billing. Each says "A consumer holds this slot but confirms nothing, so WAL grows." and has held for about 14 minutes.

Why a quiet table fills the disk

Debezium reads Postgres through a replication slot. Postgres keeps all WAL after the last position Debezium confirmed. Debezium confirms a position only when a table it captures changes. WAL belongs to the whole server, so writes to every other table still add to it. When your captured tables go quiet and the rest of the database stays busy, Debezium confirms nothing, and the slot keeps every byte.

The usual fixes don’t work

The common advice is to turn on a heartbeat. A heartbeat alone changes nothing here: Debezium still has no change to confirm. Debezium 3.4 added lsn.flush.mode=connector_and_driver, which confirms WAL during quiet periods. It stops the growth. Then, on the next restart, the connector refuses to start: “this is no longer available on the server”. In our test, that connector delivered 106,600 of 212,800 rows before it stopped. Debezium tracks related bugs in #1605 and #1863.

Control's verdict page for Holding WAL on slot dbz_setup_b, the connector_and_driver setup. "What is happening" reads "No consumer holds this slot, and it holds WAL." Consumer connected drops from 100% to 0 at the hand restart, and WAL held climbs from under 15 MB to 490 MB. Under "Underneath", the connector acme-connect/setup-b carries the warning: connector_and_driver needs offset.mismatch.strategy=trust_slot, or the connector fails its next restart after a quiet spell.

What we tested

We ran Postgres 16 and Debezium 3.4.3.Final, with four connectors side by side. Each captured one table, items. Other writers kept the rest of the server busy. We wrote to items for 5 minutes, stopped for 25 minutes, restarted every connector by hand, and wrote to items for 10 more minutes. Control watched all four slots:

ConnectorSettingsWAL held while items was quietRestartRows delivered
Adefaults, heartbeatclimbed to about 680 MBok212,800 of 212,800
Bconnector_and_driver, heartbeatunder 15 MBfailed106,600 of 212,800
CB, plus trust_slotunder 15 MBok212,800 of 212,800
Dheartbeat that writes a captured table20 to 40 MBok212,800 of 212,800

Four of Control's "WAL held by the slot" charts, one per connector, over one hour; each chart has its own scale. A climbs in a straight line to about 680 MB while items is quiet, then drops when writes resume. B stays under 15 MB, then climbs to about 410 MB after its failed restart, with no consumer. C stays under 15 MB while items is quiet. D stays between 20 and 40 MB. C and D rise and fall when writes resume.

What works

Two setups held WAL down, restarted cleanly and lost no rows. Setup C keeps connector_and_driver and adds offset.mismatch.strategy=trust_slot, so the connector trusts the slot’s position on restart. Setup D adds a small table to the connector and a heartbeat query that updates it, so the connector always has a change to confirm. A third setup, a heartbeat that emits a logical message, from Gunnar Morling’s guide to replication slots, also works. It moves every slot on the database at once, though, so it can hide a broken connector next to it.

"lsn.flush.mode": "connector_and_driver",
"heartbeat.interval.ms": "10000",
"offset.mismatch.strategy": "trust_slot"

One more trap: reusing a connector’s name

Kafka Connect keeps a deleted connector’s offsets. If you drop a slot and create a new connector with the old name, the new connector starts from the old offset. It fails with the same “no longer available” error. Give a recreated connector a new name. In our test, only connector C, with trust_slot, started under a reused name.

Watch the slot, not the connector

A connector’s status can mislead you. The connector says RUNNING while the WAL fills the disk. The slot shows the failure. Watch four numbers: the WAL the slot holds, how far its consumer is behind, whether a consumer is connected, and how fast the server writes WAL. A healthy slot rises and drops at each flush. A stuck slot climbs in a straight line while its consumer stays connected. Postgres reports the first three in pg_replication_slots, and the write rate in pg_stat_wal. max_slot_wal_keep_size caps how much WAL a slot can hold, at the cost of breaking the slot.

Control's page for slot dbz_orders. WAL held and Behind climb together in a straight line from 0 to about 340 MB over 25 minutes. The database writes WAL at a steady 220 KB a second. The consumer stays connected the whole time. Under "Held by", acme-connect/orders-cdc holds the slot, with lsn.flush.mode connector, Debezium's default, a heartbeat every 10 seconds, and no heartbeat query.

How Control catches it

Control watches both Postgres and the Debezium connector. dbhealth reports each slot on the server, and the Kafka Connect reporter reports each connector and its settings. Control matches them, so each slot names the connector that holds it. When a connected consumer confirms nothing while WAL grows, the holding_wal verdict opens. Its page shows what happened, the trend, the fix for that connector’s settings, and links to the slot, connector and database. A slot.bounded SLO alerts before the disk fills.

Control's verdict page for Holding WAL on slot dbz_orders. It has held since 19:56 UTC. "What is happening" names the fix: set lsn.flush.mode to connector_and_driver with offset.mismatch.strategy trust_slot, or keep lsn.flush.mode and add a heartbeat query that writes a captured table. Charts of WAL held and Behind climb past the "opened" mark. Under "Underneath", the slot dbz_orders holds 329 MB, the connector acme-connect/orders-cdc holds it, and the database is billing.

Monitor Postgres and Debezium for free

Setup takes five minutes. Point dbhealth at your database, add the Kafka Connect reporter to your workers, and Control shows every slot, the connector that holds it, and a verdict when a slot holds WAL that nothing will free. Sign up free.