Question

Difficulty: MediumImplement Azure Service Bus Solutions

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?

  1. Receive the message with ServiceBusReceiveMode.PeekLock, process the booking, and then call CompleteMessageAsync after the database update succeeds.Answer
  2. B
    Receive the message with ServiceBusReceiveMode.ReceiveAndDelete, process the booking, and rely on the SDK to automatically restore the message if a crash occurs.
  3. C
    Receive the message with ServiceBusReceiveMode.PeekLock, process the booking, and call AbandonMessageAsync to finalize the processing after the database update succeeds.
  4. D
    Receive 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

1
Configure the receiver to use PeekLock mode (which is the default receive mode).
The message is retrieved by the receiver and locked on the Service Bus queue for the lock duration, making it invisible to other receivers.
This guarantees that if the application crashes during processing, the lock will eventually expire and the message will reappear in the queue for retry.
2
Process the reservation request and write the changes to the database.
The database contains the updated guest booking.
The message must remain locked and not deleted from the queue until we are sure the database transaction has committed successfully.
3
Call CompleteMessageAsync on the ServiceBusReceiver.
The message is deleted from the queue.
This notifies Azure Service Bus that processing was successful and that it is safe to delete the message.

Key Concept

Azure Service Bus Receive Modes
Estimated Time:1m 30s
Rate this question