Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebApi async vs sync

I'm new to .Net WebApi, as I know it's preferable to use async APIs instead of sync ones, but what is the difference? if the API is sync and has been called from a client, and another call from another client, as I checked, no interruption will happen, and both calls will go through simultaneously. So, what's the benefit of making it Async?

Update: as I understand, if the number of requests are huge, if I use async, the waiting time for some calls will be less, cause there are more threads available to run tasks(as some of them are released waiting for database call, or network call etc.) is it true?

like image 578
shayan Avatar asked Oct 23 '18 06:10

shayan


People also ask

Is Web API synchronous or asynchronous?

XMLHttpRequest supports both synchronous and asynchronous communications. In general, however, asynchronous requests should be preferred to synchronous requests for performance reasons.

Should I use async in Web API?

In general, you should make a method asynchronous if the synchronous method blocks the ASP.NET request thread while doing no work. By making the call asynchronous, the ASP.NET request thread is not blocked doing no work while it waits for the web service request to complete.

Is REST API synchronous or asynchronous?

REST clients can be implemented either synchronously or asynchronously. Both MicroProfile Rest Client and JAX-RS can enable asynchronous clients. A synchronous client constructs an HTTP structure, sends a request, and waits for a response.

Why API calls should be asynchronous?

The API may have to wait for a backend response. These APIs may provide a callback notification to the requester when the requested resource is ready. Asynchronous requests are useful in maintaining an application's functionality rather than tying up its resources waiting on a request.


1 Answers

I case of SYNC what happens is that for each request a thread is assigned exclusively and this thread is released only upon completion of particular request. While in case of ASYNC the thread may be reused by other request.

So if your application is I/O Bound then you can see significant improvement in your application by using ASYNC, if your application is CPU Bound then ASYNC will not be that much useful.

https://en.wikipedia.org/wiki/I/O_bound

https://en.wikipedia.org/wiki/CPU-bound

like image 77
codemirror Avatar answered Oct 10 '22 17:10

codemirror