skip to content

An ASP.NET Core 10 telemetry ingest sets Kestrel's MaxRequestBodySize and MinRequestBodyDataRate to null so large, slow device uploads stop failing; what defaults did that remove, and what would you do instead?

level: seniorimportance: should knowfreq 40%

answer

  1. KestrelServerLimits on options.Limits
  2. 30,000,000 bytes, then 413
  3. 240 bytes/s after a 5 s grace
  4. null means no limit at all
  5. per-request features beat global switches

basics

~20 s

It removed the 30,000,000-byte body cap (413 when exceeded) and the 240 bytes/s minimum upload rate after a 5-second grace (408). Instead, raise the body cap for the upload endpoint only, relax the rate without removing it, and cap connections.

solid answer

~40 s

`KestrelServerLimits.MaxRequestBodySize` defaults to 30,000,000 bytes; a larger body makes the read fail with `BadHttpRequestException` and a 413 response. `MinRequestBodyDataRate` defaults to 240 bytes/second after a 5-second grace period; a client sending more slowly is cut off with a 408. Setting both to `null` makes every endpoint accept unlimited bodies at any speed, so a few clients trickling bytes can hold connections, threads waiting on reads, and buffers indefinitely. Better: keep the global body cap and raise it for the upload endpoint alone through `IHttpMaxRequestBodySizeFeature` before the body is read; lower the server-wide rate to a non-null value such as 100 bytes/s with a 10-second grace (on HTTP/2 and HTTP/3 the per-request rate feature accepts only `null`); and set `MaxConcurrentConnections`, which is unlimited by default.

code

csharp · 20 lines
csharp
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core;

builder.WebHost.ConfigureKestrel(options =>
{
    options.Limits.MaxConcurrentConnections = 2_000;   // default: unlimited
    // Relaxed but enforced; per-request rates work on HTTP/1.1 only
    options.Limits.MinRequestBodyDataRate =
        new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10));
});

var app = builder.Build();

app.MapPost("/readings/batch", async (HttpContext ctx, BatchStore store) =>
{
    // Must run before anything reads the body
    ctx.Features.Get<IHttpMaxRequestBodySizeFeature>()!.MaxRequestBodySize = 200_000_000;
    await store.SaveAsync(ctx.Request.Body, ctx.RequestAborted);
    return Results.Accepted();
});

go deeper

for a junior

Recall that Kestrel caps request bodies at 30,000,000 bytes by default and answers 413 when a body is larger.

for a middle

Explain the main KestrelServerLimits defaults, which status each produces, and that null switches a limit off entirely.

for a senior

Demonstrate per-endpoint overrides through request features, a relaxed but enforced minimum data rate, a connection cap and streaming uploads, instead of disabling protections globally.

for a principal

Decide which tier owns size and rate limits for an ingestion fleet, and how limits are load-tested and reviewed when device firmware or batch sizes change.

