Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the benefits of async webservices when not all parts of the code is async

I am wondering how much benefit you get from using async http requests if not all parts of your code is async.

Lets consider to scenarios: 1) async http requests blocking on sync database calls, and 2) sync http requests awaiting async database calls.

1) Web Api supports async action methods, but if I do a sync database call when handling the request, then the thread blocks on the call, and I will not get the benefits of better thread economy that async could give me or what?

2) If I have a synchronous ServiceStack service call that awaits an async database call, then what happens? I assume a thread is reserved for handling the entire sync http request, and when this thread awaits an async call then it is still reserved to the web request or what ?

Basicallly my question can be boiled down to this: Is there any reason to use async if not all IO calls are async?

like image 269
Stig Schmidt Nielsson Avatar asked May 30 '13 16:05

Stig Schmidt Nielsson


People also ask

What are the benefits of asynchronous code?

Asynchronous programming can help systems run more effectively, depending on the situation and often prevents long wait times. For example, if a task you want to perform uses a lot of input and output, asynchronous programming lets other tasks run, whereas synchronous programming would create a time block.

What is use of async in Web API?

Synchronous/asynchronous APIs provide a way to make immediate or scheduled requests for resources, data or services when available. Applications request data and wait until a value is returned. The synchronous or asynchronous nature of an API is a function of the time frame from the request to the return of data.

What is the benefit of using asynchronous calls instead of using synchronous calls?

Access to a web resource sometimes is slow or delayed. If such an activity is blocked within a synchronous process, the entire application must wait. In an asynchronous process, the application can continue with other work that doesn't depend on the web resource until the potentially blocking task finishes.

In what situations do we need to use asynchronous code?

Asynchronous loops are necessary when there is a large number of iterations involved or when the operations within the loop are complex. But for simple tasks like iterating through a small array, there is no reason to overcomplicate things by using a complex recursive function.


2 Answers

On the server side, there is usually no benefit in a partially-async solution.

1) Web Api supports async action methods, but if I do a sync database call when handling the request, then the thread blocks on the call, and I will not get the benefits of better thread economy that async could give me or what?

Correct. As soon as you do a synchronous (blocking) database call, you are tying up a thread for the duration of that call. So the benefits that you would have gotten from async do not apply.

2) If I have a synchronous ServiceStack service call that awaits an async database call, then what happens? I assume a thread is reserved for handling the entire sync http request, and when this thread awaits an async call then it is still reserved to the web request or what ?

This is just the same as a synchronous database call. Under the covers, a synchronous database call executes the actual call asynchronously and then blocks the calling thread until it completes. So you still have a thread blocked for the duration of the call, and you don't get any async benefits.

Basicallly my question can be boiled down to this: Is there any reason to use async if not all IO calls are async?

There are a few more obscure scenarios. You can use Task.WhenAll to do a limited type of concurrency, which is much easier with async than other forms of asynchronous concurrency. But that's the only benefit I can think of if you don't have a fully-async stack.

like image 87
Stephen Cleary Avatar answered Oct 15 '22 12:10

Stephen Cleary


It basically boils down to what the calling code can do while the async call is being done. Your first example is a good fit for this. The UI can go about doing other things when a save method is called. It doesn't matter what is going on behind the scenes within the method...control has been given back to the application to continue about its way.

Your second example just means that the servicestack service could do something while the async database call is being done. But if it isn't doing anything, there isn't any real reason to use an async call to begin with.

What the async method does during its workload is not as important as what the calling application can do during the call.

like image 41
Josh Avatar answered Oct 15 '22 12:10

Josh