Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A Spring Kafka consumer processes each record by calling a slow external API, and the logs show repeated rebalances and "CommitFailedException" or messages about the consumer leaving the group. What is the cause and how do you fix it?

Try it yourself

Consumer group: who reads which partition

Change the number of partitions and consumers, and see who owns what, who is idle, and where ordering holds.

Read this example as text

Who reads which partition

  1. 3 partitions, 2 consumers. C0 reads P0, P1; C1 reads P2.
  2. 4 partitions, 2 consumers. C0 reads P0, P1; C1 reads P2, P3.
  3. 3 partitions, 3 consumers. C0 reads P0; C1 reads P1; C2 reads P2.
  4. 6 partitions, 4 consumers. C0 reads P0, P1; C1 reads P2, P3; C2 reads P4; C3 reads P5.
  5. 2 partitions, 4 consumers. C0 reads P0; C1 reads P1; C2 is idle; C3 is idle.

A partition has exactly one reader in the group. A consumer beyond the number of partitions is idle.

Six records with 3 partitions and 2 consumers

  1. Record A1. Key A goes to P0, read by C0.
  2. Record B1. Key B goes to P1, read by C0.
  3. Record A2. Key A goes to P0, read by C0.
  4. Record C1. Key C goes to P2, read by C1.
  5. Record B2. Key B goes to P1, read by C0.
  6. Record A3. Key A goes to P0, read by C0.

A1, A2 and A3 sit in one partition and are read in that order. Nothing orders a record in one partition against a record in another.

What this example leaves out
  • One topic, and Kafka's range assignor, the first in the default list. Other assignors spread partitions differently.
  • A, B and C stand for key hashes 0, 1 and 2. The real client hashes the key bytes; the rule shown, same key to the same partition, is the same.
  • A rebalance is shown as instant. In a real group, consumers stop briefly while partitions move.

The consumer is taking longer than max.poll.interval.ms, 5 minutes by default, between two calls to poll(), because one poll can return up to max.poll.records, 500 by default, and each record waits seconds on the slow API. The broker then decides the consumer has failed, removes it from the group and gives its partitions to another member; when the slow consumer finally commits, it no longer owns those partitions, so the commit fails and the records are processed again. We fix it by lowering max.poll.records, raising max.poll.interval.ms, making processing faster or concurrent, or carefully handing work off, and we track rebalances and time between polls as metrics.

Why this question is asked

This is one of the most common Kafka problems in production, and the error message points at commits while the real cause is time. It also produces a nasty side effect, duplicate processing, which can mean duplicate emails or payments. Interviewers ask it because fixing it well needs an understanding of how the consumer proves it is alive. In this answer, we will recap the two liveness checks, do the arithmetic that causes the failure, follow the sequence of events, and go through the fixes and the metrics that catch it early.

1A Quick Recap: How a Consumer Proves It Is Alive

A Kafka consumer has two separate liveness checks. The first is the heartbeat: a background thread sends heartbeats to the group coordinator, and if none arrive for session.timeout.ms (45 seconds by default in recent clients), the consumer is considered dead. This catches crashed processes and network failures.

The second is the poll interval. The application must call poll() at least once every max.poll.interval.ms, 300,000 ms or 5 minutes by default. Heartbeats come from a separate thread, so a consumer stuck in processing would keep heartbeating for ever; the poll interval catches exactly that case, a consumer that is alive but not making progress. In Spring Kafka, the listener container calls poll() for us, hands the records to our @KafkaListener method one by one, and only polls again when the whole batch is done.

2The Arithmetic That Breaks It

One poll returns up to max.poll.records records, 500 by default. Suppose the external API takes 2 seconds per call. The container must process all 500 records before it can poll again: 500 x 2 seconds = 1,000 seconds, more than three times the 300-second limit. Even at 1 second per call it would take 500 seconds. While the topic is nearly empty, polls return a few records and everything looks fine, which is why the problem appears only under load or when the API slows down.

Java
@KafkaListener(topics = "orders", groupId = "fulfilment")
public void handle(OrderEvent e) {
shippingApi.createShipment(e); // about 2 s per call
}
// one poll: 500 records x 2 s = 1,000 s > max.poll.interval.ms (300 s)
// log: consumer poll timeout has expired ... leaving group
// later: CommitFailedException: ... the group has already rebalanced and assigned the partitions to another member

The exact log text varies by client version, so check your logs for the poll timeout message and the CommitFailedException together.

3What Happens Step by Step

After 300 seconds without a poll, the consumer's own client notices the interval has expired and sends a leave-group request; the coordinator starts a rebalance and gives its partitions to another member. That member starts from the last committed offset, which is still the offset before this batch, so it begins processing the same 500 records. Meanwhile, the original consumer is still working through the batch, calling the API for records that are now also being processed elsewhere.

