1

I am using spring-kafka 2.2.8 and writing a simple async producer with the below settings:

producer config key : compression.type  and value is : none
producer config key : request.timeout.ms  and value is : 10000
producer config key : acks  and value is : all
producer config key : batch.size  and value is : 33554431
producer config key : delivery.timeout.ms  and value is : 1210500
producer config key : retry.backoff.ms  and value is : 3000
producer config key : key.serializer  and value is : class org.apache.kafka.common.serialization.StringSerializer
producer config key : security.protocol  and value is : SSL
producer config key : retries  and value is : 3
producer config key : value.serializer  and value is : class io.confluent.kafka.serializers.KafkaAvroSerializer
producer config key : max.in.flight.requests.per.connection  and value is : 1
producer config key : linger.ms  and value is : 1200000
producer config key : client.id  and value is : <<my app name>>

I've printed the above producer settings using below code snippet:

DefaultKafkaProducerFactory defaultKafkaProducerFactory = (DefaultKafkaProducerFactory) mykafkaProducerFactory;
        Set<Entry> set  = defaultKafkaProducerFactory.getConfigurationProperties().entrySet();
        set.forEach( item ->
                System.out.println("producer config key : "+item.getKey()+"  and value is : "+item.getValue())
        );

Now i'm creating a KafkaTemplate with autoFlush as false by calling the below constructor

public KafkaTemplate(mykafkaProducerFactory, boolean autoFlush) 

Now i've an async producer producing 10 message in the span of 10 sec . Then surprisingly, i got all the 10 messages published onto the topic in few seconds and i'm sure the size of these 10 messages combined is way less than my batch.size: 33554431

Now my question is

  1. Why the messages are being published instead of waiting for either linger.ms or batch.size before producing the message?
Raj
  • 1,467
  • 3
  • 23
  • 51
  • There is only one question, not two (this seems to be a copy of https://stackoverflow.com/questions/62807232/why-async-producer-is-not-waiting-for-either-linger-ms-or-batch-size-to-be-full with the second question and Ctrl-C removed). Please explain further. – Gary Russell Jul 09 '20 at 20:04
  • sure, in the other question, I've stopped the app by clicking 'Cntrl+C' but in this case, I didn't stop the app. Even though i didn't stop the app, it's still publishing the messages in few seconds without waiting for either linger.ms or the batch size condition. So, I'm trying to understand why is this happening? – Raj Jul 09 '20 at 20:08

1 Answers1

2

It looks like you are not setting those properties correctly; show how you are setting them. I just tested with

batch.size=1000000
linger.ms=10000

and sent 10 messages back-to-back and it took exactly 10 seconds for them to arrive at the consumer.

spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.listener.type=batch

spring.kafka.producer.properties.batch.size=1000000
spring.kafka.producer.properties.linger.ms=10000
@SpringBootApplication
public class So62820095Application {


    private static final Logger LOG = LoggerFactory.getLogger(So62820095Application.class);


    public static void main(String[] args) {
        SpringApplication.run(So62820095Application.class, args);
    }

    @KafkaListener(id = "so62820095", topics = "so62820095")
    public void listen(List<String> in) {
        LOG.info(in.toString());
    }

    @Bean
    public NewTopic topic() {
        return TopicBuilder.name("so62820095").partitions(1).replicas(1).build();
    }

    @Bean
    public ApplicationRunner runner(KafkaTemplate<String, String> template) {
        return args -> IntStream.range(0,  10).forEach(i -> template.send("so62820095", "foo" + i));
    }

}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • ok, let me check that again. Thanks for the response. – Raj Jul 09 '20 at 20:26
  • I've updated the question to add more details on the producer configs. I can clearly see the producer factory entry for linger.ms is showing what I've set but still publishing messages too early. what else could be missing here in my test. please suggest. – Raj Jul 09 '20 at 22:15
  • Share your complete test project someplace and I'll take a look tomorrow to see what's wrong. – Gary Russell Jul 09 '20 at 22:30
  • sure, I'm trying to get that into a separate app. will share once i've it ready. Thanks – Raj Jul 10 '20 at 14:51
  • @Raj Any news? :) – Inego Dec 02 '20 at 06:41