How to close kafka consumer once all messages are consumed?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a popular distributed messaging system that excels at handling large volumes of real-time data efficiently. Kafka consumers read records from Kafka topics, but knowing when to shut down a consumer, particularly after all messages are consumed, can be challenging due to Kafka's nature of continuous data streaming. Here, we detail a methodology to gracefully close a Kafka consumer when it has consumed all currently available messages in a topic.
Understanding Kafka's Consumption Model
Kafka stores records in topics that can be split across multiple partitions, which can be read by multiple consumers concurrently. Each consumer maintains its offset, which refers to its position in the log of messages. Typically, Kafka consumers run in an endless loop, continuously polling for new messages. This design makes it inherently difficult to identify when all messages are "consumed" because new messages can be produced to the topic at any time.
Strategies for Closing a Kafka Consumer
- Time-Based Approach: Use a timeout to conclude no more messages are available at the moment. This is not foolproof as this might terminate the consumer while messages are still being published.
- Message Count Approach: If the expected number of messages or a termination message is known, the consumer can shut down after consuming the desired count or recognizing a specific message indicating the end.
- External Control Mechanism: Utilizing an external flag controlled by another part of the application (or manual intervention) to signal when the consumer should close.
- Partition EOF (End of File) Checking: Kafka 0.10.1.0 introduced a method to check if a consumer has reached the end of the log. This helps in determining if all available messages at the time of the check have been consumed.
Programming a Consumer to Close
Here's a sample implementation using the Partition EOF method in Java. This approach assumes that your application can tolerate closing the consumer when it reaches the end of all partitions it is reading from, and there are no new records being produced for a small window of time.
Points of Consideration
When implementing a Kafka consumer shutdown logic, consider the following checklist to ensure data consistency and application stability:
| Consideration | Description |
| Consumer Group Stability | Ensure that closing a consumer does not destabilize consumer groups or lead to rebalancing issues. |
| Data Loss | Ensure no data loss by validating that all messages are processed before shutdown. |
| At-least-once Processing | Consider the delivery semantics (at-least-once, exactly-once) to handle message processing. |
| Handling Rebalances | Gracefully handle consumer rebalances which may occur on consumer group changes. |
In conclusion, while Kafka does not natively support an easy way to determine if all messages have been consumed from a topic, combining some of the above strategies like polling for no more records and checking partitions against latest offsets can ensure a consumer can be closed down gracefully without loss of messages.
Related reading
- how to compress data in producers when using spring kafka
- How to configure Celery to run as systemd service with a Django application served by Gunicorn?
- How to configure Confluent Platform Kafka connect logs?
- How to configure docker-compose.yml for Kafka local development?
- How to configure kafka consumer with sasl mechanism PLAIN and with security protocol SASL_SSL in java?
- How to configure Kafka to behave like a FiFo queue?
- How to configure kafka topic retention policy during creation with Spring?
- How to configure logging for Kafka producers?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.