Question

Difficulty: MediumImplement Azure Service Bus Solutions

You are developing a .NET background worker application that processes messages from an Azure Service Bus queue named sensor-telemetry using the Azure.Messaging.ServiceBus SDK. The messages contain environmental data that must be successfully saved to an external SQL database. If the database is offline, the message must not be lost and must remain in the queue to be retried later. Which approach should you use to receive and process the messages?

  1. Receive messages using the default PeekLock mode, process the telemetry data, and then call CompleteMessageAsync on the receiver.Answer
  2. B
    Configure the ServiceBusReceiver to use ReceiveAndDelete mode to retrieve and process the telemetry messages.
  3. C
    Use the PeekMessageAsync method on the receiver to retrieve the message, process it, and then call CompleteMessageAsync to remove it.
  4. D
    Use a system-assigned managed identity authorized with only Key Vault Secrets User permissions to manage queue message deletion.

Answer

Receive messages using the default PeekLock mode, process the telemetry data, and then call CompleteMessageAsync on the receiver.
Receiving messages using the default PeekLock mode ensures that the message is locked on the queue and invisible to other receivers during processing. Calling CompleteMessageAsync only after the database write succeeds guarantees that the message is safely removed from the queue only when processing is fully successful. If the database is offline, the operation fails before completion, the lock eventually expires, and the message returns to the queue to be processed again.

Step-by-Step Solution

1
Analyze the reliability requirement for message processing.
Identify that the message must remain in the queue for a retry if processing fails.
This rules out any modes that delete the message before successful processing is confirmed.
2
Evaluate the correct Service Bus receive mode.
Select PeekLock mode over ReceiveAndDelete.
PeekLock mode ensures the message remains locked in the queue until either completed or the lock expires, preventing message loss during processing failures.
3
Determine the proper method call for retrieving and finalizing messages.
Use ReceiveMessageAsync to lock the message, then CompleteMessageAsync after processing.
PeekMessageAsync does not lock the message and cannot be followed by a completion call, whereas calling CompleteMessageAsync after successful processing guarantees correct queue cleanup.

Key Concept

Azure Service Bus Receive Modes and SDK message lifecycle management
Rate this question