{"id":21672553,"url":"https://github.com/redis-developer/basic-redis-rate-limiting-demo-csharp-dot-net","last_synced_at":"2025-08-30T23:37:54.620Z","repository":{"id":49444648,"uuid":"342769183","full_name":"redis-developer/basic-redis-rate-limiting-demo-csharp-dot-net","owner":"redis-developer","description":"Basic redis rate limiting demo written in C# .net","archived":false,"fork":false,"pushed_at":"2023-06-27T07:43:41.000Z","size":2474,"stargazers_count":6,"open_issues_count":0,"forks_count":3,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-12T03:53:09.561Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/redis-developer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-27T04:21:45.000Z","updated_at":"2024-01-29T06:18:18.000Z","dependencies_parsed_at":"2022-07-30T02:18:02.345Z","dependency_job_id":null,"html_url":"https://github.com/redis-developer/basic-redis-rate-limiting-demo-csharp-dot-net","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis-developer%2Fbasic-redis-rate-limiting-demo-csharp-dot-net","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis-developer%2Fbasic-redis-rate-limiting-demo-csharp-dot-net/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis-developer%2Fbasic-redis-rate-limiting-demo-csharp-dot-net/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis-developer%2Fbasic-redis-rate-limiting-demo-csharp-dot-net/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/redis-developer","download_url":"https://codeload.github.com/redis-developer/basic-redis-rate-limiting-demo-csharp-dot-net/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248514209,"owners_count":21116899,"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":[],"created_at":"2024-11-25T13:30:02.664Z","updated_at":"2025-04-12T03:53:16.316Z","avatar_url":"https://github.com/redis-developer.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"﻿\u003cdiv style=\"position: absolute; top: 0px; right: 0px;\"\u003e\n    \u003cimg width=\"200\" height=\"200\" src=\"https://redislabs.com/wp-content/uploads/2020/12/RedisLabs_Illustration_HomepageHero_v4.svg\"\u003e\n\u003c/div\u003e\n\u003cdiv style=\"height: 150px\"\u003e\u003c/div\u003e\n\n# Rate Limiting app in .NET using Redis\n\nThis demo shows how how to use Redis in .NET 5 to implement IP Rate limiting to prevent excessive calls to your app from a single client.\n\n## Technical Stack\n\n- Frontend: ASP.NET Core MVC\n- Backend: ASP.NET Core MVC / Redis\n\n## How it works?\n\n### 1. How the data is stored:\n\n- New responses are added key-ip: `SETNX your_ip:PING limit_amount`\n\n  - E.g `SETNX 127.0.0.1:PING 10`\n    \u003ca href=\"https://redis.io/commands/setnx\"\u003emore information\u003c/a\u003e\n\n- Set a timeout on key: `EXPIRE your_ip:PING timeout`\n  - E.g `EXPIRE 127.0.0.1:PING 1000`\n    \u003ca href=\"https://redis.io/commands/expire\"\u003emore information\u003c/a\u003e\n\n### 2. How the data is accessed:\n\n- Next responses are get bucket: `GET your_ip:PING`\n\n  - E.g `GET 127.0.0.1:PING`\n    \u003ca href=\"https://redis.io/commands/get\"\u003emore information\u003c/a\u003e\n\n- Next responses are changed bucket: `DECRBY your_ip:PING amount`\n\n  - E.g `DECRBY 127.0.0.1:PING 1`\n    \u003ca href=\"https://redis.io/commands/decrby\"\u003emore information\u003c/a\u003e\n\n#### Code used for configuring rate limiting\n\nWhen configuring constructing our app's middleware in Startup.cs, we initialize the cache client and inject it into our services. We then pull from the configuration the `IpRateLimit` section, and use that as the configuration for `IpRateLimitOptions`\n\n```C#\nusing AspNetCoreRateLimit;\n// ...\n\nservices.AddStackExchangeRedisCache(options =\u003e\n{\n    options.ConfigurationOptions = ConfigurationOptions.Parse(redisConnectionUrl);\n});\n\nservices.Configure\u003cIpRateLimitOptions\u003e(Configuration.GetSection(\"IpRateLimit\"));\nservices.AddSingleton\u003cIIpPolicyStore, DistributedCacheIpPolicyStore\u003e();\nservices.AddSingleton\u003cIRateLimitCounterStore, DistributedCacheRateLimitCounterStore\u003e();\nservices.AddSingleton\u003cIRateLimitConfiguration,RateLimitConfiguration\u003e();\n\n//...\napp.UseIpRateLimiting();\n```\n\nThe `IpRateLimit` section is from the `appsettings.json` file:\n\n```json\n\"IpRateLimit\": {\n  \"EnableEndpointRateLimiting\": true,\n  \"StackBlockedRequests\": false,\n  \"RealIPHeader\": \"X-Real-IP\",\n  \"ClientIdHeader\": \"X-ClientId\",\n  \"HttpStatusCode\": 429,\n  \"GeneralRules\": [\n    {\n      \"Endpoint\": \"*:/api/*\",\n      \"Period\": \"10s\",\n      \"Limit\": 10\n    }\n  ]\n}\n```\n\nThis section dictates the period the path which limitations will be applied to, `Endpoint`, the period over which restrictions are considered, `Period`, and the Limit for the number of requests permitted in that period `Limit`\n\n---\n\n## How to run it locally?\n\n```\ngit clone https://github.com/redis-developer/basic-redis-rate-limiting-demo-csharp-dot-net.git\n```\n\n#### Write in environment variable or Dockerfile actual connection to Redis:\n\n```\n   REDIS_ENDPOINT_URL = \"Redis server URI:PORT\"\n   REDIS_PASSWORD = \"Password to the server\"\n```\n\n#### Run backend\n\n```sh\ndotnet run\n```\n\nStatic content runs automatically with the backend part.\n\n## Try it out\n\n#### Deploy to Heroku\n\n\u003cp\u003e\n    \u003ca href=\"https://heroku.com/deploy\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://www.herokucdn.com/deploy/button.svg\" alt=\"Deploy to Heorku\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n#### Deploy to Google Cloud\n\n\u003cp\u003e\n    \u003ca href=\"https://deploy.cloud.run\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://deploy.cloud.run/button.svg\" alt=\"Run on Google Cloud\" width=\"150px\"/\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredis-developer%2Fbasic-redis-rate-limiting-demo-csharp-dot-net","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fredis-developer%2Fbasic-redis-rate-limiting-demo-csharp-dot-net","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredis-developer%2Fbasic-redis-rate-limiting-demo-csharp-dot-net/lists"}