Wednesday, September 30, 2020

RetryOptions in Azure Durable Functions

In this post, let's see how we can handle Retries on Activity Functions or Orchestration Functions in the Durable Functions Framework.

When you are calling an Activity Functions or another Orchestration Function from IDurableOrchestrationContext, there 2 methods to use CallActivityWithRetryAsync and CallSubOrchestratorWithRetryAsync which you can use that accepts a parameter of type RetryOptions.

So basically when you are calling an Activity Function or another Orchestration Function you can do something like below.

var retryOptions = new RetryOptions(TimeSpan.FromSeconds(10), 3)
{
    Handle = exception => exception.InnerException is MyException myException
};
var someReturn = await context.CallActivityWithRetryAsync<SomeReturn>("MyActivity", retryOptions, someInput);

So here, if MyActvity throws an exception of type MyException, we will be retrying again in 10 seconds and maxNumberOfAttempts will be 3. One of the important things to note for the Handle delegate is, you need access exceptions' InnerException if you want to get the actual exception. The exception would always be FunctionFailedException.

There is another interesting property that you can use in RetryOptions which is BackoffCoefficientBackoffCoefficient lets you increment the waiting time in subsequent retries. Default is 1, for an example if you do something like below,

var retryOptions = new RetryOptions(TimeSpan.FromSeconds(10), 3)
{
    BackoffCoefficient = 2,
    Handle = exception => exception.InnerException is MyException myException
};
var someReturnValue= await context.CallActivityWithRetryAsync<SomeReturn>("MyActivity", retryOptions, someInput);

The first retry will be in 10 seconds, the second retry will be in 20 seconds (after the first retry fail) and the final retry will be in 30 seconds (after the second retry fail). 

These are the different options available in RetryOptions that you can use to get the best retry experience.

  • BackoffCoefficient: Gets or sets the backoff coefficient.
  • FirstRetryInterval: Gets or sets the first retry interval.
  • Handle: Gets or sets a delegate to call on an exception to determine if retries should proceed.
  • MaxNumberOfAttempts: Gets or sets the max number of attempts.
  • MaxRetryInterval: Gets or sets the max retry interval.
  • RetryTimeout: Gets or sets the timeout for retries.

So hope this helps.

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment