Question

If we start a .NET Core 3.0 project today, which will use database connection (for example ASP.NET Core Web API), is there still a place for non-async operations while talking to the database?

Or in other words: Should we always (by default) use async/await in Data Repository methods, while directly querying the database? I'm talking, using ToListAsync, FirstOrDefaultAsync?

I'm struggling to see any advantages by not using async operations while talking to the database.

No correct solution

OTHER TIPS

The point of async is to allow the thread to perform other work while waiting on slower services/devices to repond.

So is your thread needed to perform other work?

  • If it is, then async/await is one way to free up your thread.
  • If it is not, then using async/await only makes your system more complicated without any real benefit.

If you want fast access by a limited number of threads its faster to query the database none async. But it will eat up threads fast so it should only be used when that extra performance is needed and when you know the method will only be used by a thread or two.

Like a background worker or similar, if the none async method is used from a web api method than you will run out of threads on the thread pool when many clients comnect and consume that web api method.

Licensed under: CC-BY-SA with attribution
scroll top