One batch takes longer than max.poll.interval.ms, so Kafka thinks the consumer is dead.
What happens, step by step:
- One
poll()returns up tomax.poll.recordsrecords. The default is 500. - At 1 second each, one batch takes about 500 seconds.
max.poll.interval.msdefaults to 300000 ms (5 minutes). The batch takes longer than that.- The consumer does not call
poll()again in time, so it is considered failed and leaves the group. - A rebalance moves its partitions to another member. That member starts from the last committed offset, so it processes the same records again.
- The original consumer's later commit is rejected. Then the loop repeats.
Fixes:
- Lower
max.poll.records, so a batch finishes well within the interval. - Raise
max.poll.interval.msif processing really is slow. - Make the work itself faster: batch the database writes, and run I/O in parallel inside the batch.
- For rare long tasks, call
pause()on the partitions and keep callingpoll(), so the consumer stays in the group.
Also make handlers idempotent: processing the same record twice must give the same result as once. Some replay will still happen.
- 500 records at 1 s each take longer than max.poll.interval.ms (5 minutes by default).
- Kafka removes the slow consumer from the group, and this triggers a rebalance.
- The new owner restarts from the last committed offset, so records are processed again.
- Lower max.poll.records, raise max.poll.interval.ms, or process records faster.
- You still need idempotent handlers, because some replay will happen.
Read more