A developer is implementing a serverless function that needs to process messages from an Azure Service Bus Queue. The function must ensure that if processing fails due to a transient issue, the message is returned to the queue for a limited number of retries before being moved to a dead-letter queue. The message should also be invisible to other consumers while it's being processed. Which message receive mode and property configuration should the developer use?
- APeekLock mode with MaxDeliveryCount set to a value greater than 1
- BReceiveAndDelete mode with MaxDeliveryCount set to 1
- CPeekLock mode with the message SessionId property
- DReceiveAndDelete mode with the message TimeToLive property
Show answer & explanationAnswer & explanation
Correct answer: A. PeekLock mode with MaxDeliveryCount set to a value greater than 1
PeekLock mode ensures the message is invisible during processing and requires explicit completion or abandonment. If abandoned or if the lock expires, the message is returned to the queue. Service Bus automatically increments the DeliveryCount. When DeliveryCount exceeds the Queue's MaxDeliveryCount (which should be set > 1 for retries), the message is moved to the dead-letter queue. ReceiveAndDelete removes the message immediately, preventing retries. SessionId is for ordered processing, not retry logic.
Why the other options are wrong
- B. ReceiveAndDelete removes the message from the queue immediately upon receipt, preventing retries for transient failures. MaxDeliveryCount is not relevant in this mode.
- C. SessionId is used for message ordering and group processing, not for managing retries for transient failures. While PeekLock is used with sessions, it's the MaxDeliveryCount that governs retries.
- D. ReceiveAndDelete mode prevents retries. TimeToLive determines how long a message stays in the queue before expiring, not how many retries it gets.
Service Bus PeekLock and MaxDeliveryCount
Azure Service Bus PeekLock receive mode makes a message invisible to other consumers while it's being processed, allowing for explicit completion or abandonment. The queue's MaxDeliveryCount property dictates how many times a message can be delivered (and potentially abandoned) before it's automatically moved to the dead-letter queue.
- PeekLock ensures 'at-least-once' delivery with explicit settlement.
- Message is invisible during PeekLock duration.
- Abandoning or lock expiry returns message to queue.
- MaxDeliveryCount (on the queue) controls retry attempts before dead-lettering.
- Requires client to explicitly Complete() or Abandon() message.
Memory trick: A locked package (PeekLock) gets retried until it's delivered or discarded (MaxDeliveryCount).