view messages
system design
kafka

How to view kafka message?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Viewing Kafka messages sounds simple, but the right method depends on what you are trying to inspect. A quick smoke test, a forensic lookup by offset, and a structured inspection of keys and headers are slightly different tasks.

The Simplest Option: 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 message values from the start of the retained topic data. If you omit --from-beginning, the consumer reads only new messages from the point it joins.

For day-to-day debugging, add useful output properties:

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

That gives you far more context than printing values alone.

Reading a Specific Partition or Offset

Sometimes "view the message" really means "show me what is at offset 12345 in partition 2". The console consumer is less convenient for that. A tool such as kcat is usually better:

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

That reads exactly one message starting from a known offset. It is extremely useful when correlating application logs with Kafka position metadata.

If your topic uses Avro, Protobuf, or JSON with schemas, viewing raw bytes may not be enough. In that case, use the serializer-aware tooling in your stack or a UI that knows how to decode messages.

Seeing More Than the Value

A Kafka record has several parts:

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

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

Programmatic inspection in Java gives you full access:

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}

This is the right approach when you need custom filtering or deserialization logic.

GUI Tools

For repeated inspection, a UI can be more efficient than shell commands. Tools such as AKHQ, Conduktor, or similar Kafka browsers let you:

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

That is especially useful in shared environments where multiple engineers need to inspect the same topic safely.

Practical Constraints

You can only view retained messages. If retention has expired or compaction has removed older records, the message is gone from Kafka even if old offsets are referenced in logs.

You also need to know whether your consumer group settings are interfering with what you expect to see. Reusing an old group ID can make it look like the topic is empty because the group has already advanced past the offsets you wanted.

Common Pitfalls

The most common mistake is consuming without --from-beginning and assuming the topic is empty. Often the consumer just started at the end.

Another issue is ignoring keys, partitions, and offsets. Those fields are often the only way to diagnose ordering, replay, or partitioning problems.

Teams also forget that raw console output may be serialized data, not readable business objects. If the topic uses Avro or Protobuf, use the appropriate decoder.

Finally, do not debug production traffic with a tool that commits offsets unintentionally unless you understand the impact on consumer groups.

Summary

  • Use kafka-console-consumer.sh for quick message viewing.
  • Use output properties to print keys, offsets, partitions, and timestamps.
  • Use kcat when you need precise control over partition and offset.
  • Use programmatic consumers or UIs when you need decoding, filtering, or header inspection.
  • Always consider retention, serializer format, and consumer-group position when messages seem to be missing.

Course illustration
Course illustration

All Rights Reserved.