A hotel reservation system uses an Azure Service Bus queue named `reservation-bookings` to process guest bookings. The processing application must ensure that booking requests are not lost if the application crashes during database updates.
You write a C# worker service using the `Azure.Messaging.ServiceBus` SDK to process these messages.
Which approach should you use to guarantee at-least-once delivery and processing of the booking messages?
- Receive the message with ServiceBusReceiveMode.PeekLock, process the booking, and then call CompleteMessageAsync after the database update succeeds.Answer
- BReceive the message with ServiceBusReceiveMode.ReceiveAndDelete, process the booking, and rely on the SDK to automatically restore the message if a crash occurs.
- CReceive the message with ServiceBusReceiveMode.PeekLock, process the booking, and call AbandonMessageAsync to finalize the processing after the database update succeeds.
- DReceive the message with ServiceBusReceiveMode.ReceiveAndDelete, process the booking, and call CompleteMessageAsync to release the message lock after the database update succeeds.
Answer
Receive the message with ServiceBusReceiveMode.PeekLock, process the booking, and then call CompleteMessageAsync after the database update succeeds.
The correct approach is to use the PeekLock receive mode. In this mode, the message is locked during processing. Calling CompleteMessageAsync after the database update succeeds ensures the message is only deleted after the processing is fully complete. If a crash occurs before calling CompleteMessageAsync, the lock expires and the message is returned to the queue, ensuring at-least-once delivery.
Step-by-Step Solution
Key Concept
Azure Service Bus Receive Modes
Estimated Time:1m 30s