Question

Difficulty: HardImplement Azure Service Bus Solutions

You are developing a .NET background worker service that processes batch database updates from an Azure Service Bus queue named `db-updates`. The queue has a message lock duration set to 1 minute.

Each update batch takes exactly 7 minutes to process. You write the following code to initialize the processor and handle messages:

csharp
var client = new ServiceBusClient(connectionString);
var options = new ServiceBusProcessorOptions
{
ReceiveMode = ServiceBusReceiveMode.PeekLock,
AutoCompleteMessages = false
};
var processor = client.CreateProcessor("db-updates", options);

processor.ProcessMessageAsync += MessageHandler;
processor.ProcessErrorAsync += ErrorHandler;

async Task MessageHandler(ProcessMessageEventArgs args)
{
await ProcessBatchAsync(args.Message); // Takes 7 minutes
await args.CompleteMessageAsync(args.Message);
}

The `MaxAutoLockRenewalDuration` property is left at its default configuration.

What is the behavior of the application when processing a message?

  1. A
    The message is successfully completed at 7 minutes because the processor continues to renew the lock indefinitely as long as the MessageHandler task is actively running.
  2. The processor automatically renews the lock up to the default duration of 5 minutes. After 5 minutes, the lock expires and the message becomes visible to other receivers on the queue. When the handler finishes processing at 7 minutes and calls CompleteMessageAsync, a ServiceBusException is thrown.Answer
  3. C
    The lock expires after 1 minute, and the message is immediately moved to the dead-letter queue because the processor only performs automatic lock renewal when AutoCompleteMessages is set to true.
  4. D
    The message lock expires after 1 minute, and the processor immediately terminates the MessageHandler task and throws a TimeoutException to the ErrorHandler.

Answer

The processor automatically renews the lock up to the default duration of 5 minutes. After 5 minutes, the lock expires and the message becomes visible to other receivers on the queue. When the handler finishes processing at 7 minutes and calls CompleteMessageAsync, a ServiceBusException is thrown.
The correct answer is correct because the Azure Service Bus SDK's ServiceBusProcessor class has a default MaxAutoLockRenewalDuration configuration of 5 minutes. Even though the queue's lock duration is 1 minute, the processor automatically renews the lock in the background up to the maximum auto-renewal duration. Since the handler takes 7 minutes to process the message, the lock renewal stops at 5 minutes, allowing the lock to expire. When the handler attempts to call CompleteMessageAsync at 7 minutes, it fails because the active lock has been lost, causing the client to throw a ServiceBusException.

Step-by-Step Solution

1
Determine the default configurations of the ServiceBusProcessorOptions.
The default value for MaxAutoLockRenewalDuration is 5 minutes.
To evaluate lock validity over the 7-minute execution window, we need the background renewal limit.
2
Compare the task duration against the auto-renewal limit.
The task takes 7 minutes, which exceeds the 5-minute renewal threshold.
Since the task duration is longer than the renewal limit, the background thread will cease renewing the lock after 5 minutes.
3
Evaluate the state of the message lock at completion time.
At the 7-minute mark, the message lock has expired, causing the message to be released back to the queue, and calling CompleteMessageAsync throws a ServiceBusException.
A client cannot complete a message if its lock has already expired or been acquired by another receiver.

Key Concept

ServiceBusProcessor automatic lock renewal limits and manual message settlement.
Rate this question