azureservicebusazure-servicebus-topics

Is there a way to guarantee bulk messages publishing in Azure service bus?


I want to fan out a process to N consumers (listeners to Azure Service Bus topic). In order to do so, I need a way to publish N messages to Service Bus Topic but in an atom8c fashion.

Its important that messages are processed in parallel by N consumers and not by one consumer which, according to my understanding, would be the case with batch messages publish.


Solution

  • There's no correlation between the approach that you use to publish (single or batch) and how consumers read messages - they are completely independent service operations.

    To answer the question being asked, to publish a batch of messages atomically, you'd use a ServiceBusMessageBatch. A ver basic example would look something like:

    // This example is simplified to illustrate the basic structure and 
    // is not a good example of real-world production use.  
    // I don't recommend using verbatim.
    
    using ServiceBusMessageBatch messageBatch = await sender.CreateMessageBatchAsync();
    
    if ((!messageBatch.TryAddMessage(new ServiceBusMessage("first"))
        || (!messageBatch.TryAddMessage(new ServiceBusMessage("second")))
    {
        throw new ApplicationException("Could not add all messages to the batch.");
    }
    
    await sender.SendMessagesAsync(messageBatch);
    

    More examples and discussion can be found in the sample Sending and receiving messages.

    Once publishing is complete, there is no relationship maintained between messages that were in a batch together. The service will extract all messages and make each one individually available to readers.

    When consuming messages, the caller is in control and may request a single message or a batch of messages for any receive operation. Note that a "batch" in this case is simply the service gathering up some number of individual messages and choosing to return them together. There is no correlation to how those messages were published.

    All consumers reading from the same entity (queue or subscription) are competing consumers. In your scenario, it sounds like what you want to do is:

    Whether consumers read a single message at a time, or some number of messages batched to improve throughput, they will continue to work in parallel and independent of one another.

    (listeners to Azure Service Bus topic)

    I'm assuming this is a typo. Consumers cannot read from a Service Bus topic, only a subscription associated with that topic.