How to gracefully shut down a Spring Kafka consumer application
Codemia Knowledge Hub ·
A graceful shutdown means the consumer stops fetching new records and finishes processing the ones it already has, so no messages are lost. In Spring Kafka you get there by controlling listener containers through KafkaListenerEndpointRegistry, tying them into the context lifecycle with SmartLifecycle, and closing the application context when a termination signal arrives.
How the consumer loop affects shutdown
Kafka consumers poll the broker in a loop to fetch records. Timing is governed by properties such as max.poll.interval.ms and session.timeout.ms in ConsumerConfig. After a record is processed, the consumer commits its offset to tell Kafka the message is done. A shutdown that interrupts processing before the commit is what causes reprocessing or loss.
Configuring the consumer for a clean stop
- Set
max.poll.interval.mshigh enough: this is the maximum delay betweenpoll()calls before the consumer is considered dead. Make it large enough for your expected processing time. - Implement
ConsumerAwareListenerErrorHandler: handle exceptions during consumption so errors do not break the shutdown path. - Use
KafkaListenerEndpointRegistry: Spring provides it to manage listener lifecycles, including pausing and resuming:
- Implement
SmartLifecycle: this gives you control over the order in which Kafka listeners start and stop with the application context:
Triggering the shutdown
- Close the application context: calling
ApplicationContext.close()starts the shutdown and invokes thestopmethods of allSmartLifecyclebeans. - Handle termination signals: catch signals such as SIGTERM on UNIX and shut down cleanly. Spring Boot's
ShutdownEndpointor the@PreDestroyannotation can hold custom cleanup logic.
Quick reference
| Key Aspect | Description |
max.poll.interval.ms | Adjust to allow ample time for message processing before rebalancing |
| Error Handling | Implementing ConsumerAwareListenerErrorHandler for proper error management |
| KafkaListenerEndpointRegistry | Utilize for pausing and resuming consumers as part of the lifecycle |
| SmartLifecycle | Implement for precise control over Kafka consumers startup and shutdown processes |
| Signal Handling | Properly handle shutdown signals for graceful cleanup |
Logging and transactions
- Logging and monitoring: log around message processing and the shutdown sequence so you can confirm everything completed.
- Kafka transactions: use them to make read-process-write sequences exact.
Related reading
- How to Guarantee Message delivery with Celery?
- How to guarantee order in Kafka partition
- How to handle backpressure in a Kafka Connect Sink?
- How to handle connection issues with kafka using the python kafka library?
- How to handle asynchronous callbacks in a synchronous way in Java?
- How to handle database migrations in Spring Boot with Hibernate?
- How to handle error and don't commit when use Kafka Streams DSL
- How to handle kafka publishing failure in robust way
System design interview problems on this topic
- Design a Distributed Messaging System: solution and walkthrough
- Design a Log Collection and Analysis System: solution and walkthrough
- Design an Ad Click Aggregation System: solution and walkthrough
- Design a Webhook Notification Service: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.