Kafka 0.9.0
Multi-thread Consumer
Kafka Consumer Tutorial
Big Data Management
Programming Guide

How to use multi-thread consumer in kafka 0.9.0?

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 0.9.0 is a significant release from the Apache Software Foundation that introduced many important features like the new Java consumer API which supports multi-threaded processing. Using a multi-threaded consumer allows Kafka clients to process messages faster and more efficiently, which is critical for high-throughput applications.

Understanding Multi-Thread Consumer in Kafka 0.9.0

In Kafka, consumers read records from brokers. The traditional way was using a simple loop to read records from a topic. However, this single-threaded approach can be a bottleneck for processing high volumes of messages quickly. To leverage multi-core CPUs effectively, Kafka 0.9.0 allows you to implement a multi-threaded consumer model, enabling more efficient processing by parallelism.

Key Concepts to Know

  • Consumer Group: A consumer group includes the set of consumer processes that are subscribing to a topic. Kafka delivers each message in the subscribed topics to one consumer instance within each subscribing consumer group.
  • Partition Assignment: In a multi-threaded environment, Kafka assigns topic partitions to different threads, ensuring that each record from a partition is processed by only one consumer thread at any given time.

How to Implement Multi-Threaded Consumer in Kafka 0.9.0

Step-by-Step Guide

  1. Consumer Group Configuration: First, configure your consumer to be part of a consumer group. This allows Kafka to distribute message consumption across multiple consumers (and thus multiple threads) in the group.
java
1   Properties props = new Properties();
2   props.put("bootstrap.servers", "localhost:9092");
3   props.put("group.id", "test-group");
4   props.put("enable.auto.commit", "true");
5   props.put("auto.commit.interval.ms", "1000");
6   props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
7   props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
8   KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
  1. Subscribe to Topics: Determine the topics you need to subscribe to and use the subscribe method. This method also supports pattern-based subscription.
java
   consumer.subscribe(Arrays.asList("my-topic", "my-other-topic"));
  1. Thread Management: Divide the workload by creating multiple threads, each responsible for consuming messages.
java
1   int numberOfThreads = 4;
2   ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads);
3   for (int i = 0; i < numberOfThreads; i++) {
4       executorService.submit(new KafkaConsumerRunnable(consumer));
5   }
  1. Polling Loop: Each thread should contain a polling loop, which constantly polls new records from the broker.
java
1   public class KafkaConsumerRunnable implements Runnable {
2       private final KafkaConsumer<String, String> consumer;
3
4       public KafkaConsumerRunnable(KafkaConsumer<String, String> consumer) {
5           this.consumer = consumer;
6       }
7
8       @Override
9       public void run() {
10           while (true) {
11               ConsumerRecords<String, String> records = consumer.poll(100);
12               for (ConsumerRecord<String, String> record : records) {
13                   // Process record
14               }
15           }
16       }
17   }

Table: Summary of Key Points for Multi-threading in Kafka 0.9.0

ParameterDescriptionExample
bootstrap.serversKafka cluster's addresslocalhost:9092
group.idUnique identifier for the consumer grouptest-group
enable.auto.commitAuto commit offset if set to truetrue
auto.commit.interval.msFrequency of offset commit in ms1000
key.deserializerKey deserializer classStringDeserializer
value.deserializerValue deserializer classStringDeserializer
Number of ThreadsNumber of consumer threads4
Poll TimeoutTimeout for the poll in milliseconds100

Best Practices and Considerations

  • Avoid Complex Logic in the Polling Loop: Keep the logic inside the polling loop to a minimum to avoid delaying the polling of new records.
  • Handle Exceptions Appropriately: Properly handle exceptions to prevent one faulty message from impacting other messages or threads.
  • Ordering Guarantees: Remember that order is only guaranteed within a specific partition, not across partitions.

Conclusion

Implementing a multi-threaded consumer in Kafka 0.9.0 involves careful planning of thread usage and partition management, but it can significantly improve the consumer's performance and throughput, particularly for applications requiring real-time processing of large data streams.


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.