Azure Service Bus - Limit number of messages process by consumer

Kasun Tharaka 76 Reputation points
2023-06-27T07:13:14.5333333+00:00

From an azure service bus queue, a consumer should only read and process defined number of messages per minute. What is the best way to achieve this ?

My current thought is to maintain a counter to track the processed messages and verify it in the ProcessMessageAsync() method before proceeding. If the limit is reached, the consumer should wait until the current minute completes, reset the counter, and continue processing.

Any proper way to do this ?

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
700 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Chathura Munasinghe 170 Reputation points
    2023-06-27T07:46:21.6233333+00:00

    Your current thought is a good way to achieve this. You can maintain a counter to track the processed messages and verify it in the ProcessMessageAsync() method before proceeding. If the limit is reached, the consumer should wait until the current minute completes, reset the counter, and continue processing.

    Here is an example of how you can implement this in code:

    Code snippet

    private int processedMessages = 0;
    
    public async Task ProcessMessageAsync(Message message)
    
    {
    
        if (processedMessages >= messageLimit)
    
        {
    
            // Wait until the current minute completes
    
            DateTime now = DateTime.Now;
    
            DateTime nextMinute = now.AddMinutes(1);
    
            await Task.Delay(nextMinute - now);
    
            // Reset the counter
    
            processedMessages = 0;
    
        }
    
        // Process the message
    
        await ProcessMessage(message);
    
        // Increment the counter
    
        processedMessages++;
    
    }
    

    This code will ensure that the consumer only processes a maximum of messageLimit messages per minute. If the limit is reached, the consumer will wait until the next minute before continuing to process messages.

    Here are some other things to consider when implementing this:

    • You should use a consistent way to measure time. For example, you could use the DateTime: https://docs.microsoft.com/en-us/dotnet/api/system.datetime class.
    • You should make sure that the consumer is able to handle the case where the queue contains more than messageLimit messages. For example, you could implement a queue overflow policy.
    • You should monitor the consumer to make sure that it is not exceeding the message limit.

    I hope this helps! Let me know if you have any other questions.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.