Kafka Consumer
Console Script
Message Retrieval
Data Streaming
Consumer API

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

bash
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my-topic --from-beginning

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:

  1. Find the last offset: determine the latest offset in each of the topic's partitions. kafka-get-offsets.sh returns it in topic:partition:offset form.
  2. Configure the consumer: use --offset to start reading from the last offset (or latest). 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.
  3. Execute and exit: run the consumer, print the last message and exit. Depending on your environment, this may need additional scripting.

Example script

bash
1# Fetch the latest offset of partition 0 (output is topic:partition:offset)
2LATEST_OFFSET=$(kafka-get-offsets.sh --bootstrap-server localhost:9092 --topic my-topic --partitions 0 --time -1 | cut -d: -f3)
3
4# Subtract 1 because offset starts at 0
5LATEST_OFFSET=$(($LATEST_OFFSET - 1))
6
7# Run consumer to fetch last message
8kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my-topic --offset $LATEST_OFFSET --partition 0 --max-messages 1

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

TopicDetailConsideration Required
Consumer ConfigurationUse --offset and --partition to specify which message to readYes, adjust for partitions and correct calculation of latest offset
ScriptingCustom script might be necessary to fetch the last message reliablyYes, to handle various deployment environments and setups
EfficiencyFetching the last message can be resource-intensive if not handled properlyYes, consider caching or state maintenance strategies

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design