John John
John John

Reputation: 1

Does Async method improve performance or it only affect extensibility of the application

I am reading this documentation about EF core @ https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/?view=aspnetcore-5.0 and the author mentioned the following about Async processing:-

Asynchronous code does introduce a small amount of overhead at run time, but for low traffic situations the performance hit is negligible, while for high traffic situations, the potential performance improvement is substantial.

but per my understanding that using Async code will not improve performance, it will only allow IIS to handle more requests, but the request response time will not be changed whether we used sync or async processing ... is my point correct?

Upvotes: 0

Views: 1285

Answers (1)

djbobo
djbobo

Reputation: 547

If you break down typical IIS application stages as

  1. Read request from Network
  2. Deserialise request
  3. Request data from database (EF call).
  4. Enrich data with cache.
  5. Serialise response
  6. Send response to Network

1, 3 and 6 wait for network / SQL server for the completion. And these steps takes majority of request time (say 80% vs steps 2, 4, 5 taking the rest 20%)

If the steps 1, 3, 6 are non async it means there is an idle Process Thread waiting when they finish. There is a limit of how many threads you can have on a server let's say it is 10,000. OS will not be able to support more because of the overhead of CPU context switches, memory associated with each of the Thread call stack. It means that you cannot have more than 10k simultaneous requests processed by the server, even though 80% of them will do nothing but wait for IO and the rest 20% will compete to be scheduled by OS kernel.

If the steps 1,3,6 are async it will be that the call variables needed to continue the execution are stored in a special Task continuation data structure which is far smaller than full Thread call stack, OS does not have overhead of rescheduling 10k threads, IIS can operate with the number of threads equals to the physical CPU cores and those thread taking 100% of CPU full steam process the completed IO continuations. The server throughput is now 4+ times more.

If you never have more simultaneous requests than number of your server CPU cores than blocking calls work faster than async because there is not need to create Tasks, queue their completion. But this is rarely the case.

Upvotes: 1

Related Questions