KafkaConsumer
Java API
Programming
Java Coding
Java Subscription Method

KafkaConsumer Java API subscribe() vs assign()

System Design practice on Codemia

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

Practice system design

Apache Kafka is a popular stream-processing software platform developed by LinkedIn and donated to the Apache Software Foundation, designed for handling real-time data feeds. The Java API provided by Kafka includes two primary methods for consuming data: subscribe() and assign(). These methods define how Kafka consumers connect to the Kafka cluster and fetch data from topics. Here, we will explore both methods, including their differences, use cases, and how they fit into the broader context of Kafka consumer operations.

Understanding KafkaConsumer subscribe() Method

The subscribe() method is used to dynamically subscribe the consumer to a list of topics. It's higher-level and enables the consumer to be part of a consumer group managed by Kafka. When consumers use subscribe(), the Kafka broker automatically allocates partitions across the group.

Use Case and Benefits

  • Dynamic Scale: Automatically adjusts when new consumers join or leave the group.
  • Load Balancing: The Kafka Cluster balances partitions among all consumers in the group, which simplifies handling consumer failures and recoveries.
  • Simplicity: Simplifies operations as the Kafka Cluster manages the partition assignments.

Example Usage

java
1KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
2consumer.subscribe(Arrays.asList("topic1", "topic2"));
3while (true) {
4    ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
5    for (ConsumerRecord<String, String> record : records) {
6        System.out.println("offset = " + record.offset() + ", key = " + record.key() + ", value = " + record.value());
7    }
8}

Understanding KafkaConsumer assign() Method

The assign() method allows for manual assignment of partitions to the consumer. This method provides more control to the developer over which partitions to consume.

Use Case and Benefits

  • Direct Control: Offers granular control over exactly which partitions to consume from without Kafka's intervention in balancing or assignment logic.
  • No Consumer Groups: Does not require the consumer to be part of a consumer group.
  • Specific Use Cases: Useful for scenarios like consuming from specific partitions for data locality or processing historical data.

Example Usage

java
1KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
2consumer.assign(Arrays.asList(new TopicPartition("topic1", 0))); // Only consume from partition 0 of topic1
3while (true) {
4    ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
5    for (ConsumerRecord<String, String> record : records) {
6        System.out.println("offset = " + record.offset() + ", key = " + record.key() + ", value = " + record.value());
7    }
8}

Comparison Table

Featuresubscribe()assign()
Partition AssignmentManaged by KafkaManual control
Consumer GroupRequiredNot required
Use CaseGeneral use, scaling consumer applicationsSpecific partitions, special use cases
FlexibilityLess flexibility, more automationMore control, less automation
Fault ToleranceHigher due to rebalancingLower, manual intervention needed for failures

Advanced Considerations

  • Rebalancing: When using subscribe(), be aware of potential rebalancing, which may temporarily halt data consumption as partitions are reassigned among consumers.
  • Offset Management: Both methods allow for different strategies in managing offsets, crucial for keeping track of record processing state.

Conclusion

Choosing between subscribe() and assign() in Kafka's Java API largely depends on the application requirements—whether the need is for flexibility and control or simplicity and scalability. While subscribe() is suitable for most scenarios where scalability and fault tolerance are necessary, assign() provides the necessary tools for situations requiring precise control over partition consumption. Understanding both methods allows developers to better design and implement robust, efficient Kafka consumer applications.


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.