1

In HTTP protocol, client anyway has to wait till server process the request and return HTML/JSON. What is significant/different between Async and sync operation? I mean there is no GUI which may freeze if sync operation used.

Async method

[HttpGet]
[Route("{id}")]
public async Task<ActionResult<Employee>> GetByID(int id)
{
    return await _employeeRepo.GetAsyncByID(id);
}

Sync method

[HttpGet]
[Route("{id}")]
public ActionResult<Employee> GetByID(int id)
{
    return _employeeRepo.GetByID(id);
}

Async will not freeze the main thread but then how does it make any difference when there is no UI

Rashmin Javiya
  • 5,173
  • 3
  • 27
  • 49
  • 3
    This article covers a lot of information about async ASP.Net Actions. https://learn.microsoft.com/en-us/aspnet/mvc/overview/performance/using-asynchronous-methods-in-aspnet-mvc-4#ChoosingSyncVasync One of the main benefits is that sync Actions will block a thread waiting for external work to be done (like calling a database) where as in an async Action that thread is freed up for something else to use. A secondary benefit is that if the HTTP request is ended early by the client, then the CancellationToken will be cancelled and you can stop earlier. –  Oct 03 '18 at 08:45
  • 1
    As far as I can say, it is a difference for server-side code. Some time ago there where a problem with IIS hosted ASP.Net apps - they can process only one request at a time. So, while someone is invoking very long request, others get waiting - async action solve this. – vasily.sib Oct 03 '18 at 08:46
  • @AndyJ Thank you for sharing the link. – Rashmin Javiya Oct 03 '18 at 09:51

2 Answers2

1

As you said, the choice to use async Tasks for your controller methods has nothing to do with the client, which will still wait for the HTTP response in the same way. The use of async controller methods allows the server to more easily use async methods internally to manage its performance.

In your first example, assuming your _emplyeeRepo uses async properly, the request thread is free while the DB access layer waits for the DB call to return. This means there's a thread free for other requests while waiting for the DB.

In your second example, though, the thread that serves the HTTP request is blocked until the DB returns.

Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
-1

If you blindly use async method the performance will be poor than a sync method. The reason is a async method is a parallel operation hence take extra time to create the parallel operation and time slice allocated to it by operating system.

Lots of developer blindly use async method while calling a web method or https request. But when exactly you need to implement async method? Suppose you are calling more than one APIs or web service which take significant amount of time to complete. In such a situation to save the amount of time you need to declare all these kind of methods as async so that all of them executes parallel and you can save time.

One clear example will be, suppose you have to call file RESTful services (apis) and each call takes 1 second. Hence a set of 5 sync calls will take exactly 5 seconds. But if you make them all async methods it will take 1 seconds and few milliseconds to creates the thread for each methods and time slice allocation. Hence all of them will be completed in 1 second and few milliseconds. This parallel execution also depends on number of processor and size of memory in you system as well.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
  • 3
    [There is no thread](https://blog.stephencleary.com/2013/11/there-is-no-thread.html). So "in async method a thread has been created so that method execute parallel" is basically wrong – Damien_The_Unbeliever Oct 03 '18 at 08:51
  • What @Damien_The_Unbeliever says is also repeated in the documentation. https://learn.microsoft.com/en-us/aspnet/mvc/overview/performance/using-asynchronous-methods-in-aspnet-mvc-4#ChoosingSyncVasync "When you're doing asynchronous work, you're not always using a thread. For example, when you make an asynchronous web service request, ASP.NET will not be using any threads between the async method call and the await." –  Oct 03 '18 at 08:56
  • 1
    @AndyJ, I have modified my answer. Thanks for the advice :-) – Abhijit Pritam Dutta Oct 03 '18 at 09:03
  • @Damien_The_Unbeliever I have modified my answer. Thanks for you comments – Abhijit Pritam Dutta Oct 03 '18 at 09:04
  • You're not actually answering the question asked. Using async controller methods in MVC doesn't have anything to do with parallelizing resource access. – Avner Shahar-Kashtan Oct 03 '18 at 09:47