Wednesday, 28 October 2015

ASP.NET Tips #41 - Concurrent asynchronous I/O on server applications

Concurrent I/O operations on server applications are tricky and hard to handle correctly. This is especially the case when applications are under high load. I would like to highlight two core principals to keep in mind:

Firstly, never perform concurrent I/O operations in a blocking fashion on your .NET server applications. Instead, perform then asynchronously, like the following code snippet:

[HttpGet]
public async Task<IEnumerable<Car>> AllCarsInParallelNonBlockingAsync() 
{
   IEnumerable<Task<IEnumerable<Car>>> allTasks = PayloadSources.Select(uri => GetCarsAsync(uri));
   IEnumerable<Car>[] allResults = await Task.WhenAll(allTasks);
   return allResults.SelectMany(cars => cars);
}

When concurrent requests are made, the asynchronous method typically performs six times faster.

Secondly, remember to also perform load tests against your server applications based on your estimated consumption rates if you have any sort of multiple I/O operations.

For more information, see http://www.tugberkugurlu.com/archive/how-and-where-concurrent-asynchronous-io-with-asp-net-web-api

No comments :

Post a comment