Get the last message from a Kafka topic with kafka-console-consumer
Codemia Knowledge Hub ·
Kafka has no direct way to fetch the last message in a topic, so you get it by working with offsets: look up the latest offset for a partition, subtract 1, and point kafka-console-consumer.sh at that offset with --partition and --offset, reading a single message. This is handy for getting the most recent state or final update from a stream of events.
How offsets make this possible
Kafka stores messages in topics split into partitions for scalability and parallelism. Each message in a partition has a unique, immutable identifier called an offset. Consumers track offsets to know which messages they have consumed and which to read next. Kafka does not support querying messages by attributes like timestamp or content, so offsets and partitions are the levers you have.
Basic console consumer command
This connects to Kafka on localhost at the default port 9092, subscribes to my-topic and reads messages from the beginning.
Reading only the last message
The idea is to control where the consumer starts reading:
- Find the last offset: determine the latest offset in each of the topic's partitions.
kafka-get-offsets.shreturns it intopic:partition:offsetform. - Configure the consumer: use
--offsetto start reading from the last offset (orlatest). On its own, this may leave the consumer waiting for new messages, so you may need extra scripting to exit right after printing the last message. - Execute and exit: run the consumer, print the last message and exit. Depending on your environment, this may need additional scripting.
Example script
The script computes the last offset and subtracts 1 so that it reads the latest message. Adjust the partition number to match your topic's partition setup.
Partitions, performance and error handling
- Performance: repeatedly querying for the last message is not efficient. If you often need the latest message, consider keeping that state in the application instead.
- Multiple partitions: with several partitions, extend the script to handle all of them, or use a partitioning key so related messages land in the same partition.
- Error handling: check for errors carefully, especially around fetching offsets and consuming messages.
Quick reference
| Topic | Detail | Consideration Required |
| Consumer Configuration | Use --offset and --partition to specify which message to read | Yes, adjust for partitions and correct calculation of latest offset |
| Scripting | Custom script might be necessary to fetch the last message reliably | Yes, to handle various deployment environments and setups |
| Efficiency | Fetching the last message can be resource-intensive if not handled properly | Yes, consider caching or state maintenance strategies |
Related reading
- Get last modified date of a Kafka topic
- Get Latest Message for a Confluent Kafka Topic in Python
- Get number of messages in an Amazon SQS Queue
- Get the latest offsets in SSL Enabled Kafka via CMD
- Get list of ALL offers from Amazon Product Advertising API
- Get list of all routes defined in the Flask app
- Get topic from kafka message
- get topic from kafka message in spark
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.