question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is IProducer.ProduceAsync(...) thread safe?

See original GitHub issue

Description

Is it safe to re-use an instance of IProducer to call ProduceAsync() from multiple threads?

My use case is an API method in an ASP.NET core application, in which I want to produce a message when the API is called.

How to reproduce

[Route( "api/demo/producer" )]
[ApiController]
public class ProducerController : ControllerBase
{
    private readonly IProducer<int, string> _kafkaProducer;

    public ProducerController( IProducer<int, string> kafkaProducer )
    {
        _kafkaProducer = kafkaProducer;
    }

    [HttpPost( "produce" )]
    public async Task<string> Produce( [FromBody] int key, [FromBody] string value )
    {
        var message = new Message<int, string> { Key = key, Value = value };
        var result = await _kafkaProducer.ProduceAsync( "test-topic", message );
        return $"Delivered '{result.Key}','{result.Value}' to '{result.TopicPartitionOffset}'";
    }
}

where the producer instance is injected as a singleton by adding it as a singleton in ConfigureServices() like this:

services.AddSingleton( new ProducerBuilder<int, string> ( config ).Build( ) );

Checklist

Please provide the following information:

  • A complete (i.e. we can run it), minimal program demonstrating the problem. No need to supply a project file.
  • Confluent.Kafka nuget version.
  • Apache Kafka version.
  • Client configuration.
  • Operating system.
  • Provide logs (with “debug” : “…” as necessary in configuration).
  • Provide broker log excerpts.
  • Critical issue.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
mhowlettcommented, Aug 28, 2019

yes, our documentation is badly lacking. it’s on the list.

3reactions
mhowlettcommented, Aug 28, 2019

yes

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is Confluent.Kafka.Producer<TKey, TValue>.ProduceAsync ...
All operation on client are thread safe (with some small exceptions documented in the API). AFAIK, there are no such method yet in...
Read more >
KafkaProducer (kafka 0.11.0.2 API)
The producer is thread safe and sharing a single producer instance across threads will generally be faster than having multiple instances.
Read more >
Performant .NET producer example | CDP Public Cloud
NET producer with async processing and TPL. This producer is capable of producing around 50000 messages every second in development environment.
Read more >
kafka.producer package — kafka-python 1.0.0 documentation
The producer is thread safe and sharing a single producer instance across threads will generally be faster than having multiple instances. The producer...
Read more >
Getting Started - Karafka framework documentation
Works from any place in your code and is thread-safe # You usually want to produce async but here it may raise exception...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found