When the original consumer finishes and tries to commit, the coordinator refuses, because the consumer is no longer a member that owns those partitions: this is the CommitFailedException. It then rejoins the group, causing another rebalance, and the cycle can repeat. Every record in the batch has been sent to the shipping API at least twice.

Step throughHow a slow batch causes duplicates
Consumer ACoordinatorConsumer BShipping API
poll: 500 records
createShipment, 2 s each
300 s pass without poll()
leave group (poll interval expired)
rebalance: B now owns A's partitions
same 500 records again
commit offsets
CommitFailedException

8 messages in order. Play them, or step through them one at a time.

The same records reach the API from both consumers.

4Fixing It

The simplest fix is to make each batch smaller. With max.poll.records set to 20, one poll takes 20 x 2 = 40 seconds, comfortably inside 5 minutes even if the API slows down threefold. This costs a little throughput because polls are more frequent, but for slow processing that cost is negligible. We can also raise max.poll.interval.ms, for example to 10 minutes, but on its own this only moves the limit, and it also means a truly stuck consumer is detected later.

The better long-term fix is to make the processing faster: set a short timeout on the API call, so one hung request cannot block a batch for minutes, and process records concurrently. Within one partition, we must keep order if it matters, but we can call the API in parallel for records of different keys within the batch and wait for all calls before returning, so the container polls only after the batch is done.

YAML
spring:
kafka:
consumer:
max-poll-records: 20 # 20 x 2 s = 40 s per poll
properties:
max.poll.interval.ms: 600000 # 10 minutes of safety margin

Handing work off to another thread pool and returning from the listener immediately looks attractive, but it breaks offset management: the container would commit offsets for records that have not been processed yet, and a crash would lose them. If we do hand off, we must commit only after the work completes, for example with manual acknowledgement and Spring Kafka's support for out-of-order acknowledgements (check your version), or by pausing the partition with the container's pause and resume methods while work is in flight. Whatever we choose, the processing must be idempotent, for example by sending a unique request id to the shipping API, because some redelivery is always possible.

5Metrics That Catch It Early

The Kafka consumer exposes metrics that show this problem before customers do. time-between-poll-max and time-between-poll-avg show how close we are to the limit; we alert when the maximum passes, say, half of max.poll.interval.ms. last-poll-seconds-ago shows a consumer that has stopped polling. Rebalance metrics such as rebalance-rate-per-hour and failed-rebalance-total in the coordinator metrics group show churn. With Spring Boot and Micrometer, these client metrics are exported automatically.

Together with the latency of the external API and consumer lag, these metrics turn a mysterious 'repeated rebalances' log into a clear signal that processing time per poll is too long.

6Follow-up questions

Would increasing session.timeout.ms help?

No. The session timeout covers heartbeats, which come from a background thread and keep working while the listener is busy. The failure here is the poll interval, which is a separate setting. Raising the session timeout would only delay detection of genuinely dead consumers.

Does the batch listener mode change anything?

A batch listener receives the whole poll as a list, but the rule is the same: the next poll happens only after the method returns. The time to process the whole list must stay below max.poll.interval.ms, so max.poll.records still limits the risk.

7Conclusion

In this answer, we saw that a slow external call per record can make the time between polls exceed max.poll.interval.ms, five minutes by default, because one poll returns up to 500 records. The consumer leaves the group, its partitions move to another member that reprocesses the uncommitted records, and its own commit fails with CommitFailedException. We fix it with smaller batches, a larger interval, faster or concurrent processing with timeouts, or careful hand-off that commits only after completion, and we watch time between polls and rebalance counts.

What to say in the interviewAbout 90 seconds aloud

Open with

The cause is time between polls, not commits. Spring's container polls, gets up to max.poll.records, which defaults to 500, and calls my listener for each record before it polls again.

Then, if they want more

If each record calls an API that takes about two seconds, one batch takes around 1,000 seconds, far beyond max.poll.interval.ms, which defaults to five minutes. Heartbeats still flow from the background thread, but the poll interval check says the consumer is not making progress, so it leaves the group and the coordinator gives its partitions to another consumer.

That consumer starts from the last committed offset and processes the same records again. When the slow consumer finally commits, it no longer owns the partitions, so it gets a CommitFailedException, rejoins and causes another rebalance. To fix it, I would first lower max.poll.records, for example to 20, so a batch takes 40 seconds, and give max.poll.interval.ms some extra margin.

Then I would make processing faster with a short timeout on the API call and, if order allows, parallel calls within the batch that finish before the listener returns. If I hand work off to another pool, I must only commit after it completes, using manual acknowledgements or pausing the partition. Processing must be idempotent anyway. And I would alert on time-between-poll-max and the rebalance rate.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A Spring Kafka consumer processes each record by calling a slow external API, and the...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/75a5c760e7/