user12310517
user12310517

Reputation:

When is it not good to use await async?

I have an ASP.NET Core 3.1 based project written using C#. I am aware that the best time to use await and async is when accessing external resources like pulling data from the database, accessing files, or making an HTTP request. This frees up the thread so more work is done instead for the thread to sit around waiting for code to finish.

However, I am trying to figure out at what point using async/await will hurt performance? Does the process of releasing the thread when await is called and retrieving a thread when the task complete have cost?

The code found next, is called asynchronously. In reality, that code does not need to be called asynchronously since all the code is executed in memory and no external requests are made.

public interface ILocator
{
    Task<Model> ExampleAsync();
}

public class Example : Controller
{
    public ILocator Locator { get; set; }

    public Example(ILocator locator)
    {
        Locator = locator;
    }

    public async Task<IActionResult> Example()
    {
        Model model = await Locator.ExampleAsync();

        return View(model);
    }
}

public class Locator : ILocator
{
    pubilc Task ExampleAsync()
    {
        Example model = new Example();
        model.Status = "New";

        return Task.CompletedTask;
    }
}

Here is the synchronous version of the above code

public interface ILocator
{
    Model Example();
}

public class Example : Controller
{
    public ILocator Locator { get; set; }

    public Example(ILocator locator)
    {
        Locator = locator;
    }

    public IActionResult Example()
    {
        Model model = Locator.Example();

        return View(model);
    }
}

public class Locator : ILocator
{
    pubilc Example()
    {
        Example model = new Example();
        model.Status = "New";
    }
}

Will the asynchronous version of the code have higher cost/lower performance than the synchronous version due to the unnecessary await/async usage?

When will async/await do more harm than good?

Upvotes: 7

Views: 4464

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273804

I have an ASP.NET Core 3.1 based project written using C#.

That means that async/await will allow you to increase your capacity (in requests pers second) a great deal.

at what point using async/await will hurt performance?

it will add a little bit of overhead that is a fixed amount. It will in general be small compared to the costs of the I/O and not really noticable.

When will async/await do more harm than good?

In a web site/service that will never see a load above ~50 requests / second.
And even then the 'harm' will be very small.

Actual numbers depend on the hardware, amount of I/O work etc.

In reality, that code does not need to be called asynchronously since all the code is executed in memory

In that case it will be faster to handle it synchronously.
But I know teams that prefer to have all Actions async, just for uniformity. And since the overhead is so small I consider that a valid approach too.

Upvotes: 6

Frederik Gheysels
Frederik Gheysels

Reputation: 56974

You typically use async/await when performing I/O bound tasks like reading from a stream, reading from a DB, sending something over the network or waiting for a response.

This makes the thread available to do some other (CPU related work).

Technically, async/await is slower in terms of raw performance, however, it increases the scalability of your application since it allows threads to be available for other work while others are waiting for I/O bound operations.

Upvotes: 13

Related Questions