Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should i use batch consumer vs single record consumer

Tags:

spring-kafka

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?

like image 672
Raj Avatar asked Oct 27 '25 05:10

Raj


1 Answers

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.

like image 60
Gary Russell Avatar answered Oct 30 '25 15:10

Gary Russell