5

The order in which Polly policies are encapsulated change the final result. Which is the best order if I want to use the following policies? This is the best order I could think of: the retries should be submitted to the bulkhead limits, and the circuit breaking is a lower-level policy that follows the timeout policy. Makes sense?

services.AddHttpClient<IService, Service>()
                .AddPolicyHandler(PolicyConfig.RetryPolicy);     
                .AddPolicyHandler(PolicyConfig.BulkheadPolicy)
                .AddPolicyHandler(PolicyConfig.CircuitBreakingPolicy)
                .AddPolicyHandler(PolicyConfig.TimeoutPolicy)
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • https://github.com/App-vNext/Polly they have steps that seem to be ordered in the way you should handle the policies. – Trevor Oct 28 '20 at 20:42
  • Makes sense to me! – Theodor Zoulias Oct 29 '20 at 05:11
  • Here is the relevant doc page: [PolicyWrap - Usage recommendations](https://github.com/App-vNext/Polly/wiki/PolicyWrap#usage-recommendations) – Theodor Zoulias Oct 29 '20 at 08:13
  • @AlessandraAnyzewski What if you change the `Bulkhead` and `CircuitBreaker`? In that way if the BH's queue is full (so `BulkheadRejectedException` will be thrown) then CB can break and it will prevent further retry attempts. During the CB's delay the BH's queue might shrink, so when it HalfOpens then it is great chance to be able push new request attempt into the queue. – Peter Csala Oct 29 '20 at 08:19
  • Great reference @TheodorZoulias – Alessandra Anyzewski Oct 29 '20 at 18:31
  • Your suggestion makes sense @PeterCsala in the case I do not allow queueing in the bulkhead policy. I think for my system it makes more sense to allow queueing. So I guess it will not make much difference interchanging them. Do you agree? – Alessandra Anyzewski Oct 29 '20 at 18:34
  • @AlessandraAnyzewski Bulkhead can also throw `BulkheadRejectedException` if there is no queue (its size is 0) and the concurrency threshold has been reached. So, depending on your CircuitBreaker's `Handle` and `Or` triggers it may or may not make sense to change the order. – Peter Csala Oct 30 '20 at 08:35

1 Answers1

-1

The policies are applied in the order defined, i.e. the first policy is the outer one, all the way to the last one (the innermost one).

Relevant documentation:

https://github.com/App-vNext/Polly/wiki/Polly-and-HttpClientFactory#what-order-are-multiple-policies-applied-in

silkfire
  • 24,585
  • 15
  • 82
  • 105