how to get the all messages in a topic from kafka server
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
To read all messages from a Kafka topic, the consumer must start at the beginning of every partition rather than resuming from previously committed offsets. The exact method depends on whether you are using the command-line tools for a one-time inspection or writing a consumer application that explicitly seeks to the earliest offsets.
Understand What "All Messages" Means In Kafka
Kafka stores records by topic and partition. There is no single flat list of messages behind a topic. Reading "everything" means:
- enumerate the topic partitions
- begin at the earliest offset for each partition
- consume until the current end offset
That also means retention matters. If older records have already expired due to retention policy, they are gone and cannot be read back.
Quick CLI Method
For ad hoc inspection, the console consumer is the easiest tool.
--from-beginning tells Kafka to start at the earliest available offset instead of the latest.
For large topics, this may print a lot of data, so it is often used with output redirection or a filter.
Important Consumer Group Detail
If you run a regular consumer with a group that already has committed offsets, Kafka will typically resume from those offsets instead of replaying the whole topic.
That is why a one-time full read often uses:
- a brand-new group ID
- or explicit
seekToBeginning - or a CLI command designed for replay from the start
If you reuse an existing consumer group, you may get only new messages rather than the full topic history.
Java Consumer Example
A programmatic consumer can subscribe, wait for partition assignment, and then explicitly seek to the beginning.
The explicit seekToBeginning is the important step when you want the replay behavior to be unambiguous.
Reading Until The End, Then Stopping
If you want a bounded read rather than an infinite consumer loop, compare current offsets to end offsets and stop when caught up. That is useful for export jobs or debugging tools.
The key idea is that "read all messages" usually means "read all messages currently retained," not "stay subscribed forever."
For shell-based inspection, many teams also use kcat because it is convenient for quick replay and filtering. The underlying concept is the same: start at the earliest retained offset and read until the current end.
Common Pitfalls
The most common mistake is setting auto.offset.reset=earliest and assuming that guarantees a full replay even for an existing consumer group. It usually affects only partitions with no committed offset yet.
Another mistake is forgetting that topics are partitioned. To read everything, you need to cover every partition’s earliest-to-latest offset range.
A third issue is expecting expired messages to still exist. Kafka retention can delete old records even though the topic still exists.
Summary
- To read all topic messages, start from the earliest retained offsets for every partition.
- For quick inspection, use
kafka-console-consumer --from-beginning. - In code, use a fresh group or call
seekToBeginningexplicitly. - Existing committed offsets can prevent a full replay if you reuse a consumer group.
- Kafka can only return messages still retained under the topic’s retention policy.
Related reading
- how to get the group commit offset from kafka(0.10.x)
- How to get the latest offset from the Kafka topic in Confluent kafka C# library?
- How to get topic list from kafka server in Java
- How to get topics list from Kafka using C#
- How to get windowed aggregation from kafka stream?
- How to gracefully shutdown spring-kafka consumer application
- How to Guarantee Message delivery with Celery?
- How to guarantee order in Kafka partition

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.