Decepticon.RateLimit.Redis 1.0.0

Rate limiter that allows you to add request throttling for service protection and daily/hourly limit for your application
Donate via Paypal: dekcodeofficial@gmail.com

Install-Package Decepticon.RateLimit.Redis -Version 1.0.0
dotnet add package Decepticon.RateLimit.Redis --version 1.0.0
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Decepticon.RateLimit.Redis, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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.

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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 233 12/22/2020