The BucketPolicy implements the classic token bucket algorithm, which gives callers a balance of tokens that they spend on requests and that are automatically refilled over time. Unlike window-based strategies that enforce a hard request count per interval, the token bucket explicitly permits short bursts — as long as the bucket has tokens available — while still enforcing a sustainable average rate through periodic replenishment.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/nimanikoo/Dotnet-RateLimiter/llms.txt
Use this file to discover all available pages before exploring further.
How It Works
Imagine a bucket that holds up toTokenLimit tokens. Each request consumes one token. When the bucket is empty, requests either wait in a queue or are rejected. On a fixed replenishment schedule (ReplenishmentPeriod), TokensPerPeriod tokens are added back — up to the maximum TokenLimit.
Registration
The policy is registered insideAddCustomRateLimiter in RateLimiterExtensions.cs:
| Option | Value | Meaning |
|---|---|---|
TokenLimit | 5 | Maximum number of tokens the bucket can hold at any time |
QueueLimit | 2 | Maximum requests that can wait when the bucket is empty |
QueueProcessingOrder | OldestFirst | Oldest waiting requests receive replenished tokens first |
ReplenishmentPeriod | 5 seconds | How frequently tokens are added back to the bucket |
AutoReplenishment | true | The runtime automatically triggers replenishment on the timer |
TokensPerPeriod | 3 | Number of tokens added at each replenishment (capped at TokenLimit) |
RejectionStatusCode | 429 | HTTP status returned when the bucket is empty and the queue is full |
Applying the Policy
Decorate any controller action with[EnableRateLimiting("BucketPolicy")]. From TestController.cs:
Effective Rate
With the above configuration, the sustained throughput is:When to Use
| Scenario | Recommendation |
|---|---|
| Mobile or browser clients with search-as-you-type | ✅ BucketPolicy handles bursts well |
| File download or streaming endpoints with periodic access | ✅ Burst capacity absorbed naturally |
| APIs where a steady, predictable request rate is required | Consider SlidingWindowPolicy |
| Multi-instance / distributed deployments | Use Redis Fixed Window instead |
AutoReplenishment = true means the ASP.NET Core rate limiter infrastructure automatically fires a background timer every ReplenishmentPeriod to add TokensPerPeriod tokens back to the bucket. You do not need to implement or register a separate hosted service or background worker — replenishment is fully managed by the runtime as long as the application is running.