ASP.NET Core Interview Question #197

What is request timeout handling and why is it important?

Performance, Caching & HTTP Senior Advanced

Quick Interview Answer

Timeouts prevent requests or downstream operations from consuming resources indefinitely. Outgoing HTTP calls, database commands, and long-running application operations should have limits appropriate to their expected duration.

Detailed Explanation

Timeouts prevent requests or downstream operations from consuming resources indefinitely. Outgoing HTTP calls, database commands, and long-running application operations should have limits appropriate to their expected duration.

Cancellation tokens should be propagated so timed-out or disconnected work can stop when supported. A timeout should produce controlled behavior and useful telemetry rather than leaving partially completed operations unnoticed.

Timeout values should be based on service objectives and dependency behavior, not arbitrary extremely large defaults.

Code Example

builder.Services.AddHttpClient<ExternalApiClient>(client =>
{
    client.Timeout = TimeSpan.FromSeconds(10);
});

public async Task<string> LoadAsync(
    CancellationToken cancellationToken)
{
    return await _http.GetStringAsync(
        "data", cancellationToken);
}