ASP.NET Core Web API Interview Question #150

What are best practices for error handling, logging, caching, rate limiting, CORS, and API reliability?

Error Handling, Logging, Caching, Rate Limiting, CORS & Reliability Senior Advanced

Quick Interview Answer

Handle unexpected exceptions centrally and return consistent, safe ProblemDetails responses. Use structured logs with correlation or trace identifiers while excluding secrets and unnecessary sensitive data. Distinguish expected client errors from unexpected server failures.

Detailed Explanation

Handle unexpected exceptions centrally and return consistent, safe ProblemDetails responses. Use structured logs with correlation or trace identifiers while excluding secrets and unnecessary sensitive data. Distinguish expected client errors from unexpected server failures.

Cache only data that can be safely reused, define expiration and invalidation rules, and choose local or distributed caching according to deployment needs. Configure CORS for trusted browser origins, apply rate limits appropriate to endpoint cost, and use health checks, timeouts, cancellation, bounded retries, circuit breakers, and idempotency mechanisms to improve reliability without creating duplicate effects or retry storms.

Code Example

builder.Services.AddProblemDetails();
builder.Services.AddOutputCache();
builder.Services.AddRateLimiter(options =>
{
    options.AddFixedWindowLimiter("api", limiter =>
    {
        limiter.PermitLimit = 100;
        limiter.Window = TimeSpan.FromMinutes(1);
    });
});
builder.Services.AddHealthChecks();

app.UseExceptionHandler();
app.UseOutputCache();
app.UseRateLimiter();
app.MapHealthChecks("/health");
app.MapControllers();