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 showFix it fast
Most common cause, 1 of 3No committed offset, and auto.offset.reset is latest
Use one fixed group id per service and set the reset policy to earliest.
# application.properties
spring.kafka.consumer.group-id=orders-service
spring.kafka.consumer.auto-offset-reset=earliestShort 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.
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.
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group orders-serviceThen find the line you see in this list and open its fix:
- 1No committed offset, and auto.offset.reset is latest
A new group name appears in --list after every start, or the group has no committed offsets for the topic - 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 - 3The killed instance still owns the partitions for 45 seconds
Records 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 stoppedAfter 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.
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.
# application.properties
spring.kafka.consumer.group-id=orders-service
spring.kafka.consumer.auto-offset-reset=earliestVerify
Stop the app, produce a few records, start it again. The listener handles them and LAG drops back to 0.
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group orders-serviceConfirmed by kafka.apache.org
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.
grep -rn '@KafkaListener' src/main/java/Fix
Name the group on the annotation, or set idIsGroup = false so the configured group-id applies again.
@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.
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group orders-service --membersConfirmed by docs.spring.io
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.
grep -n "ENTRYPOINT\|CMD" Dockerfile
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group orders-service --membersFix
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.
# 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.
docker compose restart app
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group orders-service --membersConfirmed by kafka.apache.org
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
- Apache Kafka 4.4: consumer configs (auto.offset.reset, session.timeout.ms)
- Apache Kafka 4.4: KafkaConsumer javadoc (groups, failure detection, close)
- Apache Kafka 4.4: managing consumer groups
- Spring for Apache Kafka: @KafkaListener annotation
- Dockerfile reference (ENTRYPOINT exec and shell form)
- docker container stop (SIGTERM, grace period)
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.