question

TaB-8489 avatar image
0 Votes"
TaB-8489 asked ·

Any benefit of async/await in rest api with synchronous flow?

Team,

I have hosted a rest api as a proxy layer in .net core 3.1 which shall receive 40K requests per hour. Here, a single request travels like this within the code:

  1. Authentication Middleware—>
    i. Check data in static storage
    ii. If not, call(getasync) another api using httpclient—>
    iii. Parse the returned json response using json deserialize async
    iv. Fill data in static storage
    v. Validate data if request is authentic—>

  2. If success from Authentication middleware then call Authorization Middleware—>

  3. If auth policy matches then call Controller Action—>
    i. Call(getasync) one more api using httpclient—>
    ii. Return the data to the requestor came from api.

As it is shown that each step is depending on the previous one. There is no step where I can do something before the completion of previous step like fire and forget.

In such case, should I use async/await as this comes with a cost of adding IAsyncStateMacine. Please suggest?

Thank you





dotnet-csharpdotnet-runtimedotnet-runtime-coredotnet-aspnetcore-generaldotnet-aspnetcore-webapi
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered ·

IMHO, I don't see any advantage to using async when the flow in sync. To me the async advantage would help when the client is doing async calls using WebAPI where the controller is doing async CRUD operations with the database by using the Data Access Object pattern in using async with the Entity Framework in the DAL as an example.

· 3 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@DuaneArnold-0443 , I think the best I can do is all the calls I'm making to my logger manager to log in file or console log can be added into async/await as those
segments are depending on each other. What do u say?

0 Votes 0 ·

I think the best I can do is all the calls I'm making to my logger manager to log in file or console log can be added into async/await as those
segments are depending on each other. What do u say?

I don't see why any of these functionalities would be dependent upon each other, and becuase they may not be dependent on each other, async could be used in the execution flow for each function.






0 Votes 0 ·
TaB-8489 avatar image TaB-8489 DuaneArnold-0443 ·

Yes these are not dependent on each other that is why I haven’t wrote them in the question itself. But I understood that I will make them as async await.

Thanks a lot for the help. Take care

0 Votes 0 ·
BonnieDeWitt-QnA avatar image
0 Votes"
BonnieDeWitt-QnA answered ·

@TaB-8489,
I agree that async wouldn't work for each individual step, but I'm thinking maybe you should use async for the whole process in a fire-and-forget manner. If you're going to be receiving 40K requests an hour, you have to have some mechanism for taking the request and sending it off somewhere to be processed, because the next request will be coming in pretty quickly behind the previous request, right?

So your "send it off somewhere to be processed" could be either an async method that deals with all three of your steps (each step in a synchronous manner) or a new thread that does the same thing. In either case, it's fire-and-forget because this other method/thread would handle the entire process.

Unfortunately, I'm no expert in REST APIs, and I'm not 100% sure this kind of processing is possible in a REST API, but it's something to think about.


~~Bonnie DeWitt [MVP since 2003]
http://geek-goddess-bonnie.blogspot.com


· 5 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@BonnieDeWitt-QnA, hmm correct. But the problem is each nd every step is depending on other. Even though I add all the 3 steps in async/await but the line running just after those 3 steps depending on them. So that is why I'm unable to convince my self that if there is any of async/await required in the code.

0 Votes 0 ·

@BonnieDeWitt-QnA, I think the best I can do is adding all the calls to logger manager that writes into file or console or email, into async/await because those segments are depending on each other. Would you think this as a good option?

0 Votes 0 ·

@TaB-8489 , ah, but you didn't mention that there was a step after those 3 steps that was dependent on them in your initial description of the problem!!

Is that step something that you can add to the first 3 and make it a 4 step method/process?

BTW, async does not have to be async and await. There is a way to make it a fire-and-forget type of process but I don't recall what has to be done for that.
I do asynchronous kinds of stuff the old-fashioned way (by spinning off new threads to do the work), so I don't actually work with "async" methods.

0 Votes 0 ·
TaB-8489 avatar image TaB-8489 BonnieDeWitt-QnA ·

@BonnieDeWitt-QnA , actually they are like simple trace and exception logs and they are not dependent on anything. That is why I haven’t wrote them in the question. You’re correct I can add something of fire and forget type while logging stuff.

Are you referring something like this

 public async void LogMessage(){
   await Task.Run(() => PrintConsole());
 }

Or without async/await. Just a direct call to

 Task.Run(() => PrintConsole());


0 Votes 0 ·
Show more comments
DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered ·

@TaB-8489

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await#the_await_keyword

<copied>

The await keyword


The real advantage of async functions becomes apparent when you combine it with the await keyword — in fact, await only works inside async functions. This can be put in front of any async promise-based function to pause your code on that line until the promise fulfills, then return the resulting value.

<end>

Execution is going to stop on the line and wait until the function completes.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

APoblacion avatar image
3 Votes"
APoblacion answered ·

There is one case where you will benefit from using async even if your code performs a synchronous sequence of operations. This is when any of the operations depend on an external resource (such as a database query, a disk access or a call to a remote server). When this happens, the code that you are executing blocks until the external resource responds. This "uses up" one of the (limited number) of threads that the server can handle, doing nothing but waiting for something external to respond. If you are receiving lots and lots of calls, the number of consumed threads will continue to grow until none are available to service requests, and IIS will start queuing them, or even rejecting them (error five hundred something, "server too busy") if the queue fills up. This means that some requests for other, unrelated services from your server (such as a plain html page or an image) will not be serviced, even if the server has still plenty of capacity to do so, because all of its threads are locked waiting for an external resource.
The remedy is to use await for the call to the external resource. This will free the thread, so the server can do other, unrelated work while it is waiting for the external resource. I have seen demos from Microsoft where, under the right conditions, this made the server able to handle a workload that was one thousand times larger than without the async/await.

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Right on. When you are making a call to an external API, you are blocking a thread waiting for that APi call to return. The problem is that an external API call takes relatively an eternity to return. What async alllows you to do is relatively easily free up your thread while waiting and say 'once this API call returns, then continue where I left off.' In the mean time that thread can be used again for other incoming requests. I am completely butchering what really happens, but at a high level you can think about it like that. Before async/await there were also patterns that allowed you to do this, but they were complex to use. So in your scenario: absolutely, all calls to external resources like storage, disk or other APIs should use async/await. This will increase the number of requests you can handle.

0 Votes 0 ·