ASP.NET Core Web API Interview Question #108

Why should Web API data access use asynchronous methods?

Dependency Injection, Services, EF Core, Async & Data Access Mid-Level Intermediate

Quick Interview Answer

Database and network operations are I/O-bound. Async APIs allow the request thread to be returned while the application waits for I/O, helping server throughput under concurrent workloads.

Detailed Explanation

Database and network operations are I/O-bound. Async APIs allow the request thread to be returned while the application waits for I/O, helping server throughput under concurrent workloads.

Async does not make the database query itself inherently faster. Its main server benefit is avoiding blocked threads while waiting for I/O.

Code Example

var product = await _db.Products
    .AsNoTracking()
    .SingleOrDefaultAsync(
        x => x.Id == id,
        cancellationToken);