Search RU
Tasks / Kafka / Consumer retries and dead-letter
Statement medium

Consumer retries and dead-letter

`process_batch(messages, handler, max_retries=2)` consumes a batch of messages.

`Message` is a dataclass with `offset` and `value`. `handler(message)` can:

- succeed;
- raise `KafkaError` — a transient error, the message can be retried;
- raise `ValueError` — a poison pill, retrying is pointless.

Rules:

- a successful message appears in `acked` exactly once;
- `KafkaError` is retried, at most `max_retries + 1` attempts in total;
- when attempts run out, the message goes to `dead`;
- `ValueError` goes to `dead` immediately, with no retries;
- the order of messages in `acked` and `dead` is preserved;
- an empty batch gives two empty lists.

Write tests with a handler function: success, a transient error with a retry,
exhausted attempts, a poison pill, `max_retries=0` and an empty batch.

kafkaconsumerretrydlq
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

Which error type means "retry", and which means "this message cannot be saved"?

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