Performance best practices
Most slow ASP.NET Core apps are slow for the same few reasons: too many or too big database queries, blocking calls, no caching and large responses. This lesson is a practical checklist, with how to measure before you change anything.
- StartMeasureTraces, query logs and counters
- Step 2Find the biggest costUsually database or remote calls
- Step 3Fix one thingProjection, index, cache or async
- Step 4Load testCompare with the numbers before
- EndRepeatStop when it is fast enough for users
Measure first
Guessing wastes time. Use Application Insights or OpenTelemetry to find slow requests and the dependencies inside them, the EF Core command log to see queries, dotnet-counters for CPU, memory and thread-pool queues, and a load test (k6, NBomber or Azure Load Testing) to check improvements.
Fix the biggest cost first. One N+1 query on the home page usually matters more than any micro-optimization.
Database access
- Project with Select to fetch only needed columns, and use AsNoTracking for read-only queries.
- Avoid N+1 queries; use Include or projections.
- Page every list with Skip and Take (or keyset paging).
- Add indexes for the filters and sorts you actually use.
- Use ExecuteUpdate and ExecuteDelete for bulk changes instead of loading entities.
Caching and responses
Cache data that is read often and changes rarely, and make responses small.
builder.Services.AddOutputCache();
builder.Services.AddHybridCache(); // .NET 9+: in-memory + optional distributed cache
builder.Services.AddResponseCompression(o => o.EnableForHttps = true);
app.UseResponseCompression();
app.MapStaticAssets(); // .NET 9+: fingerprinted, compressed static files
public class CatalogService(HybridCache cache, ShopContext db)
{
public ValueTask<List<CategoryDto>> CategoriesAsync(CancellationToken ct) =>
cache.GetOrCreateAsync("categories", async token =>
await db.Categories.AsNoTracking()
.Select(c => new CategoryDto(c.Id, c.Name)).ToListAsync(token),
cancellationToken: ct);
}Code habits
- Async all the way; never block with .Result or .Wait().
- Use IHttpClientFactory and keep HTTP calls off the hot path where possible.
- Move slow work (emails, PDFs, image processing) to background services.
- Avoid large objects per request (big byte arrays, huge strings); stream files instead.
- Run in Release mode on a current .NET version; each release brings free speed-ups.
Interview question
A page in your ASP.NET Core app is slow. How do you find out why?
Measure before changing anything. Look at the request in Application Insights or OpenTelemetry traces to see where time goes: database calls, HTTP dependencies or the app itself. Turn on EF Core command logging to spot N+1 queries, wide selects or missing paging, and check execution plans for missing indexes. Use dotnet-counters to rule out thread-pool starvation, then fix the largest cost and measure again.
Want the full details? Read the official Microsoft documentation for this topic.