title: C# Concurrent multiple in Parallel HttpClient calls with timeout
body: <p>I have an API that must call in parallel <strong>4 HttpClients</strong> supporting a concurrency of <strong>500 users per second (all of them calling the API at the same time)</strong></p>
  
  <p>There must be a strict timeout letting the API to return a result even if not all the HttpClients calls have returned a value.<br>
  The endpoints are external third party APIs and I don't have any control on them or know the code.<br>
  I did extensive research on the matter, but even if many solution works, I need the one that consume less CPU as possible since I have a low server budget.</p>
  
  <p>So far I came up with this:</p>
  
  <pre><code>var conn0 = new HttpClient
  {
      Timeout = TimeSpan.FromMilliseconds(1000),
      BaseAddress = new Uri("http://endpoint")
  };
  var conn1 = new HttpClient
  {
      Timeout = TimeSpan.FromMilliseconds(1000),
      BaseAddress = new Uri("http://endpoint")
  };
  var conn2 = new HttpClient
  {
      Timeout = TimeSpan.FromMilliseconds(1000),
      BaseAddress = new Uri("http://endpoint")
  };
  var conn3 = new HttpClient
  {
      Timeout = TimeSpan.FromMilliseconds(1000),
      BaseAddress = new Uri("http://endpoint")
  };
  
  var list = new List&lt;HttpClient&gt;() { conn0, conn1, conn2, conn3 };
  var timeout = TimeSpan.FromMilliseconds(1000);
  var allTasks = new List&lt;Task&lt;Task&gt;&gt;();
  
  //the async DoCall method just call the HttpClient endpoint and return a MyResponse object
  foreach (var call in list)
  {
      allTasks.Add(Task.WhenAny(DoCall(call), Task.Delay(timeout)));
  }
  var completedTasks = await Task.WhenAll(allTasks);
  var allResults = completedTasks.OfType&lt;Task&lt;MyResponse&gt;&gt;().Select(task =&gt; task.Result).ToList();
  return allResults;
  </code></pre>
  
  <p>I use WhenAny and two tasks, one for the call, one for the timeout.If the call task is late, the other one return anyway.</p>
  
  <p>Now, this code works perfectly and everything is async, but I wonder if there is a better way of achieving this.<br>
  Ever single call to this API creates lot of threads and with 500concurrent users it needs an avarage of 8(eight) D3_V2 Azure 4-core machines resulting in crazy expenses, and the higher the timeout is, the higher the CPU use is.</p>
  
  <p>Is there a better way to do this without using so many CPU resources (maybe Parallel Linq a better choice than this)?</p>
  
  <p>Is the HttpClient timeout alone sufficient to stop the call and return if the endpoint do not reply in time, without having to use the second task in <code>WhenAny</code>?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <ul>
  <li>The endpoints are third party APIs, I don't know the code or have any control, the call is done in JSON and return JSON or a string.</li>
  <li>Some of them reply after 10+ seconds once in a while or got stuck and are extremely slow,so the timeout is to free the threads and return even if with partial data from the other that returned in time.</li>
  <li>Caching is possible but only partially since the data change all the time, like stocks and forex real time currency trading.</li>
  </ul>