Question

Difficulty: HardImplement Azure Service Bus Solutions

You are developing a C# background service that processes flight ticket booking transactions from an Azure Service Bus queue. Each booking transaction involves multiple external API calls to airlines and payment gateways, which takes up to 90 seconds to complete under peak load. If the service crashes or loses connectivity while processing a booking, the transaction message must not be lost and must be made available for reprocessing by another service instance. You need to configure the `ServiceBusProcessor` using the C# Azure SDK (`Azure.Messaging.ServiceBus`). Which configuration settings must you apply to meet these requirements?

  1. Configure ServiceBusProcessorOptions with ReceiveMode set to ServiceBusReceiveMode.PeekLock and MaxAutoLockRenewalDuration set to 2 minutes.Answer
  2. B
    Configure ServiceBusProcessorOptions with ReceiveMode set to ServiceBusReceiveMode.ReceiveAndDelete and MaxAutoLockRenewalDuration set to 2 minutes.
  3. C
    Configure ServiceBusProcessorOptions with ReceiveMode set to ServiceBusReceiveMode.PeekLock and AutoCompleteMessages set to false, without calling CompleteMessageAsync in the message handler.
  4. D
    Configure ServiceBusProcessorOptions with ReceiveMode set to ServiceBusReceiveMode.ReceiveAndDelete and AutoCompleteMessages set to true.

Answer

Configure ServiceBusProcessorOptions with ReceiveMode set to ServiceBusReceiveMode.PeekLock and MaxAutoLockRenewalDuration set to 2 minutes.
The correct configuration uses PeekLock mode to guarantee that the message is not lost if processing fails, and sets MaxAutoLockRenewalDuration to 2 minutes to ensure the processor maintains the lock for the duration of the 90-second operation.

Step-by-Step Solution

1
Select the correct ReceiveMode to prevent message loss on failure.
ServiceBusReceiveMode.PeekLock must be selected so that the message remains in the queue and is only marked for deletion upon successful settlement.
ReceiveAndDelete deletes the message immediately upon receipt, which risks losing the message if the application crashes during execution.
2
Ensure the message lock duration covers the maximum processing time of 90 seconds.
Configure MaxAutoLockRenewalDuration to a duration (e.g., 2 minutes) greater than the maximum expected processing time.
By default, the queue lock duration is shorter (often 30 seconds). If the lock expires while the process is still running, another worker instance might retrieve and try to process the same message, leading to duplicate processing.
3
Ensure appropriate completion settlement behavior.
Leave AutoCompleteMessages to its default value of true (or manually complete the message using CompleteMessageAsync if set to false).
Failing to complete the message means it will never be settled successfully, leading to reprocessing loops and eventual dead-lettering.

Key Concept

Message locking and settlement modes in Azure Service Bus SDK
Rate this question