ASP.NET Core Interview Question #200

What are key ASP.NET Core performance, caching, and HTTP best practices?

Performance, Caching & HTTP Senior Advanced

Quick Interview Answer

Use asynchronous APIs for I/O-bound work, propagate cancellation, keep database queries efficient, paginate large result sets, and return only required data. Reuse outbound HTTP connections through IHttpClientFactory and configure sensible timeouts.

Detailed Explanation

Use asynchronous APIs for I/O-bound work, propagate cancellation, keep database queries efficient, paginate large result sets, and return only required data. Reuse outbound HTTP connections through IHttpClientFactory and configure sensible timeouts.

Cache only data or responses with a clear freshness and invalidation policy. Choose in-memory, distributed, HTTP, or output caching according to the scenario, and never cache personalized data in a way that can leak it across users.

Enable suitable compression and modern HTTP support through correct hosting configuration, apply rate limits where appropriate, design for horizontal scaling when required, and optimize from measured evidence rather than assumptions.

Code Example

builder.Services.AddMemoryCache();
builder.Services.AddOutputCache();
builder.Services.AddResponseCompression();
builder.Services.AddHttpClient();
builder.Services.AddRateLimiter(_ => { });

var app = builder.Build();

app.UseResponseCompression();
app.UseOutputCache();
app.UseRateLimiter();

app.MapControllers();