Decepticon.RateLimit.Redis 1.0.0

dotnet add package Decepticon.RateLimit.Redis --version 1.0.0
NuGet\Install-Package Decepticon.RateLimit.Redis -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Decepticon.RateLimit.Redis" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Decepticon.RateLimit.Redis --version 1.0.0
#r "nuget: Decepticon.RateLimit.Redis, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Decepticon.RateLimit.Redis as a Cake Addin
#addin nuget:?package=Decepticon.RateLimit.Redis&version=1.0.0

// Install Decepticon.RateLimit.Redis as a Cake Tool
#tool nuget:?package=Decepticon.RateLimit.Redis&version=1.0.0

This library allows you to add rate limiting and/or request throttling to your applications using Redis. It's all you need. Rate limiting defines your clients' quota, such as daily limit as an example. On the other hand, request throttling helps protect your system from spikes by smoothing out the traffic. Both algorithms use separate tracking in a data store.

This library is under MIT license.

Donation is much appreciated!! Donate via Paypal: dekcodeofficial@gmail.com

Quickly why?

  • To limit quota used by clients in your APIs/services for your business model
  • To protect your application from heavy traffic, abuse, or attacks
  • Throttling saves you money!

Why use this package?

  • Supper lightweight
  • Free + opensource
  • Easy to use
  • Simple, look at the code below
  • Compatible with both .NetFramework and .NetCore

Usages

Fixed Rate Limitting

Here's an example on how to do a daily limit for each of your clients.

// Create a rate limiter by pass in your Redis IDatabase instance to the constructor
var rateLimiter = new RateLimiter(redisDatabaseFromYourApp);

// Make the request such that each of your clients can only make 100 requests a day
var request = new FixedRateLimitRequest
{
    Key = "some_client_id",
    Capacity = 100,
    WindowSize = 24 * 60 * 60
};

var result = rateLimiter.Validate(request);
if (!result.IsAllowed)
{
    _logger.Log($"Denied: window resets in {result.ResetAfter} seconds");
    // Return an error like 429 Too Many Requests
}

// Code flow continues
... 

Request Throttling

Here's an example on how to do request throttling to allow each client to make 300 requests per minute but allow bursts of 50 requests or fewer as long as there's enough capacity.

// Create a rate limiter by pass in your Redis IDatabase instance to the constructor
var rateLimiter = new RateLimiter(redisDatabaseFromYourApp);

var request = new ThrottleRateLimitRequest
{
    Key = "some_client_identifier",
    Capacity = 50, // this is your bucket size
    RefillRate = 5 // per second
};

var result = rateLimiter.Validate(request);
if (!result.IsAllowed)
{
    _logger.Log($"Denied: Retry after {result.ResetAfter} seconds");
    // Return an error like 429 Too Many Requests
}

// Code flow continues
... 

Q&A and Troubleshooting

  • Why is it slow? The package doens't maintain any connection. It takes in the instance of the database to store data needed. You have to follow the best practices on how to manage connection for the technologies. For an example, in Redis, you have to create your singleton for your Redis connection.
  • How can I inject it? The library is made to be used a at a low level to be compatible with .NetFramework and .NetCore, so it's not specific for any injector so you have to create a repository facade for it.
  • How to report a bug? Go to the Github page and do it there.
  • What algorithms are they? The rate limit function uses Fixed Window. The throttling uses Token Bucket.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 1,698 12/22/2020