Kafka
Kafka Consumer
Troubleshooting
Programming
Coding Issues

Simple Kafka Consumer Example not working

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 distributed messaging system used by many organizations to handle real-time data feeds through a publish-subscribe mechanism. One of the common components of Apache Kafka is the consumer, which reads data from Kafka. However, beginners might sometimes struggle with setting up a simple Kafka consumer due to various reasons. Here we will explore why a simple Kafka consumer might not work and how to troubleshoot these issues.

Understanding Kafka Consumers

Kafka consumers read records from Kafka topics. These records are in key-value format and consumers are responsible for processing this data. Consumers can subscribe to one or more Kafka topics and read the data in real-time as it’s produced by producers.

Common Problems and Solutions

There are several common issues that might prevent a Kafka consumer from working properly, including configuration errors, network issues, and incorrect API usage.

1. Incorrect Consumer Configurations

The consumer configuration needs to be correct for the consumer to function properly. Key configurations include bootstrap.servers, which lists the Kafka brokers' addresses, and group.id, which denotes the consumer group ID for consumers to coordinate.

Solution: Verify all configurations are correct, particularly:

  • bootstrap.servers - Kafka broker addresses
  • group.id - unique consumer group ID

2. Topic Does Not Exist

If the consumer is subscribed to a non-existent topic, it won’t receive any data.

Solution: Ensure the topic exists in your Kafka cluster. You can use the Kafka command-line tools to list available topics:

bash
kafka-topics.sh --list --bootstrap-server <broker1:port,broker2:port>

3. Network Issues

Consumers might not be able to connect to Kafka brokers due to network issues such as firewalls or incorrect network configurations.

Solution: Check network connectivity and firewall rules. Kafka runs by default on port 9092, so ensuring this port is accessible is crucial.

4. Kafka Version Compatibility

Mismatch between the Kafka broker version and the client library version can cause issues.

Solution: Ensure that the Kafka client library version is compatible with the server version.

5. Consumer API Misusage

Incorrect usage of Kafka consumer API, like not correctly polling for new data, can cause the consumer to seem like it's not working.

Solution: Refer to the official Apache Kafka documentation or reliable sources to understand proper usage of Kafka consumer APIs.

Sample Code for a Simple Kafka Consumer

Here is a basic example of a Kafka consumer written in Java:

java
1import org.apache.kafka.clients.consumer.ConsumerRecord;
2import org.apache.kafka.clients.consumer.ConsumerRecords;
3import org.apache.kafka.clients.consumer.KafkaConsumer;
4
5import java.util.Arrays;
6import java.util.Properties;
7
8public class SimpleKafkaConsumer {
9    public static void main(String[] args) {
10        Properties props = new Properties();
11        props.put("bootstrap.servers", "localhost:9092");
12        props.put("group.id", "test-group");
13        props.put("enable.auto.commit", "true");
14        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
15        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
16
17        try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props)) {
18            consumer.subscribe(Arrays.asList("test"));
19            while (true) {
20                ConsumerRecords<String, String> records = consumer.poll(100);
21                for (ConsumerRecord<String, String> record : records)
22                    System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
23            }
24        }
25    }
26}

Troubleshooting Summary

Here's a quick summary of the points to consider when troubleshooting Kafka consumers:

IssueSolution
Incorrect ConfigurationsDouble-check bootstrap.servers, group.id.
Topic Does Not ExistVerify topic existence using Kafka CLI tools.
Network IssuesCheck network connectivity, firewall, and broker port accessibility (default 9092).
Version CompatibilityAlign Kafka client library with the server version.
API MisusageFollow best practices and correct API usage as per the official Apache Kafka documentation.

Final Thoughts

Effectively setting up and troubleshooting Kafka consumers requires an understanding of Kafka architecture, careful attention to configuration details, and a good grasp of networking basics. By following the guidelines and solutions discussed, one can systematically resolve most issues encountered with Kafka consumers in a development or production environment.


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.