Member-only story
Rate Limiting API Endpoints in ASP.NET Core

This post shows (1) an ASP.NET Core Web API demo project which limits inbound HTTP requests from the internet and (2) an integration test project which tests the Web API rate limit using a TestServer
and an HttpClient
. One of the integration tests shows an example approach to send concurrent API requests using a Semaphore
in order to comply with the rate limit in the Web API application.
The demo Web API application has an API end point, /api/values/isPrime?number={n}
, which checks if an integer is a prime number or not. This Web API application utilizes the AspNetCoreRateLimit NuGet package to throttle the inbound HTTP requests with a rate limit of 2 requests per second. If the web server receives more than 2 HTTP requests per second, then the server denies extra requests and returns an HTTP status code 429 (Too Many Requests
).
The integration test project employs the same setups as the Web API project and tests the controller action method. We can validate the effect of rate limiting when the API endpoint is hit by a high volume of requests. We can also see a simple way to comply with the rate limiting policy and throttle concurrent HTTP requests so that the utilization rate of the API endpoint is maximized.
The whole solution is located at this GitHub repository.
Table of Contents
Throttling Web API Endpoints in ASP.NET Core
Integration Tests of the Rate Limit Scenarios
We first create an ASP.NET Core Web API project, which contains one controller (ValuesController
) with one action method (GetIsPrime(long number)
) that accepts HTTP Get
requests. This API endpoint takes an integer as a query parameter (?number={number}
) and returns a Boolean value representing whether the number is a prime number or not. Now, let’s step into the rate limiting zone.
Throttling Web API Endpoints in ASP.NET Core
A web API endpoint is an interface for API consumers to interact with the server side application. Web API endpoints can provide data Extract, Transform, Load (ETL) services, cognition services, and other computing services. Through an API endpoint, our system can further facilitate data operations in other systems…