Count the number of messages in a Kafka topic
Codemia Knowledge Hub ·
To count the messages in a Kafka topic, get each partition's log end offset and log start offset, subtract one from the other, and add up the results across partitions. The GetOffsetShell tool, run as kafka-get-offsets.sh, returns both offsets from the command line.
Offsets and partitions
A Kafka topic is a named feed that records are published to, and many clients can consume it. Each topic is split into partitions, and every message in a partition gets a sequential ID called its offset.
Two offsets matter for counting:
- Log end offset (LEO): the offset that the next message appended to the partition will receive, which is one past the last message currently stored.
- Log start offset: the offset of the first message still in the partition. Retention policies and log compaction delete old messages, so this value moves forward over time.
Messages in a partition = log end offset minus log start offset.
Getting the offsets with GetOffsetShell
You need the topic name and access to the Kafka environment, either through the CLI or a GUI such as Confluent Control Center.
- Log end offset (
--time -1):
- Log start offset (
--time -2):
Older Kafka releases without kafka-get-offsets.sh run the same tool as kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list <broker_address> .... For a topic with several partitions, compute the difference for each partition and sum them to get the topic total.
GUI tools and scripts
- Kafka Tool and Confluent Control Center show message counts per topic and can be quicker than the command line.
- Custom scripts, for example in Python with the
kafka-pythonpackage or another Kafka client library, can fetch offsets and calculate counts automatically.
Monitoring message counts
Tracking message counts over time helps you spot anomalies in data flow, understand consumer patterns and manage storage. Prometheus with the Kafka Exporter can scrape these counts and other metrics for monitoring and alerting.
Comparing the methods
| Method | Tool or Command | Pros | Cons |
| Command Line Interface | kafka-get-offsets.sh | Precise and scriptable | Requires CLI access and familiarity |
| GUI Tools | Kafka Tool, Confluent Center | User-friendly, additional insight | Limited by provided features |
| Custom Scripts | Python, Kafka-clients | Highly customizable, automatable | Requires programming skills, setup |
Related reading
- Counting unique values in a column in pandas dataframe like in Qlik?
- Create a custom Tensorflow histogram summary
- Create a Pandas Dataframe by appending one row at a time
- Create an int list feature to save as tfrecord in tensorflow?
- Create multiple consumers in Kafka in command line
- Create Spark DataFrame in Spark Streaming from JSON Message on Kafka
- CSV Connector For Kafka
- Data aggregation using apache flink
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 courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.