I am very new to polly. I did a bit of research but still could not find out if/how it is possible to use Polly in an elegant way to use a chached value only if a request fails. Example:
Service A wants to get data from a service B via http. I always want to get the latest data (a cache policy of a few minutes would be fine). But if the service B is down I want to be able to use the cached data as long as the service is not available.
Just using the Polly cache does not seem to solve the problem. But when using the cache only in a fallback situation, it is not filled with the latest values as it was bypassed until the failure happened. Do you get my point? THX
Whenever we are talking about resilience strategies then we mainly referring to a combination of resilient patterns. In other words in order to solve your problem you need to chain some polly polices (in the right order) to be able to tackle it. (In polly nomenclature it is called wrapping)
First let's collect the patterns:
I would suggest to use the wording temporarily unavailable / unreachable because that means you want to use a resilient strategy to overcome of a transient failure. So in order to detect that a service is unavailable you can use the Circuit Breaker. It works in the following way:
This component acts as a proxy and examines the result (if any) of the requests. After a predefined number of successive / subsequent failures it will block the communication for a given time period against the downstream system. When that time period elapsed then it will allow you give it a try and see whether or not the new request is succeeding or failing.
It's worth mentioning here that Polly will throw a BrokenCircuitException
whenever the proxy is blocking the outgoing requests in order to satisfy the fail fast principle.
Whenever you are about to cache the result of an outbound request then you should consider the following concerns:
By reviewing these questions you can decide whether or not to use caching.
This simple pattern provides you the ability to use something as substitute. So in your case whenever the server is unavailable then fallback to the cache.
Now let's put them together.
The ordering is important here because Polly using escalation to chain policies. When the inner policy fails then it escalates that to the outer policy.
In your case the order would be like this (from outer to inner): Fallback >> Cache >> Circuit Breaker
Of course, you can even enhance that by using Timeout and Retry as well. In that case the ordering would look like this: Fallback >> Cache >> Retry >> Circuit Breaker >> Timeout
For further details about PolicyWrap please read their wiki page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With