Managing connector offsets

A source connector remembers its place in the database log. Stopping it, reading that place, moving it forward, resetting it and recovering a lost replication slot, with what PostgreSQL does to each.

45 minutesChapter 10Getting events out reliably

A connector tails the inventory_item table of a legacy database and publishes every change to edp.inventory.item-changes. One night someone loads a hundred thousand rows into a neighbouring table, a bad batch of updates corrupts stock quantities, and the connector is stopped for a day for maintenance. Each of these ends with the same question: where does the connector start reading when it runs again, and who decides?

A running connector is stopped with PUT stop; while stopped its offsets can be altered with PATCH or reset with DELETE, and PUT resume starts it again; PATCH or DELETE while running is refused with HTTP 400
Figure 10.1: Offsets change only while the connector is stopped

Learning objectives

  • Read and alter a connector’s offset through the Connect REST API.
  • Distinguish an offset alter from a reset by what each one skips or rebuilds.
  • Explain why an offset behind the slot’s position cannot rewind streaming.
  • Recognize a lost slot from a connector reporting RUNNING with no task.
  • Recover a lost or dropped slot by stopping, dropping and deleting offsets.

Why this matters

When a connector is stopped, a neighbouring table is loaded, or a bad batch of updates is fixed, the same question comes up: where does the connector resume, and does that choice skip changes, repeat them, or rebuild from scratch. Pick the wrong recovery, and every downstream reader is left with a gap or an unexpected flood of snapshot rows. A lost replication slot can also leave the connector reporting RUNNING with no task, so when a monitor watches only for a failed state, it can miss that nothing is being delivered at all.

Where a source connector keeps its place

A source connector has no consumer group. Kafka Connect stores its position in an offsets topic, here edp-connect-offsets, as one record per source partition. For the PostgreSQL connector the source partition is the logical server name, which is the connector’s topic.prefix:

curl -s localhost:8083/connectors/inventory-cdc/offsets
{"offsets":[{"partition":{"server":"edp"},"offset":{"lsn":29748248,"txId":762,"ts_usec":1794407704219520}}]}

lsn is a position in PostgreSQL’s write-ahead log (WAL), written as a bigint. After streaming starts the offset also carries lsn_proc, lsn_commit and messageType. The Debezium documentation does not list these keys; they come from the connector’s source. The record on the offsets topic has the key ["inventory-cdc",{"server":"edp"}] and the same offset as its value.

Connect writes the offsets topic on a timer, offset.flush.interval.ms, which defaults to 60 seconds. Until the next flush, a GET returns the previous position, and a Connect process that dies between flushes restarts from it, so the changes after it arrive a second time. A consumer of the topic has to tolerate duplicates in that case anyway. With a one-second interval every read matches the connector’s real position.

Stopping, listing, altering, resetting

The offsets API of Kafka Connect (KIP-875) has three verbs on /connectors/{name}/offsets: GET lists, PATCH alters and DELETE resets. PATCH and DELETE are accepted only when the connector is stopped. A running connector refuses both with HTTP 400 and a message that names the STOPPED state. PUT /connectors/{name}/stop is the way there: it stops the connector and its tasks, releases their resources and reports state STOPPED with an empty task list. PUT /connectors/{name}/resume starts it again.

curl -X PUT localhost:8083/connectors/inventory-cdc/stop
curl -X PATCH -H 'Content-Type: application/json' localhost:8083/connectors/inventory-cdc/offsets \
  -d '{"offsets":[{"partition":{"server":"edp"},"offset":{"lsn":30131256}}]}'
curl -X PUT localhost:8083/connectors/inventory-cdc/resume

An alter replaces the stored offset. For this connector lsn alone is enough. A reset deletes the stored offset, so the connector behaves as if it had never run: with snapshot.mode=initial it takes a snapshot again. The effect shows only after the resume.

Deleting a connector with DELETE /connectors/{name} does not delete its offsets. A connector created again under the same name finds them and continues.

Offsets and the replication slot

A PostgreSQL connector has a second position that it does not own. The logical replication slot edp_inventory lives in the database and records confirmed_flush_lsn, the last position the connector acknowledged. The slot also decides which WAL the server keeps. The offset is where the connector asks to start; the slot is where the server is willing to start.

When a client starts streaming from a slot, PostgreSQL begins at the requested position or at the slot’s confirmed_flush_lsn, whichever is greater. Two consequences follow:

  • Altering forward skips changes. Set the offset to the current end of the WAL and everything written in between is never published.
  • Altering backward does not rewind. An older offset is clamped to the slot’s position, so nothing before confirmed_flush_lsn can be asked for again. It is not free of duplicates, though: the altered offset holds only an lsn, and the last transaction the connector delivered before the stop arrives a second time, even when the slot had acknowledged it.
An offset altered ahead of the slot makes streaming start at the offset and skip the changes in between; an offset altered behind the slot is ignored and streaming starts at the slot
Figure 10.2: The server streams from the greater of the two positions

Debezium has an offset.mismatch.strategy option, in technology preview, that can compare the two positions; its default, no_validation, trusts the stored offset and does not notice that a slot was recreated.

A reset has a different shape. With snapshot.mode=initial the connector reads the table’s current rows as snapshot (r) events, not the history of how they got there, and then streams from the new slot position. A SKU updated a hundred times arrives once, with its latest quantity. With snapshot.mode=no_data a reset takes no snapshot but still streams from the slot’s confirmed_flush_lsn: the changes made while stopped arrive, plus a repeat of the last change before the stop.

