Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

A Spring Kafka listener stays silent after the Spring Boot app restarts

Your error

@KafkaListener does not receive the messages that were sent while the application was stopped1 more line it can show

Fix it fast

Most common cause, 1 of 3

No committed offset, and auto.offset.reset is latest

Use one fixed group id per service and set the reset policy to earliest.

Fix
# application.properties
spring.kafka.consumer.group-id=orders-service
spring.kafka.consumer.auto-offset-reset=earliest
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

When a @KafkaListener gets nothing after a restart, its consumer group either has no committed offset, so the default auto.offset.reset=latest skips everything sent while the app was down, or the killed old instance keeps its partitions until session.timeout.ms (45 seconds) runs out. Use a fixed group id, set auto-offset-reset to earliest, and let java shut down cleanly.

  • Applies to Spring Boot 4.x, Spring for Apache Kafka 4.x, Apache Kafka and kafka-clients 4.x with the classic group protocol, Docker Compose v2 and newer
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

List the consumer groups, then describe the one your service should use. A missing or unexpected group name, missing offsets or a lingering old member each point to a different cause.

Run this first
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group orders-service

Then find the line you see in this list and open its fix:

  1. 1No committed offset, and auto.offset.reset is latestA new group name appears in --list after every start, or the group has no committed offsets for the topic
  2. 2The listener id silently replaces the configured group id--list shows a group named after the id attribute of @KafkaListener rather than after spring.kafka.consumer.group-id
  3. 3The killed instance still owns the partitions for 45 secondsRecords do arrive, but only about 45 seconds after the restart, and --members still lists the previous consumer right after start

What does the error look like?

  • @KafkaListener does not receive the messages that were sent while the application was stopped
  • After a restart the listener only starts consuming about 45 seconds later

Cause 1 of 3

No committed offset, and auto.offset.reset is latest

You seeA new group name appears in --list after every start, or the group has no committed offsets for the topic

Check

A listener that never sees the records produced during its downtime has usually started without a committed offset. Kafka then applies auto.offset.reset, whose default is latest, and jumps to the end of each partition, so the backlog is skipped for good. A group id built from ${random.uuid}, or a group that never committed before it stopped, ends up here.

Check
grep -rn -E 'group-id|groupId|auto-offset-reset' src/main/resources/ src/main/java/

Fix

Use one fixed group id per service and set the reset policy to earliest. The policy only applies where no committed offset exists, so a group that already has offsets keeps its position and replays nothing.

Fix
# application.properties
spring.kafka.consumer.group-id=orders-service
spring.kafka.consumer.auto-offset-reset=earliest

Verify

Stop the app, produce a few records, start it again. The listener handles them and LAG drops back to 0.

Verify
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group orders-service

Confirmed by kafka.apache.org

Did this fix it?

Cause 2 of 3

The listener id silently replaces the configured group id

You see--list shows a group named after the id attribute of @KafkaListener rather than after spring.kafka.consumer.group-id

Check

Spring Kafka uses the id attribute of @KafkaListener as the consumer group.id and overrides the factory setting with it. Renaming that id moves the listener to a fresh group without offsets. Two services that share an id land in one group and split its partitions, and since each partition goes to exactly one member, one of them may get nothing at all.

Check
grep -rn '@KafkaListener' src/main/java/

Fix

Name the group on the annotation, or set idIsGroup = false so the configured group-id applies again.

Fix
@KafkaListener(id = "ordersListener", groupId = "orders-service", topics = "orders")
public void onOrder(String payload) { /* ... */ }

// or keep spring.kafka.consumer.group-id in charge:
// @KafkaListener(id = "ordersListener", idIsGroup = false, topics = "orders")

Verify

The group's member list contains only this service's consumers.

Verify
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group orders-service --members

Confirmed by docs.spring.io

Did this fix it?

Cause 3 of 3

The killed instance still owns the partitions for 45 seconds

You seeRecords do arrive, but only about 45 seconds after the restart, and --members still lists the previous consumer right after start

Check

A consumer that vanishes without leaving its group is declared dead only after session.timeout.ms, 45 seconds by default under the classic group protocol; its partitions are reassigned after that. In Docker this is what a shell-form ENTRYPOINT causes: java is not PID 1, never receives the SIGTERM from docker stop, and is killed after the 10-second grace period without closing its consumers.

Check
grep -n "ENTRYPOINT\|CMD" Dockerfile
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group orders-service --members

Fix

Use the exec form, so java receives SIGTERM. Spring Boot's JVM shutdown hook then closes the application context, and closing a Kafka consumer runs its default leave-group step, so the new instance gets the partitions straight away.

Fix
# instead of: ENTRYPOINT java -jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]

Verify

After docker compose restart, --members shows only the new consumer, and records flow within seconds.

Verify
docker compose restart app
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group orders-service --members

Confirmed by kafka.apache.org

Did this fix it?

How do I stop it coming back?

  • Fix one group id per service in configuration or in groupId on the annotation, and review any change to a @KafkaListener id as a change of consumer group.
  • Set spring.kafka.consumer.auto-offset-reset=earliest for services that must not lose records produced during deploys.
  • Keep ENTRYPOINT in exec form in every Kafka consumer image, so a stop or redeploy closes consumers instead of killing them.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Dispatch Pro, TaxiDex.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.