1. Learning the Basics of the Polly Framework Flashcards
What two categories can Polly resilience strategies be broken into?
- Reactive
2. Proactive
What is a reactive resilience strategy?
A resilience strategy that is triggered in response to a current and perhaps transient issue, like a server fault.
What is a proactive resilience strategy?
A resilience strategy that monitors for ongoing problems and attempts to stabilise the system when facing degraded quality or longer outages in remote services.
What reactive resilience strategies does Polly offer us?
- The
Retry
Policy - The
Wait and Retry
Policy - The
Circuit Breaker
Policy - The
Fallback
Policy
What does the Retry
policy do?
Immediately retries the request some specified number of times without any delay.
What does the Wait and Retry
policy do?
Works like the Retry
but introduces a delay before each retry.
What does the Circuit Breaker
policy do?
Stops all requests to an endpoint if some failure threshold is reached.
What does the Fallback
policy do?
Returns some specified default value if the request fails
ps. Often used in conjunction with other policies
What can the use of delegates with the policies offered by Polly allow us to do?
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.
What proactive resilience strategies does Polly offer us?
- The
Timeout
Policy - The
Caching
Policy - The
Bulkhead Isolation
Policy
What does the Timeout
policy do?
Set the timeout for a request rather than waiting for default as implemented by the HttpClient.
What does the Caching
policy do?
Cache responses for a period of time and then channel future requests to the cache rather than the remote service.
What does the Bulkhead Isolation
policy do?
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.
What is a resilience framework?
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.
When does a Retry
policy retries the request?
Immediately after failure.
What does the Wait and Retry
policy allow us to define?
Backoff between entries, allowing us to increase the backoff of each subsequent request.
How does the Wait and Retry
policy work for handling a request that failed?
It will decide whether to retry.
- If it decides to retry, it waits a specified amount of time before tries to send the request again.
- If no retries should be performed, it returns the response.
What parameters do the WaitAndRetry
method take?
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));
What benefit does the Wait and Retry
policy offer?
It gives the remote service time to recover, and in some scenarios, will make the retry more likely to succeed.
What is idempotence in the context of web requests and how does it relate to Polly policies?
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 POST
s to be. So, be careful when retrying POST
s.
What does the use of delegates allow us to do when used with policies?
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(); } });
What does the Fallback
policy do?
It allows us to return some meaningful default if all else has failed.
How does the Fallback
policy work when used in conjunction with a HTTP request?
If the request succeeds, the response is returned as normal. If the request fails, the Fallback
returns some default response.
What’s a common policy to use in conjunction with the Fallback
policy?
The Retry
policy — if the retries fail, use the Fallback
to return a default.