3

Is it possible to have single instance of a Kafka producer shared between multiple threads to send messages to multiple topics? Is there any concurrency issue I might want to be aware of before?

stefanobaghino
  • 11,253
  • 4
  • 35
  • 63
preetham
  • 738
  • 1
  • 8
  • 15
  • 2
    Possible duplicate of [Kafka Producer - By default supports Multithreading?](https://stackoverflow.com/questions/28540425/kafka-producer-by-default-supports-multithreading) – Valentin Michalak Jan 06 '18 at 01:32

1 Answers1

6

According to the KafkaProducer documentation

The producer is thread safe and sharing a single producer instance across threads will generally be faster than having multiple instances.

So yes, it's perfectly valid and even recommended to share a single producer between multiple threads.

Mikita Harbacheuski
  • 2,193
  • 8
  • 16