1

I am working on a retry logic in a .NET Core WebAPI project. I'm using there polly where we have WaitAndRetryAsync, WaitAndRetry methods.

What is the difference between WaitAndRetryAsync vs WaitAndRetry?
And which one should be used when?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
itaustralia
  • 127
  • 1
  • 12
  • 1
    One is asynchronous the other is not. Use the async version in an async method – Ryan Wilson Jul 24 '22 at 02:30
  • synchronous operations tasks are performed one at a time and only when one is completed, the following is unblocked. In other words, you need to wait for a task to finish to move to the next one. In asynchronous operations, on the other hand, you can move to another task before the previous one finishes. This way, with asynchronous programming you’re able to deal with multiple requests simultaneously, thus completing more tasks in a much shorter period of time. So, in case on async while waiting. it can perform other task 3 (see below) Task 1 Task 2 //asyn await retry Task 3 Task 4 – itaustralia Jul 24 '22 at 02:40

1 Answers1

3

When you use Polly you have to know upfront a couple of information about your to-be-decorated method/function to be able to define the policy in the correct way.

In case retry you can use 16 different methods to cover different use cases retry methods retry t methods

Method or Function

If your to be decorated method does not return any value then use Policy.Handle... retry methods for methods

but if it returns with a specific type then prefer Policy<T>.Handle... retry methods for functions

Sync or Async

If your to-be-decorated method/function is synchronous then prefer those methods which does not have Async suffix
sync retry methods sync retry functions

But if you want to decorate an async method/function then prefer those with the Async suffix

async retry methods async retry functions

With or without penalty

If you don't want to wait between retry attempts (so you want to kick off a new attempt immediately after the previous failed one) then prefer those methods which starts with Retry

policy without penalty policy t without penalty

If you do want to wait between attempts then use one of those methods where you have the Wait prefix

policy with penalty policy t with penalty

Limited or unlimited retry attempts

If you know that your to-be-decorated method/function will eventually succeed but don't know after how many retry attempts then prefer those methods where the method name include Forever

policy unlimited attempts policy t unlimited attempts

If you don't know whether your to-be-decorated method/function will succeed eventually for sure and you don't want to wait indefinitely then prefer those methods which does not include the word Forever so you can specific the maximum retry attempts

policy limited attempts policy t limited attempts

Peter Csala
  • 17,736
  • 16
  • 35
  • 75