Public APIs are constantly exposed to automated bots, accidental traffic spikes, and abusive clients. Without proper safeguards, a single client can overwhelm your application, consume server resources, and degrade performance for every other user.
ASP.NET Core includes built-in rate limiting middleware that makes it easy to control how many requests a client can send within a given time period. By applying appropriate rate limiting policies, you can improve application stability, prevent denial-of-service scenarios, and ensure fair resource usage.
Rather than implementing custom throttling logic, this article explains how to use the built-in ASP.NET Core rate limiting middleware, choose the right algorithm, and configure production-ready policies.
Note: Rate limiting is not a replacement for authentication, authorization, or a Web Application Firewall (WAF). It complements these security measures by controlling request frequency.
Why API Rate Limiting Matters
Without rate limiting, APIs are vulnerable to:
Accidental traffic spikes
Brute-force login attempts
API abuse
Credential stuffing attacks
Excessive resource consumption
Increased infrastructure costs
Reduced application availability
Limiting request rates helps maintain consistent performance even during periods of unusually high traffic.
Common Rate Limiting Scenarios
Rate limiting can be applied to:
Login endpoints
Public APIs
File upload endpoints
Search APIs
Payment APIs
Password reset endpoints
Third-party integrations
AI-powered endpoints
Different endpoints often require different request limits depending on their workload and business requirements.
Rate Limiting Algorithms
ASP.NET Core supports multiple rate limiting strategies.
| Algorithm | Best For |
|---|---|
| Fixed Window | Simple APIs with predictable traffic |
| Sliding Window | Smoother request distribution |
| Token Bucket | APIs with occasional traffic bursts |
| Concurrency Limiter | Limiting simultaneous requests |
Each algorithm provides different trade-offs between fairness, burst handling, and implementation complexity.
Installing the Rate Limiting Middleware
The rate limiting middleware is included with modern versions of ASP.NET Core.
Register the middleware during application startup.
builder.Services.AddRateLimiter(options =>
{
options.AddFixedWindowLimiter(
"default",
policy =>
{
policy.PermitLimit = 100;
policy.Window = TimeSpan.FromMinutes(1);
policy.QueueLimit = 0;
});
});
This configuration allows up to 100 requests per minute before additional requests are rejected.
Enabling Rate Limiting
Enable the middleware within the request pipeline.
var app = builder.Build();
app.UseRateLimiter();
app.MapControllers();
app.Run();
Without calling UseRateLimiter(), configured policies won't be enforced.
Applying Rate Limiting to Endpoints
Policies can be applied to individual endpoints.
app.MapGet("/weather", () =>
{
return Results.Ok();
})
.RequireRateLimiting("default");
This approach allows different APIs to use different request limits.
Using a Sliding Window Policy
Sliding window limiting distributes requests more evenly.
builder.Services.AddRateLimiter(options =>
{
options.AddSlidingWindowLimiter(
"sliding",
policy =>
{
policy.PermitLimit = 60;
policy.Window = TimeSpan.FromMinutes(1);
policy.SegmentsPerWindow = 6;
});
});
Unlike a fixed window, the sliding window gradually expires requests instead of resetting all limits simultaneously.
Limiting Concurrent Requests
Some operations are expensive rather than frequent.
Use a concurrency limiter for these scenarios.
builder.Services.AddRateLimiter(options =>
{
options.AddConcurrencyLimiter(
"uploads",
policy =>
{
policy.PermitLimit = 5;
policy.QueueLimit = 10;
});
});
This configuration ensures that only five upload requests execute simultaneously.
Join the conversation! Your thoughts help the community grow.