'Round Robin message assignment to partition not working for messages without key in asp.net core
Trying to send messages to all the partition in round-robin fashion but all the messages are going into the last partition. Can anyone please help me with that?
I am using Confluent.Kafka nuget package. My producer Configuration -
"ProducerConfiguration": {
"bootstrap.servers": "localhost:9092"
}
And my Kafka producer class -
public class Publisher
{
private ProducerConfig _producerConfig;
public Publisher(IOptions<ApplicationSetting> _applicationSetting)
{
var producerConfig = _applicationSetting.Value.KafkaConfiguration.ProducerConfiguration;
_producerConfig = new ProducerConfig(producerConfig);
}
public async Task<DeliveryResult<Null, TValue>> Publish<TValue>(TValue message, string topic = null)
{
using var producer = new ProducerBuilder<Null, TValue>(_producerConfig)
.SetValueSerializer(new JsonSerializer<TValue>())
.Build();
var topicName = String.IsNullOrEmpty(topic) ? message.GetType().Name : topic;
return await producer.ProduceAsync(topicName, new Message<Null, TValue>() { Value = message });
}
}
Publishing the message like -
public class DemoHandler : IRequestHandler<SendMail, string>
{
private readonly Publisher _publisher;
public DemoHandler(Publisher publisher)
{
_publisher = publisher;
}
public async Task<string> Handle(SendMail message, CancellationToken cancellationToken)
{
await _publisher.Publish(message);
return "Message sent";
}
}
All the messages are going to the last partition only -

Thanks in advance.
Solution 1:[1]
I added this answer
https://stackoverflow.com/a/71791802/628908
I faced this same issue. In this case the issue will be because you're re-creating a new IProducer instance each time you call your Publish method
If you could make sure the IProducer instance is the same every time you call Publish, that will let the producer to decide which partition send the message to.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alex |
