I have as ASP.NET Core app where I use async and await for database queries. I wonder if I will get any benefits from writing asynchronous code if I host my database on the same server as my web app. I've read that if I want to benefit from using async and await, the whole flow must be asynchronous. Do RDBMS databases, such as SQL Server or PostgreSQL, also perform queries in an asynchronous way? Do I need to host my database on another server to see any benefits?
1 Answer
Do I need to host my database on another server to see any benefits?
Async is largely about not trying up a thread while waiting on things like data; a blocked thread can't do useful work, and if you exhaust all the available threads because they're all blocked: you can get into a very bad state. Using async when talking to another server will mean that you're also avoiding any potential blockages due to network latency and bandwidth, but unless you're querying very high volumes of data or your server is geographically distant, most of the delays when talking to an RDBMS are going to be internal to the RDBMS. As such, async can still be useful for same-machine out-of-process RDBMS calls, since threads are per-process. The situation may get more complex if talking about in-process RDBMS scenarios.
As with any performance question, the best advice is: measure it in your specific scenario. And note that for low-volume systems: it probably won't matter at all, and no choice will be measurably better. The real advantages of async shine in highly contested systems.
async/awaitonly means that other code can be ran while the runtime waits for theasyncoperation to complete.