Need to send 1000s of Commands in Azure Service Bus - Best Way to batch it?

Hi all,

I am looking to send about 4000+ commands through Azure Service Bus. (NServiceBus). Each command is for an individual application which will trigger few events and save it to our database. I am looking to batch command (100-200 in one go).

What is the best way to achieve this ?

Thank you for any help!

You can’t send a batch of messages using ServiceBusMessageBatch with NServiceBus but you can dispatch multiple messages concurrently and gain some throughput improvement. You’d need to send/publish messages w/o awaiting, collecting each send operation in a list and awaiting for all those to eventually complete.

var tasks = new List<Task>();
foreach (var item in items)
await Task.WhenAll(tasks);

When dispatching messages, take into consideration wherever you do that inside or outside message handler. If outside message handler, you can go fairly wild with number of tasks. Inside message handler, you need to watch out for transport transaction mode the transport is configured with. If it’s SendsAtomicWithReceive, you’ll be limited to maximum 100 messages. With ReceiveOnly you’ll be able to surpass that limit but then you’re not sending the outgoing messages in a transaction with the incoming message. The choices, the decisions to make - depends on what you need more.