## Where the limits live Kestrel enforces its protections in **`KestrelServerLimits`**, reached through `options.Limits` inside `builder.WebHost.ConfigureKestrel`. They act at the server level, before routing or model binding sees the request, and they apply to every endpoint unless a request overrides them through a feature on `HttpContext.Features`. ## The defaults in ASP.NET Core 10 | Limit | Default | When exceeded | |---|---|---| | `MaxRequestBodySize` | 30,000,000 bytes (30 MB, about 28.6 MiB) | 413 Payload Too Large | | `MinRequestBodyDataRate` | 240 bytes/s, 5 s grace | 408, connection closed | | `MinResponseDataRate` | 240 bytes/s, 5 s grace | connection aborted | | `RequestHeadersTimeout` | 30 s | 408 | | `KeepAliveTimeout` | 130 s | idle connection closed | | `MaxRequestHeadersTotalSize` | 32 KB | 431 | | `MaxRequestHeaderCount` | 100 | 431 | | `MaxRequestLineSize` | 8 KB | 414 | | `MaxRequestBufferSize` | 1 MB | reads pause (back-pressure) | | `MaxConcurrentConnections` | `null` (unlimited) | new connection closed, warning logged | | `Http2.MaxStreamsPerConnection` | 100 | extra streams refused | Two values are often quoted wrong: `KeepAliveTimeout` is 130 seconds in source (older documentation says 2 minutes), and the body limit is 30,000,000 bytes, not 30 MiB. ## What setting both to null does - **`MaxRequestBodySize = null`** removes the cap entirely. Any endpoint, not just the upload, will read whatever a client sends. An endpoint that buffers the body (JSON deserialization, form reading) now has no ceiling on memory per request. - **`MinRequestBodyDataRate = null`** removes slow-client protection. A client can open a request, send a few bytes, and stall. Each stalled request holds a connection and a pending read. Many of them together are the classic slow-body attack, and nothing in the default configuration caps the number of connections either. The failure the team saw was real: battery-powered sensors on poor links do upload slowly, and batched readings can exceed 30 MB. The fix was too wide. ## A narrower configuration 1. **Keep the global limits**, or tighten them, for everything except ingestion. 2. **Raise the body limit per request** on the ingest endpoint. Before reading the body, set `IHttpMaxRequestBodySizeFeature.MaxRequestBodySize`; after the body has started to be read the feature is read-only and setting it throws. (MVC's and Minimal APIs' attribute and metadata for this are covered with file uploads.) 3. **Relax, do not remove, the minimum rate.** `options.Limits.MinRequestBodyDataRate = new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10))` lets slow devices through while still evicting a stalled one. The grace period must exceed Kestrel's one-second heartbeat or the constructor throws. Set it server-wide: the per-request `IHttpMinRequestBodyDataRateFeature` accepts a custom rate only on HTTP/1.1, and the ingest also speaks HTTP/2 and HTTP/3. 4. **Bound concurrency** with `MaxConcurrentConnections`, sized from load tests, and keep `MaxConcurrentUpgradedConnections` in mind if WebSockets share the server. 5. **Stream** large uploads (read `Request.BodyReader` or copy to storage) rather than buffering them, so a raised limit does not translate into memory per request. ```csharp builder.WebHost.ConfigureKestrel(options => { options.Limits.MaxConcurrentConnections = 2_000; options.Limits.MinRequestBodyDataRate = new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10)); }); app.MapPost("/readings/batch", async (HttpContext ctx) => { ctx.Features.Get<IHttpMaxRequestBodySizeFeature>()!.MaxRequestBodySize = 200_000_000; await batchStore.SaveAsync(ctx.Request.Body, ctx.RequestAborted); return Results.Accepted(); }); ``` ## How limits surface to clients and operators - Body and header violations become `BadHttpRequestException` with a status code; if the response has not started, Kestrel writes that status. - Rejected connections from `MaxConcurrentConnections` are closed without a response, with a "rejected because the maximum number of concurrent connections has been reached" warning, and are counted in the `kestrel.rejected_connections` metric. - Data-rate timeouts are logged and counted, which makes them the first thing to check when mobile or IoT clients report dropped uploads. ## Reading the symptoms When devices report failures, map the symptom to the limit before changing anything: - **413 on large batches:** `MaxRequestBodySize` (or a per-endpoint override that is smaller). - **408 or a reset mid-upload from slow links:** `MinRequestBodyDataRate`; the Kestrel log names the timeout. - **431 from clients carrying large tokens:** `MaxRequestHeadersTotalSize` or `MaxRequestHeaderCount`. - **Connections refused under load with no HTTP status:** `MaxConcurrentConnections`, or the operating system's backlog. Raising a limit should follow evidence from these logs and metrics, and stay as narrow as the evidence. ## HTTP/2 and HTTP/3 nuance The per-request `IHttpMinRequestBodyDataRateFeature` exists on HTTP/2 and HTTP/3 requests, but there it can only be set to `null` to disable the check for that request; reading it or setting another value throws `NotSupportedException`, because many streams share one connection. The server-wide `MinRequestBodyDataRate` still applies to those connections, which is why the relaxed rate above is set globally.

  • Why does setting IHttpMaxRequestBodySizeFeature.MaxRequestBodySize sometimes throw InvalidOperationException?
    The feature becomes read-only once the request body has started to be read, and for upgraded or extended CONNECT requests. If middleware or model binding already consumed the body, it is too late; the override must run before the first read.
  • What does a client see when MaxConcurrentConnections is reached?
    Kestrel accepts the TCP connection and immediately disposes it without an HTTP response, logs a warning that the connection was rejected because the maximum was reached, and counts it in its metrics. The client sees a reset or closed connection, not a status code.
  • Why might you lower KeepAliveTimeout or keep it above a load balancer's idle timeout?
    `KeepAliveTimeout` (130 s) decides how long an idle connection stays open. If Kestrel closes idle connections before the proxy in front does, the proxy can reuse a connection Kestrel just closed and return errors; keeping Kestrel's timeout longer than the proxy's idle timeout avoids that race.

A warehouse loading dock turns away trucks over a weight limit and waves off any driver who unloads one box a minute after a short grace period. Removing both rules lets one slow driver block a bay all day; the better fix is a heavy-goods bay with its own, still enforced, rules.

  • Kestrel has no default body size limit.
  • MinRequestBodyDataRate = null is a harmless way to support slow clients.
  • KeepAliveTimeout defaults to 2 minutes in ASP.NET Core 10.
  • MaxConcurrentConnections defaults to 100 connections.
  • A request body over the limit returns 400 with a validation problem.
  • The body size feature can be changed after the body has been read.

Assumes ASP.NET Core 10.0 source defaults; KeepAliveTimeout is 130 seconds there, while older Kestrel documentation still says 2 minutes.