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

3
  • 3
    This article covers a lot of information about async ASP.Net Actions. learn.microsoft.com/en-us/aspnet/mvc/overview/performance/… 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. Commented Oct 3, 2018 at 8: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. Commented Oct 3, 2018 at 8:46
  • @AndyJ Thank you for sharing the link. Commented Oct 3, 2018 at 9:51

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

-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.

5 Comments

There is no thread. So "in async method a thread has been created so that method execute parallel" is basically wrong
What @Damien_The_Unbeliever says is also repeated in the documentation. learn.microsoft.com/en-us/aspnet/mvc/overview/performance/… "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."
@AndyJ, I have modified my answer. Thanks for the advice :-)
@Damien_The_Unbeliever I have modified my answer. Thanks for you comments
You're not actually answering the question asked. Using async controller methods in MVC doesn't have anything to do with parallelizing resource access.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.