title: async and await: are they bad?
body: <p>We recently developed a site based on SOA but this site ended up having terrible load and performance issues when it went under load. I posted a question related this issue here:</p>
  
  <p><a href="https://stackoverflow.com/questions/21369634/asp-net-website-becomes-unresponsive-under-load">ASP.NET website becomes unresponsive under load</a></p>
  
  <p>The site is made of an API (WEB API) site which is hosted on a 4-node cluster and a web site which is hosted on another 4-node cluster and makes calls to the API. Both are developed using ASP.NET MVC 5 and all actions/methods are based on async-await method.</p>
  
  <p>After running the site under some monitoring tools such as NewRelic, investigating several dump files and profiling the worker process, it turned out that under a very light load (e.g. 16 concurrent users) we ended  up having around 900 threads which utilized 100% of CPU and filled up the IIS thread queue!  </p>
  
  <p>Even though we managed to deploy the site to the production environment by introducing heaps of caching and performance amendments many developers in our team believe that we have to remove all async methods and covert both API and the web site to normal Web API and Action methods which simply return an Action result. </p>
  
  <p>I personally am not happy with approach because my gut feeling is that we have not used the async methods properly otherwise it means that Microsoft has introduced a feature that basically is rather destructive and unusable!</p>
  
  <p>Do you know any reference that clears it out that where and how async methods should/can be used? How we should use them to avoid such dramas?  e.g. Based on what I read on MSDN I believe the API layer should be async but the web site could be a normal no-async ASP.NET MVC site. </p>
  
  <p>Update:</p>
  
  <p>Here is the async method that makes all the communications with the API. </p>
  
  <pre><code>public static async Task&lt;T&gt; GetApiResponse&lt;T&gt;(object parameters, string action, CancellationToken ctk)
  {
          using (var httpClient = new HttpClient())
          {
              httpClient.BaseAddress = new Uri(BaseApiAddress);
  
              var formatter = new JsonMediaTypeFormatter();
  
              return
                  await
                      httpClient.PostAsJsonAsync(action, parameters, ctk)
                          .ContinueWith(x =&gt; x.Result.Content.ReadAsAsync&lt;T&gt;(new[] { formatter }).Result, ctk);
          }
      }
  </code></pre>
  
  <p>Is there anything silly with this method? Note that when we converted all method to non-async methods we got a heaps better performance. </p>
  
  <p>Here is a sample usage (I've cut the other bits of the code which was related to validation, logging etc. This code is the body of a MVC action method).</p>
  
  <p>In our service wrapper:</p>
  
  <pre><code>public async static Task&lt;IList&lt;DownloadType&gt;&gt; GetSupportedContentTypes()
  {
    string userAgent = Request.UserAgent;
    var parameters = new { Util.AppKey, Util.StoreId, QueryParameters = new { UserAgent = userAgent } };
    var taskResponse = await  Util.GetApiResponse&lt;ApiResponse&lt;SearchResponse&lt;ProductItem&gt;&gt;&gt;(
                      parameters,
                      "api/Content/ContentTypeSummary",
                      default(CancellationToken));
                      return task.Data.Groups.Select(x =&gt; x.DownloadType()).ToList();
   }
  </code></pre>
  
  <p>And in the Action:</p>
  
  <pre><code>public async Task&lt;ActionResult&gt; DownloadTypes()
      {
          IList&lt;DownloadType&gt; supportedTypes = await ContentService.GetSupportedContentTypes();
  </code></pre>