Is IProducer.ProduceAsync(...) thread safe?
See original GitHub issueDescription
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:
- Created 4 years ago
- Reactions:4
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
yes, our documentation is badly lacking. it’s on the list.
yes