Kafka
Consumer Group
Multiple Topics
Data Access
Distributed Systems

Kafka Use common consumer group to access multiple topics

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 an open-source distributed event streaming platform used by thousands of companies for high-performance data pipelines, streaming analytics, data integration, and mission-critical applications. It was originally developed by LinkedIn and later open sourced under the Apache Software Foundation.

Understanding Kafka Consumer Groups

In Kafka, a consumer group is a group of consumer processes that are subscribed to a Kafka topic. The purpose of a consumer group is to allow a pool of processes to jointly consume a topic. The consumers in a group then share the workload, ensuring that multiple consumers can read from a topic in parallel without duplicating data among themselves.

Key Concepts:

  • Consumer: A process or thread that consumes data from Kafka topics.
  • Consumer Group: A collection of one or more consumers that jointly consume data from one or several topics.
  • Partition: Kafka topics are split into one or more partitions. Within a consumer group, a partition is consumed by only one consumer to ensure data locality and load balancing.

Using a Common Consumer Group to Access Multiple Topics

Kafka supports configuring a consumer group to consume from multiple topics. Each consumer in the group can subscribe to one or many topics, expanding the group's consumption capabilities. This configuration can effectively distribute data processing across different topics for better scalability and fault tolerance.

Technical Execution:

  1. Setting Up Kafka Consumers: Instantiate Kafka consumers and configure them to use the same consumer group through the group.id property.
  2. Subscribing to Topics: Consumers use the subscribe method with a list of topics. This enables them to listen for messages from all these topics.
java
1import org.apache.kafka.clients.consumer.KafkaConsumer;
2import java.util.Arrays;
3import java.util.Properties;
4
5public class ConsumerGroupExample {
6    public static void main(String[] args) {
7        Properties props = new Properties();
8        props.put("bootstrap.servers", "localhost:9092");
9        props.put("group.id", "test-group");
10        props.put("enable.auto.commit", "true");
11        props.put("auto.commit.interval.ms", "1000");
12        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
13        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
14
15        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
16        consumer.subscribe(Arrays.asList("topic1", "topic2", "topic3")); // subscribing to multiple topics
17        
18        while (true) {
19            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
20            for (ConsumerRecord<String, String> record : records) {
21                System.out.println("offset = " + record.offset() + ", key = " + record.key() + ", value = " + record.value() + ", topic = "+ record.topic());
22            }
23        }
24    }
25}

Benefits of Using a Common Consumer Group for Multiple Topics:

  • Load Balancing: Consumers in the group automatically share the workload of multiple topics.
  • Fault Tolerance: If one consumer fails, others can take over its partitions to ensure continuous processing.
  • Simplified Management: Managing a single group for multiple topics can simplify administration compared to managing multiple groups.

Considerations and Best Practices

  • Partition Count: The total number of consumer instances should be less than or equal to the total number of partitions across all topics for effective load balancing.
  • Offset Management: Ensure that the auto commit of offsets is appropriately configured, or manage offsets manually to prevent data loss or duplication.

Summary Table

FeatureDescription
Consumer GroupsAllows joint consumption of topics, enhancing scalability and reliability.
Multiple Topic SubscriptionConsumers can subscribe to multiple topics, centralizing processing and management.
Load BalancingEven distribution of messages among consumers in the group.
Fault ToleranceProvides durability and high availability by reassigning tasks if a consumer fails.
Offset ManagementCritical for ensuring correct message processing; can be automated or manually handled.

Conclusion

Using consumer groups to access multiple topics in Kafka is a powerful feature for building robust, scalable streaming applications. Properly configured consumer groups ensure balanced data processing, fault tolerance, and simplified operational management. This setup is highly relevant in systems where data ingestion and processing need to be optimized across various streams concurrently.


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.