TechInterviewNotes

‹ Library / Networking

.NET HTTP Client Reliability Notes

Save

Updated 30 Jul 2026 · 2 min read

Interview-ready notes on HttpClient lifetime, connection pooling, DNS refresh, timeouts, cancellation, and resilient outbound HTTP.

Why HttpClient lifetime matters

HttpClient is a high-level client, but each request ultimately depends on connections managed by SocketsHttpHandler. Creating and disposing a client for every request can churn connection pools and contribute to socket exhaustion.

Use a long-lived client or obtain clients from IHttpClientFactory.

A typed client

public sealed class CatalogClient(HttpClient httpClient)
{
    public async Task<Product?> GetAsync(
        int id,
        CancellationToken cancellationToken)
    {
        using var response = await httpClient.GetAsync(
            $"/products/{id}",
            HttpCompletionOption.ResponseHeadersRead,
            cancellationToken);

        response.EnsureSuccessStatusCode();
        return await response.Content.ReadFromJsonAsync<Product>(
            cancellationToken: cancellationToken);
    }
}

Registration:

services.AddHttpClient<CatalogClient>(client =>
{
    client.BaseAddress = new Uri("https://catalog.example");
    client.Timeout = TimeSpan.FromSeconds(10);
});

Nested generic syntax check

Real applications often keep retry or status information in nested generic structures:

var attemptsByEndpoint = new Dictionary<string, List<int>>();

The type above should remain visible exactly as written.

Timeout and cancellation

Retry rules

Retry only transient failures and only when the operation is safe to repeat. Add jitter to avoid synchronized retry storms. Do not retry validation errors, authentication failures, or an unsafe write unless idempotency is guaranteed.

Interview checklist

  1. Explain why per-request disposal can be harmful.
  2. Describe what IHttpClientFactory manages.
  3. Distinguish request timeout, cancellation, and connection timeout.
  4. Explain retry amplification.
  5. Mention observability: latency, status, retry count, and correlation ID.