Azhari
Azhari

Reputation: 642

Azure Service Bus - Multiple subscribers don't receive all messages

I'm using the code to implement Azure service bus topics that can be found here: https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-how-to-use-topics-subscriptions

I've tried to run two instances of the subscriber program, which holds these methods:

private static void RegisterOnMessageHandlerAndReceiveMessages()
{
    var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
    {
        MaxConcurrentCalls = 1,
        AutoComplete = false
    };
    _subscriptionClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
}

private static async Task ProcessMessagesAsync(Message message, CancellationToken token)
{
    Console.WriteLine($"Received #{message.SystemProperties.SequenceNumber} message: {Encoding.UTF8.GetString(message.Body)}");
    await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
}

enter image description here

However, this doesn't allow both subscribers to recieve the message, it is recieved one message each split across both subscribers.

How does I ensure both subscribers get all the messages coming from the bus?

Upvotes: 4

Views: 1746

Answers (3)

Patrick Koorevaar
Patrick Koorevaar

Reputation: 1343

I also ran into this.

I resolved it by making an extra micro service just for forwarding the messages. The forwarder service itself must subscribe to the messages that each of your subscribers must receive. Each Subscriber registers itself (with a unique id) with the event forwarder service (by sending a message to it on startup). The subscribers must then use the message name + the unique id to subscribe to the messages. The message forwarder service will receive the messages and forward them to all the registered subscribers by changing the subject/label of the message (the unique id is added to the subject/label).

This workaround also has it's limitations, there can only be one instance of the message forwarder service or otherwise you have the same problem again. And the message forwarder service must also be running before the other subscribers otherwise it doesn't receive the registrations.

Upvotes: 0

darksider474
darksider474

Reputation: 1000

It's worth stating - as it's not obvious from any of the documentation - that each topic should be setup with a subscription for each subscriber.

I.e. if I have 3 subscribers:

/myservicebus/mytopic/mysubscription1
/myservicebus/mytopic/mysubscription2
/myservicebus/mytopic/mysubscription3

I was expecting - probably same as OP - that topics and subscribers were automatically configured as one:many.

Essentially, if multiple subscribers are configured to the same subscription, then this becomes a 'first sub wins' scenario - the same as if it were a queue.

Upvotes: 1

Sean Feldman
Sean Feldman

Reputation: 26057

Azure Service Bus is a broker with Competing Consumer pattern when it comes to retrieving messages. This is expected behaviour. What you have here is a scaled-out processing endpoint, where both are trying to process messages from the same subscription. The messages are distributed between these two competing consumers. If you need to distribute the same message to more than a single subscriber, you should have different subscription entities created and listened to.

Upvotes: 8

Related Questions