Search code examples
spring-kafka

Why async producer is not waiting for either linger.ms or batch.size to be full when i set them a custom value with autoFlush is set to false?


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

linger.ms : 300000, 
batch.size: 33554431, 
max.block.ms: 60000.

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

public KafkaTemplate(ProducerFactory<K, V> producerFactory, boolean autoFlush) 

Now i've a simple test producing 10 message in the span of 10 sec using the above async producer and then stopped my producer using 'Cntrl+C'. Then surprisingly, i got all the 10 messages published onto the topic and i'm sure the size of these 10 messages combined is way less than my batch.size: 33554431

Now i've two questions

  1. How the messages are being published instead of waiting for either linger.ms or batch.size before producing the message?
  2. In this scenario, what is the significance of autoFlush= false?

Solution

  • If this is a Spring Boot application, Ctrl-C will close the application context and the DefaultKafkaProducerFactory will close the producer in its destroy() method (called by the application context during close()).

    You will only lose records if you kill -9 the application.