view messages
system design
kafka

How to view messages in a Kafka topic

Codemia Knowledge Hub ·

For a quick look, run kafka-console-consumer.sh with --from-beginning. Add --property flags to print keys, partitions, offsets and timestamps, use kcat to read one message at a known partition and offset, and use a programmatic consumer or a GUI when you need decoding, filtering or headers.

Console consumer

The built-in console consumer is the fastest way to see message values:

bash
1kafka-console-consumer.sh \
2  --bootstrap-server localhost:9092 \
3  --topic orders \
4  --from-beginning

This prints values from the start of the retained topic data. Without --from-beginning, the consumer only reads messages that arrive after it joins.

For day-to-day debugging, print more of each record:

bash
1kafka-console-consumer.sh \
2  --bootstrap-server localhost:9092 \
3  --topic orders \
4  --from-beginning \
5  --property print.key=true \
6  --property key.separator=" | " \
7  --property print.partition=true \
8  --property print.offset=true \
9  --property print.timestamp=true

Reading one message at a specific partition and offset

Sometimes "view the message" means "show me what is at offset 12345 in partition 2". The console consumer is awkward for that, and kcat is usually better:

bash
1kcat \
2  -b localhost:9092 \
3  -t orders \
4  -p 2 \
5  -o 12345 \
6  -c 1

This reads exactly one message from a known offset, which is handy when correlating application logs with Kafka positions.

If the topic uses Avro, Protobuf or JSON with schemas, raw bytes may not be enough. Use the serializer-aware tooling in your stack or a UI that can decode the messages.

Keys, headers and the rest of the record

A Kafka record has several parts:

  • topic
  • partition
  • offset
  • key
  • value
  • timestamp
  • headers

When debugging routing or compaction, the key matters as much as the value. When debugging retries or trace propagation, headers matter too.

A Java consumer gives you full access to every field:

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("group.id", "viewer");
4props.put("auto.offset.reset", "earliest");
5props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
6props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
7
8try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props)) {
9    consumer.subscribe(List.of("orders"));
10
11    while (true) {
12        ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1));
13        for (ConsumerRecord<String, String> r : records) {
14            System.out.printf(
15                "partition=%d offset=%d key=%s value=%s%n",
16                r.partition(), r.offset(), r.key(), r.value()
17            );
18        }
19    }
20}

Use this when you need custom filtering or deserialization logic.

GUI tools for inspecting messages

For repeated inspection, a UI is often faster than shell commands. Tools such as AKHQ, Conduktor and similar Kafka browsers let you:

  • search within messages
  • inspect headers
  • jump between partitions
  • decode structured payloads

This helps most in shared environments where several engineers need to inspect the same topic safely.

Why a topic looks empty

  • The consumer started at the end. Consuming without --from-beginning and concluding the topic is empty is the most common mistake.
  • The consumer group already moved past the data. Reusing an old group ID can make a topic look empty because the group has already advanced past the offsets you wanted.
  • Retention or compaction removed the records. You can only view retained messages. Once retention expires or compaction removes older records, they are gone from Kafka even if logs still reference their offsets.

Other mistakes when inspecting messages

  • Ignoring keys, partitions and offsets. These fields are often the only way to diagnose ordering, replay or partitioning problems.
  • Reading serialized data as if it were readable business objects. If the topic uses Avro or Protobuf, use the appropriate decoder.
  • Debugging production traffic with a tool that commits offsets unintentionally, without understanding the impact on consumer groups.

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