Terminate Kafka Console Consumer when all the messages have been read
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 streaming platform used for building real-time streaming data pipelines and applications. Kafka's architecture is set up to handle vast amounts of data efficiently, which makes it an ideal choice for enterprises dealing with high volumes of event data. Kafka, however, fundamentally does not natively support terminating a consumer when all messages are consumed because it is designed to continually listen for new messages.
Understanding Kafka Consumers
A Kafka consumer pulls data from Kafka topics to which it has subscribed. Since Kafka is built for constant real-time data consumption, the Kafka console consumer (kafka-console-consumer) tool, which is part of Apache Kafka's command line tools, is designed to run indefinitely, processing records as they arrive.
Use Case for Terminating the Console Consumer
Sometimes, for testing or batch jobs, you might want to terminate the Kafka console consumer after all the current messages in a topic have been read. Since this is not an out-of-the-box feature, we'll need to implement a workaround or use specific configurations/tools.
Workarounds to Terminate Kafka Console Consumer
- Using
timeoutcommand (Linux or MacOS): A simple Linux workaround is to use thetimeoututility, which terminates a command after a specified period of inactivity or total elapsed time. This is obviously far from perfect, as it doesn't guarantee that all messages are read or that no new messages will arrive during the period.Example:
This command terminates the consumer after 30 seconds regardless of the state of message consumption.
- Using Kafka Consumer Groups and Checking Lags: More sophisticated and reliable way is to track the consumer lag, which indicates how many messages are left unread by a consumer in a partition. When the lag reaches zero for all partitions the consumer is tracking, the process can be programmed to end.Here's a high-level approach:
- Run the consumer within a consumer group.
- Periodically check the consumer lag.
- If the lag for all partitions is zero, programmatically terminate the consumer. Example using Kafka's
kafka-consumer-groups.shtool:
You would need a wrapper script or service to parse this command’s output and terminate the consumer process when appropriate.
- Write a custom consumer using Kafka API: You can create a custom Kafka consumer using a programming language that supports Kafka client libraries (e.g., Java, Python). In the consumer code, you can manage the consumption state and terminate the consumer when all messages have been processed.Here’s a simple pseudocode:
Summary Table
| Method | Reliability | Complexity | Use Case |
timeout command | Low | Low | Simple, time-constrained tests |
| Consumer groups with lag checks | Medium to High | Medium | Ensure complete consumption in a controlled manner |
| Custom consumer application | High | High | Flexible, reliable for production use |
Additional Considerations
- Handling rebalances and consumer failures: If you're managing consumers across many instances or handling critical data, consider implementing error handling and recovery mechanisms to deal with potential issues such as consumer rebalances, broker failures, etc.
- Performance optimization: When designing a Kafka consumer, especially a custom one, it's essential to optimize performance by tuning various consumer configurations such as
fetch.min.bytes,fetch.max.wait.ms, andenable.auto.commit.
Terminating a Kafka console consumer after all messages have been read can be challenging due to the continuous nature of Kafka's design. However, by leveraging the appropriate strategies and tools, you can effectively manage and terminate Kafka consumers according to your specific needs.
Related reading
- Test Kafka Streams topology
- Testing a @KafkaListener using Spring Embedded Kafka
- testing kafka consumer and producer failed on connection
- Testing Kafka HA and getting, NetworkException The server disconnected before a response was received
- Testing RabbitMQ with Spring and Mockito
- Testing window aggregation with Kafka Streams
- The benefits of Flink Kafka Stream over Spark Kafka Stream? And Kafka Stream over Flink?
- The correct way for creation of KafkaTemplate in spring boot

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.