Kafka
Batch Consumer
Data Processing
Apache Kafka
Message Queue

Does Kafka have a batch 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 is a popular distributed event streaming platform that is widely used for building real-time data pipelines and streaming apps. It is built around the concept of topics, which store streams of records in a fault-tolerant way. Kafka facilitates real-time data processing, but when it comes to consuming data, the concept of batch consumption can be a bit nuanced. Kafka does not have a native batch consumer in the traditional sense that databases might execute batch operations. However, Kafka consumers can be configured to handle data in batches through its API settings which effectively allows the application to process records in batch mode.

Understanding Kafka Consumers

Kafka consumers read records from Kafka topics. They subscribe to one or more Kafka topics and read the records in the order in which they were produced. The consumer uses a pull model to retrieve records, meaning that the consumer requests batches of records from the broker.

Poll Mechanism and Batch Processing

The primary method by which a Kafka Consumer fetches data is through the poll() method. The poll() method retrieves records in batches from the broker based on configurations set in the consumer. The size of these batches and how often they are pulled can impact both performance and real-time processing capabilities.

Key Settings for Batch Consumption

To manage Kafka batch consumption effectively, certain configurations need to be tuned:

  • fetch.min.bytes: This configuration sets the minimum amount of data that the broker should return for a fetch request. If not enough data is available, the broker will wait until more becomes available rather than return a smaller set.
  • fetch.max.wait.ms: This configuration sets the maximum amount of time the broker will wait before responding to a fetch request if the fetch.min.bytes condition has not been met.
  • max.poll.records: This configures the maximum number of records the consumer will return when polling records.

Example Scenario

Here is a simple scenario illustrating how a consumer might be configured for batch-like processing:

java
1import org.apache.kafka.clients.consumer.KafkaConsumer;
2import org.apache.kafka.clients.consumer.ConsumerRecords;
3
4import java.util.Properties;
5
6public class BatchConsumer {
7    public static void main(String[] args) {
8        Properties props = new Properties();
9        props.put("bootstrap.servers", "localhost:9092");
10        props.put("group.id", "test");
11        props.put("enable.auto.commit", "false");
12        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
13        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
14        props.put("max.poll.records", "500");
15        props.put("fetch.min.bytes", "1024");
16        props.put("fetch.max.wait.ms", "500");
17
18        try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props)) {
19            consumer.subscribe(Arrays.asList("some-topic"));
20
21            while (true) {
22                ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
23                if (records.count() == 0) {
24                    continue;
25                }
26
27                // Process the batch of records
28                processRecords(records);
29
30                // Manually committing the offsets
31                consumer.commitSync();
32            }
33        }
34    }
35
36    private static void processRecords(ConsumerRecords<String, String> records) {
37        // logic to process each record
38    }
39}

In this configuration:

  • max.poll.records is set to 500 to ensure that each poll returns up to 500 records.
  • fetch.min.bytes and fetch.max.wait.ms are set to ensure that the consumer waits for enough data to be available or reaches a time limit before fetching the batch.

Summary Table

ConfigurationPurposeTypical Value
max.poll.recordsControls the maximum number of records per poll call.500
fetch.min.bytesMinimum amount of data per fetch request.1024 bytes
fetch.max.wait.msMaximum wait time for data on fetch request.500 ms

Conclusion

While Kafka does not have a native batch consumer functionality like bulk read/write operations in databases, it can be configured to emulate batch processing via its consumer API. By fine-tuning the consumer configurations, developers can manage the data flow to optimize both processing and throughput according to specific application needs. This makes Kafka a versatile tool for handling large-scale, real-time data in diverse environments.


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.