title: ASP.NET and async - how it works?
body: <p>I know that is a common question, but I've read a kiloton of articles and feel confused. And now I think that it would be better not to read them at all )).</p>
  
  <p>So, how ASP.NET works (only about threads):</p>
  
  <ol>
  <li>http request is served by thread from the thread pool.</li>
  <li>while request is processing this thread is busy, because request is processing inside exactly this thread.</li>
  <li>when request processing finishes, the thread returns to thread pool, server sends a response.</li>
  </ol>
  
  <p>Is this described behaviour right ?</p>
  
  <p>What really happens when I start new task inside ASP.NET MVC controller?</p>
  
  <pre><code>public ActionResult Index()
  {
      var task1 = Task.Factory.StartNew(() =&gt; DoSomeHeavyWork());
      return View();
  }
  
  private static async Task DoSomeHeavyWork()
  {
      await Task.Delay(5000);
  }
  </code></pre>
  
  <ol>
  <li>controller action starts to execute inside thread that processes current request - T1.</li>
  <li>the thread pool allocates another one thread (T2) for task1.</li>
  <li>task1 starts "immediately" inside T2.</li>
  <li>the View result returns "immediately".</li>
  <li>ASP.NET do some work, server sends a response, T1 returns to the thread pool, T2 is still alive.</li>
  <li>after some time when the DoSomeHeavyWork will be finished, the T2 thread will be returned to the thread pool.</li>
  </ol>
  
  <p>Is it correct ?</p>
  
  <p>Now lets look to async action</p>
  
  <pre><code>public async Task&lt;ActionResult&gt; Index()
  {
      await DoSomeHeavyWork();
      return View();
  }
  </code></pre>
  
  <p>, I understand the difference with previous code sample, but not the process, in this example the behaviour is the following:</p>
  
  <ol>
  <li>action starts to execute inside thread that processes current request - T1.</li>
  <li>DoSomeHeavyWork "immediately" returns a task, let's call it "task1" too.</li>
  <li>T1 returns to the thread pool.</li>
  <li>after the DoSomeHeavyWork finishes, Index action continue to execute.</li>
  <li>after Index action execution the server will send a response.</li>
  </ol>
  
  <p>Please explain what happening between points 2 and 5, the questions are:</p>
  
  <ol>
  <li>is the DoSomeHeavyWork processed inside task1 or where (where it is "awaited") ? I think this a key question.</li>
  <li>which thread will continue to process the request after await - any new one from the thread pool, right ?</li>
  <li>request produces thread allocating from the thread pool, but response will not be sent until the DoSomeHeavyWorkAsync finished and it doesn't matter in which thread this method executes. In other words, according to <strong>single</strong> request and <strong>single</strong> concrete task (DoSomeHeavyWork) there is no benefits of using async. Is it correct ?</li>
  <li>if the previous statement is correct, then I don't understand how the async can improve performance for multiple requests with the same <strong>single</strong> task. I'll try to explain. Let's assume that the thread pool has 50 threads available to handle requests. Single request should be processed at least by one single thread from the thread pool, if the request starts another threads, then all of them will be taken from the thread pool, e.g. request takes one thread to process himself, starts 5 different tasks in parallel and wait all of them, thread pool will have 50 - 1 - 5 = 44 free threads to handle incoming requests - so this is a parallelism, we can improve performance for single request, but we reduce number of requests which can be processed. So according request processing in ASP.NET I suppose that only task that somehow starts IO completion thread can achieve a goal of async (TAP). But how IO completion thread calls back thread pool thread in this case ?</li>
  </ol>