Kafka Consumers
High Level Consumer
Low Level Consumer
Apache Kafka
Consumer Comparison

Kafka High Level Vs Low level consumer

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, a distributed streaming platform, offers two types of consumer APIs: high-level and low-level. These APIs differ significantly in their design, usage, and level of control they provide to developers managing data streams. Understanding these differences is crucial for developers and architects to make informed decisions when building Kafka-based applications.

High-Level Consumer (Consumer Groups)

The high-level consumer API in Kafka is part of the newer Consumer API (org.apache.kafka.clients.consumer.KafkaConsumer) introduced in Kafka 0.9. This API abstracts many of the complexities of the underlying details and provides a simpler interface for consuming messages from Kafka topics.

Features:

  1. Group Management: Easily supports consumer groups, enabling Kafka to dynamically distribute partitions across the members of the group. This provides load balancing and failover capabilities.
  2. Offset Management: Automatically manages partition offsets, with options for committing offsets periodically or based on some custom logic, reducing the risk of data loss or duplication by ensuring that every message is processed exactly once.
  3. Integration with Kafka's Consumer Coordinator: Leverages Kafka's built-in coordinator for rebalancing partitions among consumer instances, simplifying overall management.
  4. Ease of Use: Simplifies the consumption of messages from Kafka, enabling developers to focus more on the processing logic rather than the details of how messages are fetched and managed.

Example Usage:

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("group.id", "test-group");
4props.put("enable.auto.commit", "true");
5props.put("auto.commit.interval.ms", "1000");
6props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
7props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
8KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
9consumer.subscribe(Arrays.asList("topic1", "topic2"));
10
11try {
12    while (true) {
13        ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
14        for (ConsumerRecord<String, String> record : records) {
15            System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
16        }
17    }
18} finally {
19    consumer.close();
20}

Low-Level Consumer (Simple Consumer)

The low-level consumer API (org.apache.kafka.clients.consumer.SimpleConsumer) is deprecated as of Kafka 0.11 and should generally be avoided unless there is a specific reason for its use, such as a need for fine-grained control over partition and offset management.

Features:

  1. Manual Control: Provides direct control over partition assignments and offsets, giving the developer fine-grained control over message consumption.
  2. Complexity: Requires manual management of offsets, partitions, and error handling, which can increase the complexity of the codebase.
  3. No Consumer Group Coordination: Does not natively support Kafka’s consumer group management, requiring more effort to achieve similar rebalancing and failover capabilities.

Example Usage:

Due to its deprecation, providing an extended example of the SimpleConsumer usage is not generally advised.

Comparison Table

Here is a concise summary of the key differences:

FeatureHigh-Level ConsumerLow-Level Consumer
Consumer GroupsSupported automaticallyManual implementation required
Offset ManagementAutomatic with options for manual controlEntirely manual
Ease of UseHigh (less boilerplate code)Low (more manual setup required)
Failover and Load BalancingManaged by KafkaManually managed

Conclusion

For most applications, the high-level consumer API is recommended due to its robust feature set and ease of use. It significantly reduces the amount of boilerplate code required to safely and efficiently consume messages from Kafka. The low-level consumer may be used in specialized situations where greater control over the consumption process is necessary, but such scenarios are less common.

Additional Considerations

In enterprise settings, monitoring, security, and tuning (such as adjusting consumer lag and understanding throughput) are important aspects of managing Kafka consumers. Both types of consumers can be monitored using Kafka's JMX metrics, though the high-level consumer generally provides more straightforward integrations with monitoring tools due to its widespread use and support.

In summary, choosing the right consumer API depends on specific application needs, but the high-level consumer will satisfy requirements for most users with added benefits of ease of use and comprehensive support from the Kafka community.


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.