2

I'm currently working on an ASP NET web api project, which has all its calls to the database in an asynchronous way.

We are using ServiceStack.OrmLite, which provides us with an asynchronous and synchronous API to make the database calls.

Example we are doing this:

Option 1

var activity = await context.SingleByIdAsync<Activity>(x => x.Id == myId);
var place = await context.SingleByIdAsync<Place>(x => x.Id == myId);

But we could also implement it this way.

Option 2

var activity = context.SingleById<Activity>(x => x.Id == myId);
var place = context.SingleById<Place>(x => x.Id == myId);

From what little I understand of asynchronous programming, with option 1 we are blocking the thread and releasing it when the operation is completed.

I think that option 1 is more expensive the way we are implementing it than option 2.

Go into a blocking wait - Thread.Sleep, Task.Wait, etc. This means that you fire your database request on thread A and then enter a wait on that thread: the thread is allocated but is blocked and not usable for anything else. When your data is ready, you somehow learn of that and exit the wait and your code continues on thread A.

  1. What is the best approach?

  2. What is the benefit of using asynchronous operations for database calls?

  3. Are we implementing asynchronous calls properly?

  4. Will I have a problem if I switch all calls to the database to synchronous?

3
  • 2
    You have asked a lot of questions.... 1) We cant know the best apprach, we dont know what is in your head. 2) The benefit of async is its scalable, and a hardware chip is waiting for a callback not blocking a thread. 3) in option 1, yes 4) who knows, depends what you call a problem Commented Jul 30, 2020 at 3:17
  • Reading on async await would give you better idea for comparing synchronous and asynchronous implementations. stackoverflow.com/questions/28841345/… and learn.microsoft.com/en-us/dotnet/csharp/programming-guide/… Commented Jul 30, 2020 at 3:31
  • 3
    The entire point of async/await is that it does not block threads. In the first option your thread will be free to do other stuff while you wait on the I/O from the DB. For a web app this means that you can handle more traffic. What little code you have looks right, but async/await is something that you have to do from the top to the bottom of your code and if you throw a single Wait or Result call in there you're going to be in trouble, so it's hard to say if you're doing it right without a lot more detail. Commented Jul 30, 2020 at 4:04

1 Answer 1

3
  1. What is the best approach?

    This is hard to tell by so little code, but in general if you are willing to change all the methods in the call-stack and there is a proper async methods available use it.

    What I mean by change all the methods in the call-stack is, if you change method T A() to be async (i.e. async Task<T> AAsync() then you will have to also change any method calling A to be also async.

    What I mean by proper async method: In the case of ASP.Net there is no UI-Thread so it would be unimportant which thread would be blocked by a synchronous method. So in this context a proper async method would be one that does some async IO and does not block any thread (not even some thread-pool-thread).

  2. What is the benefit of using asynchronous operations for database calls?

    In generell the benefit of using async in ASP.Net is that while a request is asynchronously waiting for some (proper) async operation to finish, it does not block any thread from processing another request.

    So as long the packaged you use, implements the database calls as proper async IO you will have the benefit of better scalability of your server (i.e. it can process more request in parallel).

  3. Are we implementing asynchronous calls properly?

    It is hard to tell from so little code, but as long you don't use Task.Wait() in the call stack you are probably using to correct.

  4. Will I have a problem if I switch all calls to the database to synchronous?

    As said in 2. you could get problems when two many request are done at the same time.

You said "From what little I understand of asynchronous programming, with option 1 we are blocking the thread and releasing it when the operation is completed.", it is the opposite. When a thread comes to an await it will asynchronously wait for the awaited tasks to be finished. That means that the thread will be free to do other things. A task that finishes or fails (with an exception) will be scheduled to be continued. Where this is depends on the captured synchronization context.

You also said "I think that option 1 is more expensive the way we are implementing it than option 2.", that is not enterily wrong since async methods are build into a state machine and will have a little bit of overhead. But since you are in an ASP.Net context you will greatly benefit of using the async operations to free thread to work on other requests.

Here is an article on using async on ASP.Net.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.