question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sharing Bulkhead policy capacity across multiple HttpClients from HttpClientFactory

See original GitHub issue

Summary: What are you wanting to achieve? This might be a simple question but… I am trying to limit the amount of concurrent outbound requests using multiple typed Http clients in .Net Core 2.2. It is not the amount of threads/cpu usage etc I am trying to limit, the goal is to prevent reaching the outgoing connections limit in Azure Webapps. The API itself can under heavy load make a very high amount of outgoing requests at the same time, limiting connection reuse.



What code or approach do you have so far?
I have looked at the Bulkhead policy and I want to check if that is the correct usage of that policy.

For example in Startup.cs:

var bulkheadPolicy = Policy.BulkheadAsync<HttpResponseMessage>(1000, int.MaxValue);

services.AddHttpClient<ITestService1, TestService1>(client =>
{
     client.BaseAddress = new Uri("example1");
 })
.AddPolicyHandler(bulkheadPolicy)
.AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(300)));

services.AddHttpClient<ITestService2, TestService2>(client =>
{
     client.BaseAddress = new Uri("example2");
 })
.AddPolicyHandler(bulkheadPolicy)
.AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(300)));

Will this configuration limit the concurrent outgoing calls to 1000 for both HttpClients combined? And is it the correct order (bulkhead first)?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
reisenbergercommented, Feb 20, 2019

You don’t really want to handle a failure you want to implement a throttle I think of Polly in terms of deciding what to do, or how to cope, when something goes wrong.

Hi all. Yes, Polly encompasses resilience strategies beyond pure fault-handling for a while now. Polly’s wiki page on fault-handling vs proactive resilience engineering discusses and slots each policy into this broader context.

@emilssonn Yes, a bulkhead policy is an extremely simple within-process parallelism throttle. Note: bulkhead policy, being based on SemaphoreSlim, will clearly operate as a per-VM-instance throttle only; there is no distributed/shared state across VM instances. If your App Service plan is such that your outgoing connections limit will ever be shared across VMs, bulkhead policy as-now will not be sufficient to govern this across VMs.


Notwithstanding the above caveat, I’ll answer the questions about bulkhead policy scoping and sequencing with HttpClientFactory, for completeness:

@emilssonn Your scoping of the bulkheadPolicy instance in the original post is correct for the goal stated about typed clients: to govern the separate call-streams via TestService1 and TestService2 within the same overall parallelism limit, share the same bulkhead policy instance across both, as you have done, and as we also discuss briefly in the HttpClientFactory doco here and here.

However, you want to sequence the bulkhead and the retry in the other order. You should configure:

.AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(300))
.AddPolicyHandler(bulkheadPolicy)

See the policy sequencing recommendations within PolicyWrap for explanation. You don’t (I would assume) want any of the bulkhead’s capacity occupied by waiting for the next try; you want all the bulkhead’s capacity dedicated to placing outbound calls. So the bulkhead should be ‘inside’ the wait-and-retry, only governing the downstream call, not the waits. For more on how policy configuration order on HttpClientFactory translates into execution order/wrapping, see our diagrams in the doco.

1reaction
emilssonncommented, Feb 20, 2019

@reisenberger and @RichardHowells thank you for the help!

It looks like the bulkhead policy will work for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Polly and Multiple HttpClients
I want to use Polly in conjunction with my HttpClientFactory (in my C# .NET 5.0 project). But the problem I have is that...
Read more >
3 Ways To Increase App Reliability With Polly - CodeWithStu
Learn how to use retries with decorrelated jitter, bulkhead and circuit breaker patterns to increase reliability in your applications with ...
Read more >
ASP.NET Core 2.1-preview1: Introducing HTTPClient factory
We will be building a package that allows easy integration of Polly policies with HttpClients created by the HttpClient factory.
Read more >
Polly 7.2.4
Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead ...
Read more >
Make HTTP requests using IHttpClientFactory in ASP.NET ...
It allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found