Kafka console consumer How to get only the last N messages from a topic instead of everything from the beginning?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka is a powerful streaming platform capable of handling real-time data feeds. Kafka console consumer is a command-line tool that comes with Kafka and can be used to read data from Kafka topics. It’s particularly useful for debugging or for operational insights into data as Kafka processes it.
One common requirement is to fetch only the last N messages from a topic rather than streaming all messages from the beginning of the topic's log. This capability is especially useful in scenarios where one needs to quickly check the most recent data, such as monitoring recent transactions or system alerts.
Fetching Only the Last N Messages
To retrieve only the last N messages, you can use the --offset option of the Kafka console consumer in combination with the total number of messages in the topic. Here are the detailed steps and considerations:
- Determine the Total Number of Messages: First, you need to know how many messages are in your topic. This can be done using the
kafka-run-classutility to executekafka.tools.GetOffsetShell. For example:
This will return the log size for each partition of the topic, which tells you how many messages are in those partitions.
- Calculate the Offset: If you want the last N messages and you know the total messages count M in the topic, then you can start consuming from offset
M-N. If the topic has more than one partition, you'll need to calculate this for each partition or consume from a partition that has enough messages to satisfy your requirement. - Start Kafka Consumer with Offset: Use the kafka-console-consumer.sh script with the
--offsetoption. For instance:
- Limiting Returned Messages: To ensure that the consumer stops after reading the last N messages, you can use the
--max-messagesoption:
Considerations and Tips
- Broker and Client Version: Ensure that the Kafka broker and the client are compatible and you are using the correct versions that support these features.
- Topic Partitions: If your topic has multiple partitions, you need to calculate offsets for each partition or choose the appropriate partition.
- Ordering: Kafka only guarantees ordering within a partition, not across different partitions.
- Real-time Monitoring: For real-time monitoring, consider using Kafka's Consumer Groups, which can manage offsets automatically.
Summary Table
| Feature | Description | Useful Commands & Options |
| Offset Calculation | Calculate where to start consuming messages. | kafka.tools.GetOffsetShell |
| Consume with Offset | Start consuming from a specific offset. | --offset |
| Limit Messages | Control the number of messages to consume. | --max-messages |
| Partition Consideration | Manage offsets when multiple partitions exist. | Manually calculate offset per partition. |
Additional Tools and Options
For advanced monitoring and management of Kafka data, beyond the kafka-console-consumer.sh utilities, consider using Kafka's comprehensive set of APIs. These include the Admin API for managing and inspecting topics, the Producer and Consumer APIs for advanced data handling, and Streams API for real-time data processing tasks.
Moreover, tools such as Kafka Manager or Confluent Control Center provide a user-friendly interface for managing Kafka clusters, monitoring data flows, and much more, which can enhance operational capabilities and ease the maintenance burdens associated with large Kafka deployments.
In summary, selectively consuming the last N messages from a Kafka topic involves calculating the correct offset based on the total messages in the topic. Utilizing the kafka-console-consumer.sh with appropriate option flags provides a straightforward mechanism to achieve this, though tuning might be necessary depending on the topic's partitioning and message distribution.

