Kafka
Python
Coding
Partition Management
Topic Partition

Kafka-python get number of partitions for topic

System Design practice on Codemia

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

Practice system design

Introduction

If you need the number of partitions for a Kafka topic in Python, the answer comes from topic metadata rather than from consuming messages. A Kafka client can ask the cluster for metadata about the topic and then count the partition ids returned for that topic.

Using KafkaConsumer.partitions_for_topic

With kafka-python, one of the simplest APIs is partitions_for_topic.

python
1from kafka import KafkaConsumer
2
3consumer = KafkaConsumer(
4    bootstrap_servers=["localhost:9092"],
5)
6
7partitions = consumer.partitions_for_topic("orders")
8print(partitions)
9print(len(partitions) if partitions is not None else 0)
10
11consumer.close()

If the topic exists and metadata is available, partitions is typically a set of partition numbers such as {0, 1, 2}.

Why The Return Value Can Be None

A return value of None usually means one of these things:

  • the topic does not exist
  • metadata has not been fetched successfully yet
  • the broker connection is unavailable

That is why len(partitions) should be guarded instead of assumed.

Refreshing Metadata Matters

Kafka clients cache metadata, and sometimes the answer appears stale if the topic was created or expanded very recently. Polling or explicitly waiting for metadata refresh can help in startup code.

python
consumer = KafkaConsumer(bootstrap_servers=["localhost:9092"])
consumer.topics()  # triggers metadata fetch
partitions = consumer.partitions_for_topic("orders")

Calling topics() is a simple way to ensure the client has performed a metadata request before you inspect the topic.

Using KafkaAdminClient For Administrative Code

If the code is administrative rather than consumer-oriented, you may prefer the admin client.

python
1from kafka.admin import KafkaAdminClient
2
3admin = KafkaAdminClient(bootstrap_servers="localhost:9092")
4metadata = admin.describe_topics(["orders"])
5print(metadata)
6admin.close()

Depending on library version and broker compatibility, the exact response shape varies, but the principle is the same: count the partitions described in the metadata.

Why Partition Count Matters

Applications query partition count for several common reasons:

  • validating deployment assumptions
  • assigning work across processes
  • calculating expected parallelism
  • checking whether a topic resize took effect

This is especially common in management scripts and monitoring code.

Be Careful With Producer Partition Logic

Knowing the number of partitions is useful, but an application should still not hardcode partition ids casually unless it owns the partitioning strategy. Topic partition counts can change over time, and brittle assumptions about fixed ids can create subtle production bugs.

Use metadata to learn about the topic, not to cement static assumptions unless the design requires it.

Handle Broker Errors Gracefully

Metadata calls can fail because Kafka is unavailable, the bootstrap server list is wrong, or authentication settings are missing.

That means partition discovery code should surface errors clearly instead of quietly treating every failure as "topic has zero partitions."

Common Pitfalls

The most common mistake is assuming partitions_for_topic can never return None. Another is forgetting to close the client after a short admin-style script finishes. Developers also sometimes query metadata immediately after topic creation and assume the result is authoritative before the client has refreshed metadata. Finally, partition count is metadata, not a guarantee about current consumer lag, leader health, or application throughput.

Summary

  • Use Kafka metadata APIs to discover partition count, not consumer message flow.
  • 'partitions_for_topic is a simple way to get the partition ids for a topic.'
  • Guard against None in case metadata is missing or the topic does not exist.
  • Refresh or trigger metadata fetch when topic state may have changed recently.
  • Treat partition count as useful cluster metadata, not as a substitute for full topic health checks.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
Track 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.

Practice system design

All Rights Reserved.