Kafka
Data Storage
Message Counting
Topic Management
Big Data Analysis

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):
bash
     kafka-get-offsets.sh --bootstrap-server <broker_address> --topic <topic_name> --time -1
  • Log start offset (--time -2):
bash
     kafka-get-offsets.sh --bootstrap-server <broker_address> --topic <topic_name> --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-python package 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

MethodTool or CommandProsCons
Command Line Interfacekafka-get-offsets.shPrecise and scriptableRequires CLI access and familiarity
GUI ToolsKafka Tool, Confluent CenterUser-friendly, additional insightLimited by provided features
Custom ScriptsPython, Kafka-clientsHighly customizable, automatableRequires programming skills, setup

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design