0

Base topic: Does Parallel.ForEach use threads from the ASP.NET thread pool?

I am wondering if microservice is deployed to multi-instance clustered containerized environment (like k8s) and uses Parallel.For, Parallel.ForEach, Task.WaitAll,...), how TPL parallel code affects scale-out (not scale-up) abilities?

It is understandable that one request is processed faster if TPL is used, but other requests may suffer if there is only one instance (webserver) for microservice. What about multiple instances of the same microservice (inside k8s clustered environment) and internal load balancer before each microservice? Internal load balancer should transfer a new request to different instance of microservice if the first instance is currently overloaded. Therefore scale-out abilities should not suffer when using TPL, right?

Thank you for your explanations.

It's hard to test potential drawbacks in development time.

Leon
  • 1
  • Do you intend to use the `Parallel` class for parallelizing [CPU-bound](https://stackoverflow.com/questions/868568/what-do-the-terms-cpu-bound-and-i-o-bound-mean) work, or I/O-bound work? – Theodor Zoulias Nov 10 '22 at 11:55
  • Mostly I/O work with light processing (math addition, subtract, if-else based on calculated values,...). Executing multiple HttpClients with Task.WhenAll(). – Leon Nov 10 '22 at 12:12
  • Are there available asynchronous APIs for your worked, or you are forced to use synchronous APIs and block threads? In case you can use async APIs, look at the [`Parallel.ForEachAsync`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallel.foreachasync) method. As a side note, regarding the phrase *"multiple HttpClients"*, the `HttpClient` class is intended to be instantiated [once](https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client#create-and-initialize-httpclient), and reused throughout the life of an application. – Theodor Zoulias Nov 10 '22 at 12:21
  • All have async APIs. The main reason is parallel calculations for each entity in BlockingCollection in memory. For many HttpClient instances, I have a reason - each instance should have a different URL and authentication token data and headers. – Leon Nov 10 '22 at 13:12
  • So scale-out ability when using TPL inside asp.net web application is not affected? – Leon Nov 16 '22 at 08:54
  • I have no experience with microservices, so I can't answer the question. I asked for clarifications on behalf of other people who might know about this stuff. I you want to bump the question, try editing it. For example you could include in the question's body your latest comments. – Theodor Zoulias Nov 16 '22 at 10:12

0 Answers0