1

I am using dotnet to build a Kafka application and I found that this NuGet package from Confluent.

The documentation is okay, but I was wondering the following: I have a controller, and I will receive multiple requests, where I will use a producer to insert data into a Kafka topic. But I'm not sure what to do with the ProducerBuilder class. Should I create a producer on each request, or just a producer per the lifetime of the app? What are the pros & cons?

Vivere
  • 1,919
  • 4
  • 16
  • 35

1 Answers1

1

Producer instances should only be created once, per unique producer configuration. One instance can write to many topics. You need a builder to get the instance.

You should be able to inject a built Producer into your Service/Controller classes rather than needing to rebuild one on each request.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Okay, that's good to hear. Their examples always show something like `using (var prod = ProducerBuilder(...)` so I'm guessing that in this case, I need to keep an `IProducer` as a field, implement `IDisposable` and manually call `Dispose` on the producer. – Vivere May 09 '22 at 14:18
  • 2
    You shouldn't close the producer after each request, either, only during your application teardown lifecycle – OneCricketeer May 09 '22 at 14:21