RedisRateLimiting 1.2.0
dotnet add package RedisRateLimiting --version 1.2.0
NuGet\Install-Package RedisRateLimiting -Version 1.2.0
<PackageReference Include="RedisRateLimiting" Version="1.2.0" />
paket add RedisRateLimiting --version 1.2.0
#r "nuget: RedisRateLimiting, 1.2.0"
// Install RedisRateLimiting as a Cake Addin #addin nuget:?package=RedisRateLimiting&version=1.2.0 // Install RedisRateLimiting as a Cake Tool #tool nuget:?package=RedisRateLimiting&version=1.2.0
aspnetcore-redis-rate-limiting
Set up a Redis backplane for Rate Limiting ASP.NET Core multi-node deployments. The library is build on top of the built-in Rate Limiting support that's part of .NET 7 and .NET 8.
For more advanced use cases you can check out the official documentation here.
install
PM> Install-Package RedisRateLimiting
TargetFramework: net7.0; net8.0
Dependencies:
StackExchange.Redis
System.Threading.RateLimiting
PM> Install-Package RedisRateLimiting.AspNetCore
TargetFramework: net7.0; net8.0
Dependencies:
RedisRateLimiting
strategies
Concurrent Requests Rate Limiting
<br>
Concurrency Rate Limiter limits how many concurrent requests can access a resource. If your limit is 10, then 10 requests can access a resource at once and the 11th request will not be allowed. Once the first request completes, the number of allowed requests increases to 1, when the second request completes, the number increases to 2, etc.
Instead of "You can use our API 1000 times per second", this rate limiting strategy says "You can only have 20 API requests in progress at the same time".
<br>
<br>
You can use a new instance of the RedisConcurrencyRateLimiter class or configure the predefined extension method:
builder.Services.AddRateLimiter(options =>
{
options.AddRedisConcurrencyLimiter("demo_concurrency", (opt) =>
{
opt.ConnectionMultiplexerFactory = () => connectionMultiplexer;
opt.PermitLimit = 5;
// Queue requests when the limit is reached
//opt.QueueLimit = 5
});
});
<br>
<br>
Fixed Window Rate Limiting
<br>
The Fixed Window algorithm uses the concept of a window. The window is the amount of time that our limit is applied before we move on to the next window. In the Fixed Window strategy, moving to the next window means resetting the limit back to its starting point.
<br>
<br>
You can use a new instance of the RedisFixedWindowRateLimiter class or configure the predefined extension method:
builder.Services.AddRateLimiter(options =>
{
options.AddRedisFixedWindowLimiter("demo_fixed_window", (opt) =>
{
opt.ConnectionMultiplexerFactory = () => connectionMultiplexer;
opt.PermitLimit = 1;
opt.Window = TimeSpan.FromSeconds(2);
});
});
<br>
Sliding Window Rate Limiting
<br>
Unlike the Fixed Window Rate Limiter, which groups the requests into a bucket based on a very definitive time window, the Sliding Window Rate Limiter, restricts requests relative to the current request's timestamp. For example, if you have a 10 req/minute rate limiter, on a fixed window, you could encounter a case where the rate-limiter allows 20 requests during a one minute interval. This can happen if the first 10 requests are on the left side of the current window, and the next 10 requests are on the right side of the window, both having enough space in their respective buckets to be allowed through. If you send those same 20 requests through a Sliding Window Rate Limiter, if they are all sent during a one minute window, only 10 will make it through.
<br>
<br>
You can use a new instance of the RedisSlidingWindowRateLimiter class or configure the predefined extension method:
builder.Services.AddRateLimiter(options =>
{
options.AddRedisSlidingWindowLimiter("demo_sliding_window", (opt) =>
{
opt.ConnectionMultiplexerFactory = () => connectionMultiplexer;
opt.PermitLimit = 1;
opt.Window = TimeSpan.FromSeconds(2);
});
});
<br>
Token Bucket Rate Limiting
<br>
Token Bucket is an algorithm that derives its name from describing how it works. Imagine there is a bucket filled to the brim with tokens. When a request comes in, it takes a token and keeps it forever. After some consistent period of time, someone adds a pre-determined number of tokens back to the bucket, never adding more than the bucket can hold. If the bucket is empty, when a request comes in, the request is denied access to the resource.
<br>
<br>
You can use a new instance of the RedisTokenBucketRateLimiter class or configure the predefined extension method:
builder.Services.AddRateLimiter(options =>
{
options.AddRedisTokenBucketLimiter("demo_token_bucket", (opt) =>
{
opt.ConnectionMultiplexerFactory = () => connectionMultiplexer;
opt.TokenLimit = 2;
opt.TokensPerPeriod = 1;
opt.ReplenishmentPeriod = TimeSpan.FromSeconds(2);
});
});
<br>
snippets
These samples intentionally keep things simple for clarity.
<br>
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- StackExchange.Redis (>= 2.7.33)
- System.Threading.RateLimiting (>= 7.0.1)
-
net8.0
- StackExchange.Redis (>= 2.7.33)
- System.Threading.RateLimiting (>= 8.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on RedisRateLimiting:
Package | Downloads |
---|---|
RedisRateLimiting.AspNetCore
AspNetCore Redis extension for rate limiting |
|
TinyFx.Extensions.RedisRateLimiting
Asp.Net 速率限制中间件 |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on RedisRateLimiting:
Repository | Stars |
---|---|
cristipufu/aspnetcore-redis-rate-limiting
Set up a Redis backplane for ASP.NET Core multi-node deployments, using the built-in Rate Limiting support that's part of .NET 7 and .NET 8.
|
Version | Downloads | Last updated |
---|---|---|
1.2.0 | 105,888 | 6/14/2024 |
1.1.0 | 236,211 | 11/16/2023 |
1.0.12 | 54,664 | 9/19/2023 |
1.0.11 | 32,655 | 5/28/2023 |
1.0.10 | 70,133 | 12/23/2022 |
1.0.9 | 654 | 12/20/2022 |
1.0.8 | 468 | 12/20/2022 |
1.0.7 | 532 | 12/14/2022 |
1.0.6 | 2,127 | 11/27/2022 |
1.0.5 | 142 | 11/3/2022 |
1.0.4 | 125 | 10/31/2022 |
1.0.3 | 168 | 10/30/2022 |