Using Kafka Producer by different threads

Bill Goldberg picture Bill Goldberg · Mar 24, 2016 · Viewed 10.6k times · Source

I have kafka producer for my java based web application to push messages to Kafka. As per the documentation I could see kafka producer is thread safe. Does it mean that I can have single instance of Kafka producer and use it by different threads ( web requests ) each will open and close the producer in my case. Will this create any issues ? Or Is better to initiate Producers per request ?

Answer

Shawn Guo picture Shawn Guo · Mar 24, 2016

Yes, KafkaProducer is threadsafe.
Refer to Class KafkaProducer

A Kafka client that publishes records to the Kafka cluster.

The producer is thread safe and should generally be shared among all threads for best performance.

The producer manages a single background thread that does I/O as well as a TCP connection to each of the brokers it needs to communicate with. Failure to close the producer after use will leak these resources.