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

Tanul 1,251 Reputation points
2021-01-24T12:21:03.2+00:00

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

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,164 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,119 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Alberto Poblacion 1,556 Reputation points
    2021-01-31T13:42:28.763+00:00

    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.

    3 people found this answer helpful.

  2. Duane Arnold 3,211 Reputation points
    2021-01-24T20:19:31.967+00:00

    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. Bonnie DeWitt 811 Reputation points
    2021-01-24T21:29:13.563+00:00

    @Tanul ,
    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


  4. Duane Arnold 3,211 Reputation points
    2021-01-30T22:34:24.36+00:00

    @Tanul

    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.

    0 comments No comments