{"id":16534417,"url":"https://github.com/cristipufu/aspnetcore-redis-rate-limiting","last_synced_at":"2025-04-12T15:36:32.600Z","repository":{"id":63461788,"uuid":"559625219","full_name":"cristipufu/aspnetcore-redis-rate-limiting","owner":"cristipufu","description":"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.","archived":false,"fork":false,"pushed_at":"2024-05-15T18:43:40.000Z","size":546,"stargazers_count":193,"open_issues_count":16,"forks_count":20,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-17T06:14:19.514Z","etag":null,"topics":["asp-net-core","aspnetcore","distributed","net7","net8","rate-limit","rate-limit-redis","rate-limiter","rate-limiter-api","rate-limiting","redis","redis-cache","redis-database"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cristipufu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-30T17:35:46.000Z","updated_at":"2024-06-13T09:26:23.116Z","dependencies_parsed_at":"2023-02-10T03:15:52.524Z","dependency_job_id":"bfd03af7-32b7-4764-a427-0195c415a0d8","html_url":"https://github.com/cristipufu/aspnetcore-redis-rate-limiting","commit_stats":{"total_commits":78,"total_committers":4,"mean_commits":19.5,"dds":0.3076923076923077,"last_synced_commit":"6824756096a0076e3e5782a44367cba37cff294b"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristipufu%2Faspnetcore-redis-rate-limiting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristipufu%2Faspnetcore-redis-rate-limiting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristipufu%2Faspnetcore-redis-rate-limiting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristipufu%2Faspnetcore-redis-rate-limiting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cristipufu","download_url":"https://codeload.github.com/cristipufu/aspnetcore-redis-rate-limiting/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248590357,"owners_count":21129798,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asp-net-core","aspnetcore","distributed","net7","net8","rate-limit","rate-limit-redis","rate-limiter","rate-limiter-api","rate-limiting","redis","redis-cache","redis-database"],"created_at":"2024-10-11T18:24:06.592Z","updated_at":"2025-04-12T15:36:32.576Z","avatar_url":"https://github.com/cristipufu.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# aspnetcore-redis-rate-limiting\n\n[![NuGet](https://img.shields.io/nuget/v/RedisRateLimiting)](https://www.nuget.org/packages/RedisRateLimiting)\n[![NuGet](https://img.shields.io/nuget/v/RedisRateLimiting.AspNetCore)](https://www.nuget.org/packages/RedisRateLimiting.AspNetCore) \n[![Nuget](https://img.shields.io/nuget/dt/RedisRateLimiting)](https://www.nuget.org/packages/RedisRateLimiting) \n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=cristipufu_aspnetcore-redis-rate-limiting\u0026metric=coverage\u0026token=5fea970e583d49909265cbcc99a54f2b5a9d3c19)](https://sonarcloud.io/summary/new_code?id=cristipufu_aspnetcore-redis-rate-limiting)\n[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=cristipufu_aspnetcore-redis-rate-limiting\u0026metric=code_smells\u0026token=5fea970e583d49909265cbcc99a54f2b5a9d3c19)](https://sonarcloud.io/summary/new_code?id=cristipufu_aspnetcore-redis-rate-limiting)\n[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=cristipufu_aspnetcore-redis-rate-limiting\u0026metric=vulnerabilities\u0026token=5fea970e583d49909265cbcc99a54f2b5a9d3c19)](https://sonarcloud.io/summary/new_code?id=cristipufu_aspnetcore-redis-rate-limiting)\n[![GitHub](https://img.shields.io/github/license/cristipufu/aspnetcore-redis-rate-limiting)](https://github.com/cristipufu/aspnetcore-redis-rate-limiting/blob/master/LICENSE)\n\nSet 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](https://devblogs.microsoft.com/dotnet/announcing-rate-limiting-for-dotnet/).\n\nFor more advanced use cases you can check out the [official documentation here](https://learn.microsoft.com/en-us/aspnet/core/performance/rate-limit?view=aspnetcore-7.0).\n\n\n\n# install\n\n```xml\nPM\u003e Install-Package RedisRateLimiting\n```\n```\nTargetFramework: net7.0; net8.0\n\nDependencies:\nStackExchange.Redis\nSystem.Threading.RateLimiting\n```\n\n```xml\nPM\u003e Install-Package RedisRateLimiting.AspNetCore\n```\n```\nTargetFramework: net7.0; net8.0\n\nDependencies:\nRedisRateLimiting\n```\n\n# strategies\n\n## Concurrent Requests Rate Limiting\n\n\u003cbr\u003e\n\nConcurrency 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.\n\nInstead 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\".\n\n\u003cbr\u003e\n\n![concurrency](https://github.com/cristipufu/aspnetcore-redis-rate-limiting/blob/master/docs/concurrency.png)\n\n\u003cbr\u003e\n\nYou can use a new instance of the [RedisConcurrencyRateLimiter](https://github.com/cristipufu/aspnetcore-redis-rate-limiting/blob/master/src/RedisRateLimiting/Concurrency/RedisConcurrencyRateLimiter.cs) class or configure the predefined extension method:\n\n```C#\nbuilder.Services.AddRateLimiter(options =\u003e\n{\n    options.AddRedisConcurrencyLimiter(\"demo_concurrency\", (opt) =\u003e\n    {\n        opt.ConnectionMultiplexerFactory = () =\u003e connectionMultiplexer;\n        opt.PermitLimit = 5;\n        // Queue requests when the limit is reached\n        //opt.QueueLimit = 5 \n    });\n});\n```\n\n\u003cbr\u003e\n\n![concurrent_queuing_requests](https://user-images.githubusercontent.com/3955285/201516823-f0413ad7-de83-4393-acd7-a2f7c8c1e359.gif)\n\n\u003cbr\u003e\n\n## Fixed Window Rate Limiting\n\n\u003cbr\u003e\n\nThe 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.\n\n\u003cbr\u003e\n\n![fixed_window](https://github.com/cristipufu/aspnetcore-redis-rate-limiting/blob/master/docs/fixed_window.png)\n\n\u003cbr\u003e\n\nYou can use a new instance of the [RedisFixedWindowRateLimiter](https://github.com/cristipufu/aspnetcore-redis-rate-limiting/blob/master/src/RedisRateLimiting/FixedWindow/RedisFixedWindowRateLimiter.cs) class or configure the predefined extension method:\n\n```C#\nbuilder.Services.AddRateLimiter(options =\u003e\n{\n    options.AddRedisFixedWindowLimiter(\"demo_fixed_window\", (opt) =\u003e\n    {\n        opt.ConnectionMultiplexerFactory = () =\u003e connectionMultiplexer;\n        opt.PermitLimit = 1;\n        opt.Window = TimeSpan.FromSeconds(2);\n    });\n});\n```\n\n\u003cbr\u003e\n\n## Sliding Window Rate Limiting\n\n\u003cbr\u003e\n\nUnlike 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.\n\n\u003cbr\u003e\n\n![fixed_window](https://github.com/cristipufu/aspnetcore-redis-rate-limiting/blob/master/docs/sliding_window.png)\n\n\u003cbr\u003e\n\nYou can use a new instance of the [RedisSlidingWindowRateLimiter](https://github.com/cristipufu/aspnetcore-redis-rate-limiting/blob/master/src/RedisRateLimiting/SlidingWindow/RedisSlidingWindowRateLimiter.cs) class or configure the predefined extension method:\n\n```C#\nbuilder.Services.AddRateLimiter(options =\u003e\n{\n    options.AddRedisSlidingWindowLimiter(\"demo_sliding_window\", (opt) =\u003e\n    {\n        opt.ConnectionMultiplexerFactory = () =\u003e connectionMultiplexer;\n        opt.PermitLimit = 1;\n        opt.Window = TimeSpan.FromSeconds(2);\n    });\n});\n```\n\n\u003cbr\u003e\n\n## Token Bucket Rate Limiting\n\n\u003cbr\u003e\n\nToken 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.\n\n\u003cbr\u003e\n\n![token_bucket](https://github.com/cristipufu/aspnetcore-redis-rate-limiting/blob/master/docs/token_bucket.png)\n\n\u003cbr\u003e\n\nYou can use a new instance of the [RedisTokenBucketRateLimiter](https://github.com/cristipufu/aspnetcore-redis-rate-limiting/blob/master/src/RedisRateLimiting/TokenBucket/RedisTokenBucketRateLimiter.cs) class or configure the predefined extension method:\n\n```C#\nbuilder.Services.AddRateLimiter(options =\u003e\n{\n    options.AddRedisTokenBucketLimiter(\"demo_token_bucket\", (opt) =\u003e\n    {\n        opt.ConnectionMultiplexerFactory = () =\u003e connectionMultiplexer;\n        opt.TokenLimit = 2;\n        opt.TokensPerPeriod = 1;\n        opt.ReplenishmentPeriod = TimeSpan.FromSeconds(2);\n    });\n});\n```\n\n\u003cbr\u003e\n\n## snippets\n\nThese samples intentionally keep things simple for clarity.\n\n- [Custom Rate Limiting Policies](https://github.com/cristipufu/aspnetcore-redis-rate-limiting/wiki/Custom-Rate-Limiting-Policies)\n- [Rate Limiting Headers](https://github.com/cristipufu/aspnetcore-redis-rate-limiting/wiki/Rate-Limiting-Headers)\n\n\u003cbr\u003e\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcristipufu%2Faspnetcore-redis-rate-limiting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcristipufu%2Faspnetcore-redis-rate-limiting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcristipufu%2Faspnetcore-redis-rate-limiting/lists"}