Search RU
Tasks / Kafka / Offset commit after processing
Statement medium

Offset commit after processing

`consume_partition(messages, handler, committer, controller)` reads one
partition and commits offsets under the at-least-once rules.

`Message` is a dataclass with `partition`, `offset` and `value`.

Rules:

- messages are processed in `offset` order;
- `handler(message)` is called exactly once per message;
- a commit happens **only after** a successful `handler`;
- the next offset is committed: `committer.commit(partition, offset + 1)`;
- if `handler` raises, that message's offset is not committed;
- on error `controller.pause(partition)` is called, processing of the partition
stops and further messages are not touched;
- a successful partition does not call `pause`.

Return a dict:

```python
{"processed": <successfully processed>,
"paused": <bool>,
"failed_offset": <offset of the failure or None>}
```

Write tests: the "handler → commit" order, an error in the middle, an error on
the first message, an empty batch and the exact committed value.

kafkaoffsetsat-least-onceconsumer
test_solution.py Python 3.14
AI mentor

The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.

Log in and ask
Hints 1 of 3 unlocked · attempts: 0
Hint 1

What happens on a consumer restart if the offset is committed before `handler`
has actually processed the message?

Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts