1. Learning the Basics of the Polly Framework Flashcards

1
Q

What two categories can Polly resilience strategies be broken into?

A
  1. Reactive

2. Proactive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a reactive resilience strategy?

A

A resilience strategy that is triggered in response to a current and perhaps transient issue, like a server fault.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a proactive resilience strategy?

A

A resilience strategy that monitors for ongoing problems and attempts to stabilise the system when facing degraded quality or longer outages in remote services.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What reactive resilience strategies does Polly offer us?

A
  1. The Retry Policy
  2. The Wait and Retry Policy
  3. The Circuit Breaker Policy
  4. The Fallback Policy
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the Retry policy do?

A

Immediately retries the request some specified number of times without any delay.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the Wait and Retry policy do?

A

Works like the Retry but introduces a delay before each retry.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the Circuit Breaker policy do?

A

Stops all requests to an endpoint if some failure threshold is reached.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the Fallback policy do?

A

Returns some specified default value if the request fails

ps. Often used in conjunction with other policies

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What can the use of delegates with the policies offered by Polly allow us to do?

A

Run custom code as part of the policy. For example, we can check if the cause of a failure was a specific error and look to resolve it before retrying the original request.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What proactive resilience strategies does Polly offer us?

A
  1. The Timeout Policy
  2. The Caching Policy
  3. The Bulkhead Isolation Policy
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the Timeout policy do?

A

Set the timeout for a request rather than waiting for default as implemented by the HttpClient.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the Caching policy do?

A

Cache responses for a period of time and then channel future requests to the cache rather than the remote service.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the Bulkhead Isolation policy do?

A

Manage the load on your own application by limiting the number of concurrently executing requests and limiting the size of the queue of requests that are waiting to execute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a resilience framework?

A

There is no agreed-upon definition, but you can think of it as a set of libraries that help an application to recover from transient or longer failures in services or infrastructure upon which it depends when recovery is possible, and that facilitates graceful degradation of your application when it is not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When does a Retry policy retries the request?

A

Immediately after failure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the Wait and Retry policy allow us to define?

A

Backoff between entries, allowing us to increase the backoff of each subsequent request.

17
Q

How does the Wait and Retry policy work for handling a request that failed?

A

It will decide whether to retry.

  1. If it decides to retry, it waits a specified amount of time before tries to send the request again.
  2. If no retries should be performed, it returns the response.
18
Q

What parameters do the WaitAndRetry method take?

A

It takes the number of times to retry; and a lambda function that takes in an int parameter which tells us the retry count and returns a TimeSpan that determines how long to wait before retrying.

...
    .WaitAndRetryAsync(3,
        retryAttempt => TimeSpan.FromSeconds(
             Math.Pow(2, retryAttempt) / 2));
19
Q

What benefit does the Wait and Retry policy offer?

A

It gives the remote service time to recover, and in some scenarios, will make the retry more likely to succeed.

20
Q

What is idempotence in the context of web requests and how does it relate to Polly policies?

A

A request is idempotent if it can be called any number of times and the result is the same each time.

GET, PUT, and DELETE are usually designed to be idempotent, but you should not expect POSTs to be. So, be careful when retrying POSTs.

21
Q

What does the use of delegates allow us to do when used with policies?

A

It allows us to execute arbitrary code before the request is retried — log a failure, call some other method, etc.

/...
.RetryAsync(3,
   onRetry: (httpResponseMessage, _) =>
   {
      if (httpResponseMessage.Result.StatusCode == HttpStatusCode.Unauthorized)
      {
         PerformReauthoripzation();
      }
   });
22
Q

What does the Fallback policy do?

A

It allows us to return some meaningful default if all else has failed.

23
Q

How does the Fallback policy work when used in conjunction with a HTTP request?

A

If the request succeeds, the response is returned as normal. If the request fails, the Fallback returns some default response.

24
Q

What’s a common policy to use in conjunction with the Fallback policy?

A

The Retry policy — if the retries fail, use the Fallback to return a default.

25
Q

What does the Fallback policy offer us when it comes to executing arbitrary code?

A

Like Retry and Wait and Retry, Fallback offers a delegate that can be called before the Fallback’s behavior clause is executed.

26
Q

When using the HttpClient to call a remote service, what will it do if the remote service doesn’t respond within a certain time?

A

Throws an HttpRequestException.

27
Q

How does the workflow for simply handling a HttpClient timeout with Polly look like?

A

A request is made. If a HttpClient timeout occurs, we check if we should perform a retry. If we should, we make the request again; if we shouldn’t, we’ll propagate that timeout exception.

28
Q

What is the default time to timeout when making a request with the HttpClient?

A

100 seconds.

29
Q

How does the Timeout policy work?

A
  1. You specify a period you’re willing to wait for a request to respond.
  2. You send that request
    3a. If no response is received within the specified time, the Timeout policy throws a TimoutRejectedException. At this point, you can also pass a cancellation token to the HttpClient, allowing it to cancel the request and release any resources it’s holding.
    3b. If you did receive a response within the specified time period, normal operation resumes and the response is returned to the caller.
30
Q

What methods of the Policy type can we use to set a timeout policy in Polly and what parameters do they take?

A

The Timeout or the TimeoutAsync method. They take an integer parameter indicating the number of seconds to wait before timing out.

var policy = Policy.TimeoutAsync(1);
31
Q

What methods of the Policy type can we use to set a fallback policy in Polly and what parameters do they take?

A

The Fallback or the FallbackAsync method in conjunction with the HandleResult method. It takes a type T parameter for the default to return.

var p = 
Policy.HandleResult(
        r => !r.IsSuccessStatusCode)
    .FallbackAsync(
        // some default to return...
    )
32
Q

What methods of the Policy type can we use to set a retry policy in Polly and what parameters do they take?

A

The Retry or the RetryAsync method in conjunction with the HandleResult method. It takes in an integer parameter indicating the number of times to retry the request before giving up.

Policy.HandleResult(
        r => !r.IsSuccessStatusCode)
    .RetryAsync(3);
33
Q

How do you run a Polly policy and what argument must you supply in order to do so?

A

By calling the Execute method for synchronous policies or the ExecuteAsync for the asynchronous ones. You supply it with an anonymous function to execute, which must return the type specified in the HandleResult clause.

policy.ExecuteAsnyc(
    () => httpClient.GetAsync(requestEndpoint));