I have a list of messages need to be sent to rabbitMq. I am using masstransit and i publish messages one by one like this;
foreach (var @event in events)
{
await _bus.Publish(@event, ....);
}
But with this approach throughput is very low, so i want to try batch publishing somethig like this;
var tasks = new List<Task>();
foreach (var @event in events)
{
var t = _bus.Publish(@event, ....);
tasks.Add(t);
}
await Task.WnenAll(tasks);
//and with this configuration;
hostConfigurator.ConfigureBatchPublish(x =>
{
x.SizeLimit = 256 * 1024;
x.Enabled = true;
x.Timeout = TimeSpan.FromMilliseconds(4);
x.MessageLimit = 100;
});
My question is: With this batch publishing method, will the order of the messages being sent to RabbitMQ still be preserved same as first one by one method?
I tried publish messages one by one but throughput is very low.
They should be delivered to the broker in-order, yes. You can also use the PublishBatch
extension method instead of looping yourself.