As far as I know, there is no special concept such as batch consumer in Apache Kafka documentation but spring-kafka has an option to create a batch consumer using below code snippet.
@Bean
public KafkaListenerContainerFactory<?> batchFactory() {
    ConcurrentKafkaListenerContainerFactory<Integer, String> factory =
            new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());
    factory.setBatchListener(true);  // <<<<<<<<<<<<<<<<<<<<<<<<<
    return factory;
}
Now my question is,
When should i use batch consumer vs single record consumer? Can someone share few use cases to explain the usage of single record consumer vs batch consumer
As per the below thread, the main difference between single record consumer vs batch consumer is how many records are handled to the listener from the poll list.
What's the basic difference between single record kafka consumer and kafka batch consumer?
An example of using a batch listener might be if you to want to send data from multiple records to a database in one SQL statement (which might be more efficient than sending them one-at-a-time).
Another case is if you are using transactions; again, it might be more efficient to use one transaction for multiple SQL updates.
Generally, it's a performance consideration; if that's not a concern then the simpler one-at-a-time processing might be more suitable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With