Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can async/await pattern cause performance penalties on minor units of work?

I'm looking for some guidance on the "workload threshold" between where async/await makes sense (e.g. to free up IO Completion Ports and avoid thread starvation) vs. unit of work being too simple/cheap to execute, so synchronous execution is a better choice.

In other words, could the use of async/await lead to degraded performance when used in conjunction with relatively fast/resource-inexpensive units of work, and simply executing the work synchronously would be the preferred approach?

Example (all in one method, all asynchronous with await):

  • Save some minor update to DB
  • Reading a very small file upload to a stream using ReadAsStreamAsync
  • Copy read stream to file stream using CopyToAsync
  • Flushing writer stream using FlushAsync
like image 527
Alex Avatar asked Oct 31 '13 06:10

Alex


People also ask

How does asynchronous code affect performance?

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.

Does async await stop execution?

async functions use an implicit Promise to return results. Even if you don't return a promise explicitly, the async function makes sure that your code is passed through a promise. await blocks the code execution within the async function, of which it ( await statement ) is a part.

What happens if we execute an asynchronous method but don't await it?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

Is async await slower?

Async/await incurs a significant hit to performance, thanks to the overhead of the state machine. Nearly every article states that async is actually slower than synchronous. Paradoxically, performance is also the selling-feature of async await.


1 Answers

I recommend you approach this not from a timing standpoint, but from an I/O-vs-CPU standpoint.

CPU-bound methods are naturally synchronous; I/O-bound methods are naturally asynchronous.

I'm assuming that your environment is server-side, based on your example:

  • Save some minor update to DB. Naturally asynchronous: network (or at least out-of-proc) communication, possibility of contention, disk I/O.
  • Reading a very small file upload to a stream using ReadAsStreamAsync. Naturally asynchronous: network communication.
  • Copy read stream to file stream using CopyToAsync. Naturally asynchronous: possibility of contention, disk I/O.
  • Flushing writer stream using FlushAsync. Naturally asynchronous: possibility of contention, disk I/O.

All of these are naturally-asynchronous operations, so they should all be implemented asynchronously. CPUs are incredibly faster than memory, which is incredibly faster than network or disk I/O. Because of this, if you implement a naturally asynchronous method synchronously, you will block a thread. This isn't the end of the world, but the thread pool has to compensate if it's blocked for too long, and the only time you're "saving" is the thread switch time, which will be orders of magnitude shorter than any network or disk I/O could possibly be.

Another thing to consider is unpredictable latency, which usually happens due to resource contention. What if another process is writing to the db at the same time? What if there's a routing hiccup while the file is uploading, requiring retransmission of packets? What if the disk is defragmenting while you're trying to write the output file? Asynchronous operations have a tendency to be unpredictable like this, and async code ensures you won't be blocking a thread for much longer than you expected.

In conclusion: use synchronous code for synchronous (CPU-bound) work, and asynchronous code for asynchronous (I/O-bound) work.

like image 162
Stephen Cleary Avatar answered Oct 30 '22 09:10

Stephen Cleary