Home ADO: Async all the way down the tubes?
Reply: 0

ADO: Async all the way down the tubes?

user1948
1#
user1948 Published in April 26, 2018, 1:45 pm

Okay, so "async all the way down" is the mandate. But when is it problematic?

For example, if you have limited access to a resource, as in a DbConnection or a file, when do you stop using async methods in favor of synchronous?

Let's review the complexity of an asynchronous database call: (Not putting .ConfigureAwait(false) for readability.)

// Step 1: Ok, no big deal, our connection is closed, let's open it and wait.
await connection.OpenAsync();
// Connection is open!  Let's do some work.

// Step 2: Acquire a reader.
using(var reader = await command.ExecuteReaderAsync())
{
    // Step 3: Start reading results.
    while(await reader.ReadAsync())
    {
        // get the data.
    }
}

Steps:

  1. Should be reasonably innocuous and nothing to worry about.

  2. But now we've acquired an open connection in a potentially limited connection pool. What if when waiting for step 2, other long running tasks are at the head of the line in the task scheduler?

  3. Even worse now, we await with an open connection (and most likely added latency).

Aren't we holding open a connection longer than necessary? Isn't this an undesirable result? Wouldn't it be better to use synchronous methods to lessen the overall connection time, ultimately resulting in our data driven application performing better?

Of course I understand that async doesn't mean faster but async methods provide the opportunity for more total throughput. But as I've observed, there can definitely be weirdness when there are tasks scheduled in-between awaits that ultimately delay the operation, and essentially behave like blocking because of the limitations of the underlying resource.

[Note: this question is focused on ADO, but this also applies to file reads and writes.]

Hoping for some deeper insight. Thank you.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.346537 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO