Soru

Zorluk: Çok zorImplement Azure Service Bus Solutions

You are developing a .NET background worker service that processes critical payment transactions from an Azure Service Bus queue. The processing of each payment involves calling a third-party gateway, which can take up to 2 minutes during peak hours. The Service Bus queue is configured with a default LockDuration of 30 seconds and a MaxDeliveryCount of 5.

You must ensure that:
1. Messages are never lost if the worker service crashes or restarts mid-transaction.
2. Messages are not processed by multiple workers simultaneously if processing exceeds 30 seconds.
3. Message processing failures due to transient errors are immediately released back to the queue for retry up to the maximum delivery count.

You use the Azure.Messaging.ServiceBus SDK to initialize the processor with the following code:

csharp
var client = new ServiceBusClient(connectionString);
var options = new ServiceBusProcessorOptions
{
ReceiveMode = [ReceiveMode],
AutoCompleteMessages = [AutoComplete],
MaxAutoLockRenewalDuration = [MaxAutoLockRenewal]
};
ServiceBusProcessor processor = client.CreateProcessor(queueName, options);

processor.ProcessMessageAsync += async args =>
{
try
{
await ProcessPaymentWithRetryAsync(args.Message);
[CompleteMessage]
}
catch (Exception)
{
[HandleException]
}
};

Which set of configuration values and code segments should you use to meet these requirements?

  1. [ReceiveMode] = ServiceBusReceiveMode.PeekLock
    [AutoComplete] = false
    [MaxAutoLockRenewal] = TimeSpan.FromMinutes(5)
    [CompleteMessage] = await args.CompleteMessageAsync(args.Message);
    [HandleException] = await args.AbandonMessageAsync(args.Message);
    Cevap
  2. B
    [ReceiveMode] = ServiceBusReceiveMode.ReceiveAndDelete
    [AutoComplete] = true
    [MaxAutoLockRenewal] = TimeSpan.FromMinutes(5)
    [CompleteMessage] = // No-op
    [HandleException] = // No-op
  3. C
    [ReceiveMode] = ServiceBusReceiveMode.PeekLock
    [AutoComplete] = false
    [MaxAutoLockRenewal] = TimeSpan.Zero
    [CompleteMessage] = await args.CompleteMessageAsync(args.Message);
    [HandleException] = await args.AbandonMessageAsync(args.Message);
  4. D
    [ReceiveMode] = ServiceBusReceiveMode.PeekLock
    [AutoComplete] = true
    [MaxAutoLockRenewal] = TimeSpan.FromMinutes(5)
    [CompleteMessage] = await args.CompleteMessageAsync(args.Message);
    [HandleException] = await args.AbandonMessageAsync(args.Message);

Cevap

Use PeekLock mode, disable auto-complete, set the automatic lock renewal duration to 5 minutes, explicitly complete the message upon success, and explicitly abandon the message upon failure.
The configuration using PeekLock receive mode, AutoCompleteMessages set to false, and MaxAutoLockRenewalDuration set to 5 minutes is correct. PeekLock ensures that messages remain on the queue until explicitly completed, preventing message loss. Setting MaxAutoLockRenewalDuration to 5 minutes ensures the lock remains active for the duration of the 2-minute payment process. Explicitly completing the message on success and abandoning it on exception ensures correct settlement and immediate retry in case of failure.

Adım Adım Çözüm

1
Analyze reliability requirements to determine the correct ServiceBusReceiveMode.
ServiceBusReceiveMode.PeekLock must be used. Using ReceiveAndDelete deletes messages immediately upon retrieval, resulting in data loss if the worker crashes before processing finishes.
Ensures that messages are not lost and can be retried in case of application failure.
2
Determine the lock renewal duration based on the processing time.
MaxAutoLockRenewalDuration must be set to a duration greater than the maximum processing time (e.g., TimeSpan.FromMinutes(5)).
Since payment processing takes up to 2 minutes but the queue lock duration is only 30 seconds, auto-renewal prevents other workers from processing the active message concurrently.
3
Establish the settlement model by configuring AutoCompleteMessages and handler code.
Set AutoCompleteMessages to false, explicitly complete the message using CompleteMessageAsync on success, and explicitly abandon it using AbandonMessageAsync on exception.
Explicitly abandoning the message on failure ensures it is immediately unlocked and made available for retry rather than waiting for the remaining lock duration to expire. Redundant manual settlement when AutoCompleteMessages is true causes runtime exceptions.

Anahtar Kavram

Azure Service Bus message receive modes, lock renewal configuration, and message settlement lifecycle.
Tahmini Süre:3m 0s
Bu soruyu puanla