~/blog/dotnet-performance-tuning-api-throughput
cd ../blog
dotnet-performance-tuning-api-throughput.md
May 20, 20268 min read
Software Architecture

Deep-Dive: .NET Core API Performance Tuning & Memory Optimization

[.NET Core][Performance][GC Optimization][APIs]

In high-throughput microservices, CPU utilization and Garbage Collector (GC) pauses are often the silent killers of low-latency SLAs. Recently, we undertook a migration and optimization effort on a core API endpoint handling over 15,000 requests per second. Here is the engineering breakdown of how we achieved a 40% reduction in response latency.

1. The Problem: Gen 0/1 GC Spikes

Through detailed memory profiling using dotnet-dump and PerfView, we identified that our primary bottleneck was GC collection pauses. Specifically, large amounts of short-lived objects were being allocated per request, causing frequent Generation 0 and Generation 1 Garbage Collections that paused thread execution.

2. The Fix: Structs, ArrayPool, and Span<T>

We systematically refactored the request pipeline to reduce heap allocations:

  • ArrayPool & MemoryPool: Instead of allocating new byte arrays for body deserialization on every request, we leased arrays from ArrayPool<byte>.Shared and returned them immediately after processing.
  • Span<T> and ReadOnlySpan<T>: We converted string slicing operations into zero-allocation spans. This prevented millions of string allocations per minute.
  • ValueTask: Asynchronous methods that frequently complete synchronously were changed from returning Task<T> to ValueTask<T>, eliminating Task object allocations.
// Zero-allocation parsing snippet
public ReadOnlySpan<char> ExtractToken(ReadOnlySpan<char> header) {
    int index = header.IndexOf("Bearer ");
    if (index == -1) return ReadOnlySpan<char>.Empty;
    return header.Slice(index + 7);
}

3. Results and Metrics

After deploying these optimizations, load tests run via k6 showed a drop in p99 latency from 180ms to 42ms. Heap allocations per request dropped from 14KB to under 200 bytes. This not only improved user experience but also slashed our container resource requirements by half.

$ whoami

MR

Boda Madhukar Reddy

// Software Architect @ Revalsys Technologies

Building high-throughput .NET Core systems, load-testing with k6 + Grafana, and engineering AI-driven automation tools. Writing about real-world engineering problems and production-first solutions.