title: Java/Kotlin Kafka Consumer using for multiple instances (container) in web app
body: <p>For my Spring boot web app, after the application starts, I want to call a method of a class to keep it running until the app shutdowns. </p>
  
  <p>However if my application is deployed to Kubernetes cluster, so it would normally have multiple instances of the web app, let's say 4 instances (so I assume there will be 4 instances of <code>KafkaConsumer</code> as well). I am wondering whether the following piece of code is thread safe or can be horizontal scale?</p>
  
  <p>As when my app stops, I got some warning like following: 
  <code>kafka consumer is not safe for multithreaded access</code></p>
  
  <pre><code>@Component
  class KafkaConsumerService : ApplicationRunner {
    private lateinit var kafkaConsumer: KafkaConsumer&lt;String, String&gt;
  
    init {
      val props = Properties()
      props[ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG] = "127.0.0.1:9092"
      props[ConsumerConfig.GROUP_ID_CONFIG] = "AnotherDemoConsumer"
      props[ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG] = StringDeserializer::class.java.name
      props[ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG] = StringDeserializer::class.java.name
  
      kafkaConsumer = KafkaConsumer(props)
    }
  
    override fun run(args: ApplicationArguments?) {
      receiveFromKafka()
    }
  
    fun receiveFromKafka() {
      kafkaConsumer.subscribe(listOf("test-topic"))
  
      while (true) {
        val consumerRecords = kafkaConsumer.poll(3000)
  
        consumerRecords.forEach { record -&gt;
          logger.info("Receive Kafka message having key: ${record.key()}, value: ${record.value()}, " +
              "partition: ${record.partition()}, offset: ${record.offset()}")
        }
      }
    }
  }
  </code></pre>