Kafka Consumer get assigned partitions for a specific topic
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka, a popular distributed streaming platform, handles large volumes of real-time data streams. It uses a high-throughput, low-latency platform capable of handling trillions of events a day. Central to its design is the concept of topics and partitions that help in distributing the data across a cluster to achieve fault tolerance and increased throughput. This article explains how a Kafka consumer can discover which partitions of a specific topic are assigned to it, particularly useful for processing streams effectively.
Understanding Partitions in Kafka
In Kafka, a topic is a category or feed name to which records are published. Topics in Kafka are always multi-subscriber; that is, a topic can have zero, one, or many consumers that subscribe to the data written to it. A topic is divided into one or more partitions. Each partition is an ordered, immutable sequence of records that is continually appended to a structured commit log. The partitions of the topics are distributed over the servers in the Kafka cluster with each server handling data and requests for a share of the partitions.
Consumer Groups and Partition Assignments
Multiple consumers can belong to a group (known as a consumer group) and each consumer in a group reads from exclusive partitions of a topic, ensuring that each partition is only read by one consumer from the group. If a consumer group has more consumers than partitions, some consumers will be idle.
Discovery and Assignment of Partitions
Kafka consumers use a group coordinator and a consumer leader to assign partitions. This is handled automatically by the Kafka protocol itself via consumer groups. When a consumer wants to join a group, it sends a request to the group coordinator. After all consumer requests are received, the leader assigns partitions to each consumer.
Fetch Assigned Partitions
The kafka.consumer.KafkaConsumer class in Kafka clients (like the Java client) allows consumers to interact with Kafka. To get the list of partitions assigned to a consumer, one can use the method assignment() after the consumer subscribes to a topic and Kafka completes the rebalance process.
Here is a Java example demonstrating this functionality:
In the example, consumer.assignment() returns a set of TopicPartition, which represents the partitions assigned to the consumer. The consumer polls (consumer.poll(100)) to refresh any pending partition assignments.
Summary Table
| Method/Attribute | Description | Importance |
subscribe() | Used by a consumer to subscribe to one or more topics. | Necessary to initiate partition assignment process. |
poll() | Fetches the set of new records or triggers a rebalance. | Essential for receiving assigned partitions. |
assignment() | Returns the set of partitions currently assigned to this consumer. | Key to determine partitions the consumer works on. |
Additional Details
Handling Partition Rebalancing
Partition rebalancing is a process that is triggered when a consumer joins or leaves a consumer group or when the topic partitions are modified. Such changes can cause a shift in which partitions are assigned to which consumer. This could potentially lead to issues like duplicate processing or data loss if not managed correctly. It is therefore crucial for consumers to handle rebalance events gracefully by committing offsets (if needed) before a rebalance and seeking to the appropriate offsets after rebalance.
Monitoring and Maintenance
Monitoring which partitions are assigned to which consumers can help in debugging issues in the flow of stream processing and ensuring that there are no imbalances that might lead to processing bottlenecks.
These features of Kafka not only assist in effective data management but also ensure that distributed data streams are processed in a fault-tolerant and efficient manner.
Related reading
- Kafka consumer gets stuck after exceeding max.poll.interval.ms
- kafka consumer group is rebalancing
- Kafka consumer group keep moving to PreparingRebalance state and stops consuming
- Kafka consumer group offset retention
- Kafka consumer group script to see all consumer group not working
- Kafka consumer groups and partitions
- Kafka consumer groups deletion
- Kafka Consumer hanging at .hasNext in java

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.