Rate limiting prevents an API from being saturated with requests, ensuring that the service provided is not degraded. Rate limiting is a tactic commonly employed by an API but can also be employed by a client that consumes from an API. We might see the latter scenario for an application consuming from a third-party API that imposes request-rate restrictions with punitive measures for consumers routinely in violation of the given restrictions. Recently, I encountered such a scenario.

A visualization of a rate-limited API client

Figure 1: A visualization of a rate-limited API client

Precedence for a Rate-Limited API Client

Some time ago, I started work on a personal project: a web app that displays match statistics about highly ranked players in a competitive video game. The statistics displayed on the web app are produced by a .NET Core console app. The console app runs on an hourly interval, pulling the latest match data from the game’s public API, aggregating the data, and subsequently storing it for later use by the web app. The game’s API imposes request-rate restrictions with the typical limitations seen for a public API: a consumer should not exceed X requests over Y seconds.Applications that are routinely in violation of the rate limit are blocked from accessing the API. Given that my console app attempts to pull all of the latest match data every hour, as quickly as possible, I set out to implement a rate limiter for the app to prevent it from being penalized by the API.

The Structure of this Series of Articles

In the following series of articles, I will document how I implemented a rate limiter for a .NET Core application. Each article in this series will have a separate focus,

  • The first article (what you are currently reading) puts focus on the rate limiter algorithm being used and how it can be implemented in .NET Core
  • The second article documents how the implementation of the rate limiter can be validated with unit tests
  • The third article provides additional analysis of the chosen rate limiter algorithm, and data from several simulations that make use of the rate limiter implementation
  • The last article details how the rate limiter implementation can be used by a .NET Core application to limit outbound requests to an API

The implementation I am about to discuss was developed with the intent of being used within a single application — and does not apply in a distributed setting. This is not to say that the algorithm could not be applied in a distributed setting or that the below code could not be modified to handle such a use case, but that it is out of the scope of this series of articles.

#rate-limiting #algorithms #networking #csharp #dotnet-core

Rate Limiting a .NET Core Application
1.35 GEEK