What is Rate Limiting?
- Rate limiting is the process used to restrict the number of requests allowed to a particular resource in the specified time window.
- .NET Core 7 has an inbuilt rate limiter middleware using which we can configure settings related to rate limiting.
Why is Rate Limiting?
- Rate limiting is for limiting network traffic.
- Rate limiting protects our application from denial-of-service attacks (DoS).
- It helps us to manage server resources efficiently when a high volume of requests is received.
- Rate limiting helps us improve the performance of our application by reducing unnecessary delays to send back responses.
- Rate Limiting reduces cost. For example, suppose our API is publicly available and the end-user made a high volume of request. In that case, it may require additional server and network resources to manage that load which increase infrastructure cost.
Rate Limiting Algorithm
Fixed Window
- Fixed Window is a simple and memory-efficient algorithm that divides time into a fixed window or time interval corresponding to the rate-limiting definition, and if the number request limit is exceeded, then the further request is throttled.
- For example – Suppose the fixed window algorithm allows 20 requests per minute. In that case, the one window starts at 00.00.00 to 00.01.00, and it allows 20 requests within that window, and after a time frame of the fixed window, the request count renews at the beginning of every time frame.
Sliding Window
- The sliding window algorithm is a combination of fixed and Sliding log algorithms and is divide the window into multiple segments and counts the number of requests within each segment. The window slides each passing and discards the old request counts, and allows the new one’s current segment.
Token Bucket
- Token Bucket is a memory-efficient algorithm that uses a bucket to store a fixed number of tokens and maintained count.
- For example- Suppose have a 100-token limit for a bucket, and whenever the requests will come it will check whether the token is available in the bucket or not. If a token is available, it will refill new requests and decrease the token count. Otherwise, if the token limit is exceeded and not available in that case, requests are getting throttled.
Leaky Bucket
- The Leaky Bucket algorithm uses the bucket with a hole to store the number of tokens, and each token specifies the API request. It will be stored in FIFO (First in First out) queue with a fixed size to hold the number of requests if a new request will come that will be added at the end of the queue, and if the queue is getting full then throttled the subsequent request.
Sliding Log
- Sliding Log algorithm tracks a timestamp log for each API request, and that will be stored internally inside the hashed database and sorted by time. This algorithm helps to control the number of requests per unit of time and data transferred by the same. When a new request is processed then the sum of log entries corresponding to that particular user is calculated to find the request rate. If that rate exceeds the limit, then subsequent requests are throttled.
Rate Limiting in .NET Core 7
ASP.NET Core has built support for Rate Limiter and has middleware for the same.
Step 1

Join the conversation! Your thoughts help the community grow.