Search code examples
.netazureapache-kafkaazureservicebusservicebus

Producer Batching Service Bus Vs Kafka


Kafka provides a feature where we can specify the producer batch size. This is the size of the messages that can be sent to the Queue at once. The advantage being we can reduce the number of network calls the the Queue. Do we have anything similar in Service Bus? The only batching Service Bus does is when the message is delivered to the Queue. But this doesn’t reduce the number of I/O calls made.

The objective here is to batch the messages being sent to the Service Bus Queue by producer to reduce the number of network calls.


Solution

  • With .NET SDK you can create ServiceBusMessageBatch. When creating a batch, CreateMessageBatchOptions can be passed it to specify the maximum batch size. For example

    var options = new CreateMessageBatchOptions { MaxSizeInBytes = 1_024 };
    using var messageBatch = await sender.CreateMessageBatchAsync();
    

    Will construct a batch of up to 1,024 bytes. When not specified, by default, a batch will be the maximum message size for the namespace tier used, 256KB for Standard and 100 MB for Premium.