Service Bus Batching

October 02, 2022

One annoying little issue that you come across when dealing with batch sending for Azure Service Bus if that the total size of the batched message is restricted to the same size as a single message. At the time of writing, this was 256KB for standard tier. What this means is that, if you have a message that’s, say, 500 bytes - you’re restricted to a batch of around 5 - 600 before you exceed the send limit.

You can batch these; and, in fact, Azure Service Bus has a little feature that allows you to do this: ServiceBusMessageBatch.

Let’s take the following code:



await using var serviceBusClient = new ServiceBusClient(connectionString);
var sender = serviceBusClient.CreateSender(topicname);
List<ServiceBusMessage> serviceBusMessages = new();    

string messageText = new string('a', 500);

for (int i = 1; i <= 1000; i++)
{
    serviceBusMessages.Add(
        new ServiceBusMessage(messageText);        
}
await sender.SendMessagesAsync(serviceBusMessages);

This code would crash, as it would exceed the message limit. However, the following code has the same effect, but would simply batch the messages into messages no larger than 256KB and send each as it was ready:



List<ServiceBusMessage> serviceBusMessages = new();
var serviceBusMessageBatch = await sender.CreateMessageBatchAsync();

for (int i = 1; i <= count; i++)
{
    if (!serviceBusMessageBatch.TryAddMessage(new ServiceBusMessage(messageText)))
    {
        await sender.SendMessagesAsync(serviceBusMessageBatch);
        serviceBusMessageBatch.Dispose();
        serviceBusMessageBatch = await sender.CreateMessageBatchAsync();
    }
}

await sender.SendMessagesAsync(serviceBusMessageBatch);

References

https://weblogs.asp.net/sfeldman/sb-safebatching



Profile picture

A blog about one man's journey through code… and some pictures of the Peak District
Twitter

© Paul Michaels 2024