-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
One of the top customer asks for rate limiting is supporting distributed rate limiting. To make that work you need to be able to share state between each of the nodes doing the rate limiting. The two main architectures for doing that in .NET are using Orleans or Redis.
There are a number of moving pieces involved in either solution. Packaging those up into a library that can be redeployed would be substantially more work that creating a sample, as it would need considerable configuration flexibility, that is more easily expressed when you can modify the code. Using Aspire orchestration should simplify the setup configuration and make it easy enough that a developer can copy the sample and be able to "F5" and have it running without additional work.
The Aspire samples repo already exists, so is a good place to place such a sample. We can then refer to it from the rate-limiting documentation.
@ReubenBond has a couple of samples he wrote a few years ago:
- https://github.com/ReubenBond/DistributedRateLimiting.Orleans
- https://github.com/ReubenBond/DistributedRateLimiting.Redis
We should use one (or both) as the starting point. Suggestion is to do the Orleans version first.
Goals
A simple sample that can be downloaded and run. The sample should comprise of:
- A library project for the distributed sampling infrastructure
- A WebAPI project with an endpoint that has a rate limit defined using the library project
- A single page Blazor project that will invoke the API endpoint that is rate limited
- can just be a button and log of the API calls together with success/failure status
- has a list of endpoints and a simple round-robin approach to calling them - it should not use anything complicated to make the API calls
- Aspire Apphost
- creates multiple instances of the WebAPI project
- sets up the storage for Orleans or Redis
- passes the WebAPI endpoints to the blazor project so it knows what to hit
- Service defaults
- includes the metrics namespace for the rate limiting counters
Ideally the WebAPI project can use builder and apphost extension methods from the library to setup the rate limiting sharing. The policy definition with the constraints (10 / min, easy to demonstrate in the sample) should be in the WebAPI project. The ideal being that the sample can be cloned and just the WebAPI project needs to be replaced with what developers want to protect with rate limiting.