An alter is therefore the tool for “skip this bad batch”, and a reset is the tool for “rebuild the downstream copy from the table as it is now”. Neither can rewind.

When the slot is lost

While a connector is stopped, nothing advances its slot, and PostgreSQL keeps every WAL file from the slot’s restart_lsn onward. That includes WAL written by tables the connector does not capture: a bulk load into an unrelated table is retained too. The cost is disk, and pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn) measures it. When the connector resumes it reads the backlog, acknowledges it, and the retained bytes fall.

Setting max_slot_wal_keep_size caps the retention. A slot that needs more WAL than the cap allows is invalidated: its wal_status becomes lost, and the position it recorded no longer exists on the server. Two things then look alike and differ in what Connect reports:

  • The slot is lost. The connector stays RUNNING with an empty task list and logs “Cannot obtain valid replication slot” on a retry loop that lasts about 30 minutes. Nothing reports FAILED, so an alert on failed tasks stays silent while nothing is delivered. slot.max.retries and slot.retry.delay.ms do not shorten the loop.
  • The slot was dropped and the offsets kept. The task goes FAILED with a trace that says the position is no longer available on the server and asks for a snapshot mode. POST /connectors/{name}/restart?includeTasks=true&onlyFailed=true restarts the task into the same failure.
A lost slot leaves the connector running with no task; a dropped slot with offsets kept fails the task; both are recovered by stopping, dropping the slot, deleting the offsets and resuming
Figure 10.3: A slot the server cannot use is replaced, not repaired

Neither is fixed by a restart, because the stored position is what is wrong. The recovery is the same for both: stop the connector, wait until the slot is inactive, drop it with pg_drop_replication_slot, delete the offsets, and resume. The connector creates a new slot and takes a snapshot. Dropping a slot that is still active fails, and the slot stays active for a moment after STOPPED is reported, so the drop waits for active = f. Anything written in the gap between the old position and the snapshot is not replayed; the snapshot is the current state.

How Quarkus and Camel fit in

The connector is configuration, not code. It lives in connect/inventory-cdc.json and is registered with PUT /connectors/inventory-cdc/config; the Connect API is plain HTTP, and curl is what an operator uses. The application’s part is to consume the topic, and to stay correct when offsets change.

ChangeLogRoute reads edp.inventory.item-changes with the Camel Kafka component and hands each Debezium envelope to a bean:

from("kafka:" + Topics.INVENTORY_ITEM_CHANGES + "?groupId=inventory-change-log")
        .routeId("change-log")
        .unmarshal().json(ChangeEnvelope.class)
        .bean("itemChangeRecorder", "record");

ChangeEnvelope keeps the operation, the row after the change and source.lsn. The recorder persists an ItemChange Panache entity: op, sku, qty, lsn and received_at. The table records what the connector delivered, so a replay and a skip can be told apart. The lsn column makes a duplicate visible as the same position twice.

Altering and resetting offsets change what the consumer sees: a skip is a gap, a reset is a second snapshot of the same rows. A consumer that cannot tolerate either needs an Idempotent Receiver.

Build, run, observe

(cd examples/domain-model && mvn -B -q install)
cd examples/10-managing-connector-offsets
mvn -B verify
./verify.sh

verify.sh registers the connector, seeds five SKUs and tries each way to change its offsets. Watch what each alteration delivers afterwards: skipped changes, one repeated change, snapshot rows without history. It prints 40/40 checks passed.

Try one change and rerun:

  • Alter the offset with the connector running: the answer is HTTP 400 and nothing changes.
  • Send the incremental snapshot signal instead of resetting: the connector keeps running and the five rows arrive again as r events.
  • OFFSET_FLUSH_INTERVAL_MS=60000 ./verify.sh: a GET lags the connector, no offset is stored soon after the snapshot and the run stops at its precondition.

When not to touch offsets

Do not alter an offset to repair a consumer. The change goes into the connector for every reader of the topic, and a skipped batch is a gap in every one of them. A consumer that needs to reprocess reads the topic again from an earlier position with its own group.

Do not reset to rewind. A reset rebuilds from the table as it is, and it discards the connector’s history of the table’s changes. A downstream system that expects every intermediate update sees only the latest state.

Do not leave a stopped connector stopped. The retained WAL grows until the disk fills or a cap invalidates the slot, and the cap turns a pause into a snapshot. Set max_slot_wal_keep_size knowing that its price is a lost slot, and alert on wal_status and the retained bytes rather than on failed tasks.

Common pitfalls

  • Altering an offset to fix one consumer affects every reader of the topic, turning a private fix into a shared gap.
  • Resetting a connector to rewind instead discards its history of intermediate changes and replaces it with a fresh snapshot of current rows.
  • Reading an offset right after a flush interval has not yet elapsed returns a stale position, since Connect writes the offsets topic only on its own timer.
  • Leaving a stopped connector stopped lets retained WAL grow until the disk fills or a size cap invalidates the slot, turning a pause into an unplanned snapshot.

🤖 Explore with your agent “In examples/10-managing-connector-offsets, stop the inventory-cdc connector, update SKU-1 ten times, alter its offset to the current WAL position, resume and read inventory.item_change written by ChangeLogRoute and ItemChange. Then delete the offsets and resume, and compare the rows. State when an alter is the right tool and when a reset is.” Check: the ten updates are missing after the alter, and the reset delivers exactly five r rows with the latest quantities and no u rows.

References