Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefaultKafkaProducerFactory - add threadLocalProducer option #1095

Closed
bekker opened this issue May 14, 2019 · 1 comment · Fixed by #1096
Closed

DefaultKafkaProducerFactory - add threadLocalProducer option #1095

bekker opened this issue May 14, 2019 · 1 comment · Fixed by #1096
Assignees
Milestone

Comments

@bekker
Copy link

bekker commented May 14, 2019

Affects Version(s): all version

DefaultKafkaProducerFactory currently produces a singleton Producer (#8), because Producer is thread-safe and faster in that way.

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

Thus KafkaTemplate with DefaultKafkaProducerFactory shares one Producer across all threads.

But when autoFlush set to true, KafkaTemplate tries to flush shared Producer for every send request.

When a fail or a delay occurs on Kafka cluster and a send request takes a long time, every other send requests have to wait for it while they don't have to.

One delay may cause unnecessary full-stop on whole application.

I think DefaultKafkaProducerFactory should always create new object - not singleton - so that KafkaTemplate be able to choose to use thread-local Producer or not. But in that case there would be huge implications... any suggestions?

@garyrussell garyrussell changed the title Producer should not be singleton when KafkaTemplate autoFlush set to true DefaultKafkaProducerFactory - add threadLocalProducer option May 14, 2019
@garyrussell
Copy link
Contributor

garyrussell commented May 14, 2019

  • Add an option the DKPF to maintain producers in a ThreadLocal instead of a singleton.
  • add closeProducer to the DKPF so clients can physically close the producer and clean up the TL when no longer needed

@garyrussell garyrussell added this to the 2.3.M3 milestone May 14, 2019
@garyrussell garyrussell self-assigned this May 15, 2019
garyrussell added a commit to garyrussell/spring-kafka that referenced this issue May 15, 2019
garyrussell added a commit to garyrussell/spring-kafka that referenced this issue May 15, 2019
artembilan pushed a commit that referenced this issue May 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants