Skip to content
Tutorials Center
My learning

Topics

Lesson 42 of 52 ยท Testing, Performance and Deployment

Performance Tuning

Make ASP.NET Core apps faster: async all the way, response compression, output caching, fewer allocations, pooling, Native AOT and how to measure it all.

ASP.NET Core is already one of the fastest mainstream web frameworks, so most slow applications are slow for reasons of their own: a database query inside a loop, a thread blocked waiting for a network call, a megabyte of JSON nobody compresses, or work repeated for every request that could have been done once. Performance tuning is mostly about finding that one thing, and fixing it, rather than sprinkling micro-optimizations everywhere.

In this lesson you will learn the order in which to look for problems, then apply the techniques that matter most for web apps: asynchronous code, response compression, output caching, allocation control and pooling, and the leaner build options (slim builders and Native AOT). Every live example measures something, because the first rule of performance work is the same as in science: measure, change one thing, measure again.

Measure first

Without measurements you are guessing, and guesses about performance are usually wrong. Three tools cover most needs:

  • Stopwatch (System.Diagnostics): times a block of code. Good for a quick comparison, which is what we use here.
  • A load generator such as bombardier, wrk or k6: sends many concurrent requests and reports requests per second and latency percentiles.
  • Profilers and counters: dotnet-counters, dotnet-trace, Visual Studio or JetBrains profilers show where the CPU time, memory and threads go. Observability in Production covers metrics you can collect continuously.

Look at percentiles, not averages. If the average response is 50 ms but the 99th percentile is 3 seconds, one user in a hundred is having a terrible time. And always test in Release mode (dotnet run -c Release), since Debug builds are slower and behave differently.

flowchart LR
    A["Measure<br/>the baseline"] --> B["Find the<br/>biggest cost"]
    B --> C["Change<br/>one thing"]
    C --> D["Measure again"]
    D -->|"better"| E["Keep it"]
    D -->|"not better"| F["Revert"]
    E --> B
    F --> B

Tuning is a loop. Skipping the measuring steps is how you end up with complicated code that is no faster.

A rough order of where problems usually hide, biggest first: I/O (database and network calls), blocking (threads waiting), repeated work (missing caching), payload size (no compression, over-fetching), and last of all CPU-level details and allocations.

Async all the way

A web server handles thousands of requests with a limited number of thread pool threads. When a handler calls a database or another service, an await frees the thread to serve other requests while the I/O is in flight. A handler that blocks instead keeps its thread parked doing nothing. A few dozen blocked requests can exhaust the pool, and then even trivial requests wait: this is thread pool starvation, and it is among the most common causes of "the server is slow but the CPU is idle".

The rules are simple:

  • Use the Async version of every I/O API and await it. Never call .Result, .Wait() or .GetAwaiter().GetResult() on a task inside a request (Tasks and async/await and Common Async Pitfalls explain why).
  • Do not wrap synchronous work in Task.Run to look asynchronous: that just moves the work to another pool thread.
  • Pass the CancellationToken (HttpContext.RequestAborted or an endpoint parameter) through to every async call so abandoned requests stop working.
  • Run independent operations concurrently with Task.WhenAll instead of awaiting them one after another.

The last rule often gives the largest win, because latency adds up. The program below builds a "dashboard" from three slow parts, once sequentially and once concurrently. (It uses WebApplication.CreateSlimBuilder, which comes up again later in this lesson.)

C# Sequential awaits versus Task.WhenAll

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Both endpoints return the same text, but the concurrent version costs about one part's latency instead of three. Only combine calls this way when they are truly independent, when the downstream service can take the extra concurrency, and note one restriction: a single DbContext (or any non-thread-safe object) must not be used by two tasks at once.

Do not use Task.WhenAll to fire off thousands of calls at once. Unbounded concurrency overwhelms the target service and your own sockets. Limit it with a SemaphoreSlim or Parallel.ForEachAsync(..., new ParallelOptions { MaxDegreeOfParallelism = 8 }).

Response compression

Text compresses extremely well, and for slow or mobile connections smaller responses are often a bigger win than faster code. ASP.NET Core can compress responses with Brotli and gzip using AddResponseCompression and UseResponseCompression. The client announces what it understands with Accept-Encoding, the server picks an encoding and tells the client with Content-Encoding.

Most production systems compress at the reverse proxy or CDN (nginx, Cloudflare), which is usually cheaper than doing it in the app. In-app compression is right when you have no such layer, and it is simple to turn on. Two cautions: compression is off for HTTPS by default (EnableForHttps) because of the BREACH attack, which matters when a response mixes secrets with attacker-influenced text, and already-compressed formats (images, video, zip files) gain nothing.

C# Gzip response compression

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The compressed payload is a small fraction of the original, and the client still receives identical JSON after decompressing. In a real browser the Accept-Encoding header is sent automatically and decompression is invisible.

Output caching: do the work once

The cheapest request is one you never compute. If many users ask for the same expensive page or data, output caching stores the finished response on the server and replays it, skipping your handler and everything behind it. The full tour of caches is in Caching: Memory, Distributed and Output; here is the performance angle. Two ideas make output caching practical:

  • Policies decide what is cached, for how long, and what varies the cache key (query string, header values).
  • Tags label cached entries so you can evict exactly the entries made stale by a change, instead of waiting for them to expire.
C# Output caching with tag-based eviction

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Four requests ran the handler three times: the second was a cache hit, the third had a different cache key, and the fourth came after eviction. Be careful what you cache: a response that depends on the signed-in user must not be served to someone else. By default the output cache skips requests with an Authorization header or cookies, and you must opt in deliberately for personalized content.

Allocations, pooling and the garbage collector

Every object you allocate is later cleaned up by the garbage collector (GC). The GC is fast, but at thousands of requests per second, needless garbage (temporary strings, arrays, closures, LINQ chains in hot paths) adds CPU time and latency spikes. Reduce it only where measurements say it matters, for example in code that runs per request or per item:

  • Avoid repeated string concatenation in loops; use StringBuilder or build once.
  • Use Span<T> and stackalloc for short-lived buffers and parsing instead of allocating substrings and arrays.
  • Rent large buffers from ArrayPool<T>.Shared and return them, instead of allocating big arrays per request (large arrays go to the Large Object Heap, which is expensive to collect).
  • Stream instead of buffering. Writing a large result with IAsyncEnumerable<T> or directly to Response.Body keeps memory flat. Building a million-item List<T> first does not.
  • Reuse expensive objects: HttpClient through the factory, JsonSerializerOptions as a static readonly field (creating new options per call is a classic slowdown), Regex instances.
  • Return the cheapest type. A Task<T> allocates; ValueTask<T> can avoid it on paths that complete synchronously. This helps only in measured hot paths.

The next program measures allocated bytes with GC.GetAllocatedBytesForCurrentThread(), a precise and deterministic way to compare two implementations, since it counts bytes rather than time. It compares three ways to build the same text and the cost of renting a buffer from the pool versus allocating one each time.

C# Measuring allocations: concatenation, StringBuilder and ArrayPool

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The numbers are printed as true or false checks on purpose, because exact byte counts differ between runtimes, while the ratios stay dramatic. Treat such micro-measurements as a way to confirm an idea, never as a reason to make all code allocation-free: clear code that is fast enough beats clever code that is slightly faster.

If your server still allocates a lot after you fixed hot spots, check the GC mode. Server GC (ServerGarbageCollector in the project file, on by default for ASP.NET Core apps) uses a heap and thread per core and favors throughput. In containers with small CPU limits, concurrent and workstation settings can use less memory. Change them only with measurements in hand.

Database and serialization costs

For most real applications the database is the biggest cost, so tuning there beats everything above: select only the columns you need, avoid N+1 query patterns, add indexes, paginate lists (OpenAPI, Documentation and API Versioning covers pagination conventions) and use no-tracking queries for read-only data (EF Core in Practice).

For JSON, use the built-in System.Text.Json, keep a single shared JsonSerializerOptions, and consider source generation (a JsonSerializerContext with [JsonSerializable] attributes) which avoids run-time reflection, speeds up startup and is required for Native AOT. Do not serialize more than the client needs: smaller DTOs mean less CPU and less data on the wire (DTOs, Records and Mapping).

Slim builders and Native AOT

WebApplication.CreateBuilder() configures many defaults (all configuration sources, logging providers, HTTPS and HTTP/3 support, IIS integration). They are convenient, and each costs a little startup time and memory. Two leaner options exist, and the first example in this lesson already used one:

Builder What it gives you
WebApplication.CreateBuilder() the full set of defaults
WebApplication.CreateSlimBuilder() the essentials: JSON configuration, user secrets, console logging, but no IIS, no HTTPS/HTTP/3 and no EventLog; the choice for Native AOT
WebApplication.CreateEmptyBuilder(...) nothing at all; you add every service yourself

Native AOT (ahead-of-time compilation) turns your app into a single native executable: no JIT, no separate runtime, starts in milliseconds and uses much less memory. That suits containers that scale to zero, serverless functions and small cloud bills. The trade-offs are real: no run-time code generation, so libraries that depend on reflection (including parts of MVC, which is not AOT-friendly) do not work, and the JSON serializer needs source generation. Minimal APIs were built with AOT in mind. This code is read-only, because the runner here has neither the Web SDK nor an AOT toolchain:

XML
<!-- MyApi.csproj -->
<PropertyGroup>
  <PublishAot>true</PublishAot>
  <InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
C#
// Program.cs
using System.Text.Json.Serialization;

var builder = WebApplication.CreateSlimBuilder(args);

builder.Services.ConfigureHttpJsonOptions(options =>
    options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonContext.Default));

var app = builder.Build();
app.MapGet("/todos/{id}", (int id) => new Todo(id, "Learn AOT"));
app.Run();

record Todo(int Id, string Title);

[JsonSerializable(typeof(Todo))]
internal partial class AppJsonContext : JsonSerializerContext;

You publish with dotnet publish -c Release and get a self-contained native binary. The build reports AOT incompatibilities as warnings, so treat them as errors in an AOT project. Choose AOT when startup time and memory are your bottleneck; for a long-running service on a normal server, the regular JIT-compiled runtime often reaches higher peak throughput thanks to tiered compilation and profile-guided optimization. Deployment: Kestrel, Reverse Proxies and Containers shows how this fits into container images.

A practical checklist

  1. Measure with a load test in Release mode, and look at p95 or p99 latency.
  2. Make sure every I/O call is async, awaited and cancellable; remove .Result and .Wait().
  3. Fix the database first: queries, indexes, pagination, round trips.
  4. Cache what is expensive and shared: output cache, memory cache, CDN for static files.
  5. Compress text responses (at the proxy if you have one) and keep payloads small.
  6. Reuse expensive objects and avoid needless allocation in hot paths only.
  7. Consider slim builders and Native AOT when startup time or memory dominates.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is thread pool starvation, and what usually causes it in a web app?

    Blocked requests keep their threads parked while doing nothing. When the pool runs out of free threads, even trivial requests have to wait. Using await frees the thread during I/O.
  2. 2. Three independent calls take 200 ms each. About how long does awaiting them with Task.WhenAll take, compared with awaiting them one after another?

    Concurrent calls overlap, so the total is roughly the slowest one (200 ms). Awaiting sequentially adds the latencies (600 ms).
  3. 3. Why does output caching support tags?

    A tag groups cached entries. When the data changes, EvictByTagAsync removes all of them at once, instead of waiting for each to expire.
  4. 4. Which is the best first step when an endpoint is slow?

    Guessing wastes effort; the bottleneck is usually I/O such as a slow query, not CPU details. Measure, change one thing and measure again.
  5. 5. What is a good reason to choose Native AOT?

    AOT compiles to a native executable that starts in milliseconds and uses little memory, at the price of restrictions on reflection and dynamic code.

Summary

  • Measure before tuning: use Stopwatch for quick comparisons, load tests for real numbers, profilers for causes, and look at percentiles in Release builds.
  • Async all the way: never block on tasks, pass cancellation tokens through and use Task.WhenAll for independent work, with bounded concurrency.
  • Compress text responses, preferably at the proxy, and keep payloads small.
  • Output caching skips the handler entirely; use policies, vary rules and tags for targeted eviction, and never cache personalized responses by accident.
  • Reduce allocations in hot paths with StringBuilder, Span<T>, ArrayPool<T>, streaming and shared JsonSerializerOptions.
  • Slim builders trim defaults; Native AOT gives fast startup and low memory but requires source-generated JSON and AOT-friendly code.
  • The database is usually the largest cost, so fix queries and round trips first.

Next: Deployment: Kestrel, Reverse Proxies and Containers