ASP.NET Core Interview Question #177

Why should asynchronous programming be used for I/O-bound ASP.NET Core work?

Performance, Caching & HTTP Senior Advanced

Quick Interview Answer

Asynchronous I/O allows a request thread to return to the thread pool while waiting for operations such as database queries, file access, or HTTP calls. This improves scalability when many requests are waiting on external I/O.

Detailed Explanation

Asynchronous I/O allows a request thread to return to the thread pool while waiting for operations such as database queries, file access, or HTTP calls. This improves scalability when many requests are waiting on external I/O.

Async does not make the external operation itself faster, and it is not automatically beneficial for short CPU-bound work. Avoid blocking asynchronous operations with .Result or .Wait(), because blocking consumes threads and can reduce throughput.

Code Example

[HttpGet("{id:int}")]
public async Task<IActionResult> Get(
    int id,
    CancellationToken cancellationToken)
{
    var product = await _service.GetAsync(id, cancellationToken);
    return product is null ? NotFound() : Ok(product);
}