See size of Kafka Topics in Bytes
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a distributed streaming platform capable of handling trillions of events a day. One fundamental component of Kafka is the concept of topics, which are categories or feeds to which records are published. Monitoring the size of these topics in bytes is crucial for effective capacity planning, performance tuning, and ensuring the health of the Kafka ecosystem.
Understanding Kafka Topic Storage
Kafka stores records in topics, and topics are split into partitions for scalability and fault tolerance. Each partition is an ordered, immutable sequence of records that is continually appended to—a commit log. The records in the partitions are stored in a set of segment files on disk.
Calculating the Size of a Kafka Topic
The size of a Kafka topic in bytes is essentially the total disk space used by the topic's partitions. This includes all active and archived data (if log compaction or retention policies are used). To determine the size of a Kafka topic, you can sum up the sizes of its partition logs.
Using Kafka Bin Scripts
Kafka ships with a set of binary scripts which can be used to interact with its cluster. Among these, kafka-log-dirs.sh can be particularly useful. This script allows you to describe log directories on Kafka brokers.
To check the size of topic partitions across all brokers, run:
Replace localhost:9092 with your cluster's broker address and my-topic with your topic name.
This script outputs details like the size of each partition in every broker. By aggregating these sizes, you determine the total size of a topic.
Programmatically Using AdminClient API
For a more dynamic approach, you could use Kafka's AdminClient API. Here is an example in Java to fetch the sizes:
In this example, my-topic is the name of your Kafka topic. This will print the size of each partition belonging to the topic.
Key Metrics to Monitor
| Metric | Description | Relevance |
| Topic Size | Total size of the topic across all partitions. | Useful for monitoring the disk space utilization. |
| Partition Count | Number of partitions in the topic. | Important for load balancing and parallelism. |
| Replication Factor | Number of replicas per partition. | Critical for fault tolerance and durability. |
| Broker Storage Utilization | Percentage of storage used in each broker. | Prevents brokers from getting overwhelmed. |
Additional Considerations
- Retention Policies: Retention settings affect how long data is stored and thus the size of a topic. Ensure that your retention settings align with your storage capacity.
- Compaction: If a topic is configured to use log compaction, older data is replaced by only the latest value for each key. This can significantly affect the size calculations, especially for topics with high update frequency but few unique keys.
- Performance Impacts: Larger topic sizes can impact performance, especially during rebalances or recoveries. Monitoring and managing the size can help in maintaining optimal performance.
In summary, understanding and monitoring the size of Kafka topics is essential for maintaining a robust and efficient streaming platform. By employing tools and APIs provided by Kafka, alongside careful consideration of topic configuration and system architecture, administrators can effectively manage their data’s footprint in a Kafka cluster.
Related reading
- Seeing partition doesn't exist warnings/failures after kafka using kafka partition re-assignment tool
- Select an element from a stream with uniform distributed probability
- Send bulk of messages Kafka Producer
- Send byte array to storm kafka bolt
- Send email from Elmah?
- Sending information to a ngnix from php on the same server without http
- Send Custom Java Objects to Kafka Topic
- Send KafkaProducer from local machine to hortonworks sandbox on virtualbox

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.