Kafka Topics
Data Measurement
Bytes
Server Management
Information Technology

How to see the size of a Kafka topic in bytes

Codemia Knowledge Hub ·

A Kafka topic's size is the disk space used by its partition logs, so you get it by summing partition sizes. The quickest way is kafka-log-dirs.sh --describe, which reports the size of each partition on every broker; for code, use the AdminClient describeLogDirs API.

How Kafka stores topic data

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), and those records are stored in a set of segment files on disk. The topic size therefore depends on what retention or log compaction leaves in those segments.

Using kafka-log-dirs.sh

kafka-log-dirs.sh ships with Kafka and describes the log directories on brokers. To check the size of a topic's partitions across all brokers, run:

bash
kafka-log-dirs.sh --describe --bootstrap-server localhost:9092 --topic-list my-topic

Replace localhost:9092 with your broker address and my-topic with your topic name. The output lists the size of each partition on every broker; add them up to get the topic total.

Using the AdminClient API in Java

To fetch sizes from code, use AdminClient:

java
1Properties properties = new Properties();
2properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
3try (AdminClient adminClient = AdminClient.create(properties)) {
4    DescribeLogDirsResult result = adminClient.describeLogDirs(Collections.singletonList(0)); // Broker ID 0
5    Map<Integer, Map<String, LogDirDescription>> logDirs = result.allDescriptions().get();
6    logDirs.forEach((broker, logDir) -> {
7        logDir.forEach((dir, info) -> {
8            info.replicaInfos().entrySet().stream()
9                .filter(e -> e.getKey().topic().equals("my-topic"))
10                .forEach(e -> System.out.println("Partition " + e.getKey().partition() + " Size: " + e.getValue().size()));
11        });
12    });
13}

Here my-topic is the topic name. The code prints the size of each partition of that topic.

MetricDescriptionRelevance
Topic SizeTotal size of the topic across all partitions.Useful for monitoring the disk space utilization.
Partition CountNumber of partitions in the topic.Important for load balancing and parallelism.
Replication FactorNumber of replicas per partition.Critical for fault tolerance and durability.
Broker Storage UtilizationPercentage of storage used in each broker.Prevents brokers from getting overwhelmed.

What changes a topic's size

  • Retention policies: retention settings control how long data is kept, and therefore how big the topic gets. Make sure they fit your storage capacity.
  • Compaction: with log compaction, older records are replaced by the latest value for each key. This can shrink a topic a lot when it has frequent updates but few unique keys.
  • Performance: large topics can slow rebalances and recoveries, so keeping size in check helps performance.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design