Course Content
Data Structures & Algorithms
Full Stack Web Development
Understanding and playing with DOM (Document Object Model)
0/2
MERN project
0/2
Low Level System Design
LLD Topics
High Level System Design
Fast-Track to Full Spectrum Software Engineering
1. Rate Limiting API Design Overview

The System APIs for a rate limiter are essentially the endpoints and interactions that allow users and systems to check or enforce rate limits. These APIs could be part of the backend system or an API gateway that handles requests before they reach the application services.

 

You can break down these APIs into the following types:

 

  1. Request Checking API
  2. Reset or Refill API
  3. Status or Analytics API
  4. Custom Rate Limit Configuration API

 

2. Common Rate Limiting System APIs

1. Check Request API

This is the core API that checks whether a request should be allowed based on the rate limiting rules set for a particular user or resource.

 

Endpoint: POST /rate-limiter/check

 

Description: This API is called each time a user or client makes a request to the service. It verifies if the request exceeds the allowed rate limit within the defined time window. It checks against the defined rate limiting algorithm (e.g., token bucket, fixed window, etc.).

 

Request Parameters:

 

  • user_id (string): The identifier for the user or client.
  • api_key (string, optional): API key that can be used to track rate limits per API or per application.
  • resource (string): The specific API endpoint or resource being requested.

 

Response:

 

  • 200 OK: If the request is allowed.
  • 429 Too Many Requests: If the rate limit has been exceeded.

 

Response body could include:

 

  • retry_after (integer): The time in seconds until the rate limit resets.
  • limit (integer): The maximum number of requests allowed in the window.
  • remaining (integer): The remaining number of requests allowed in the current window.

 

Example:

 

{
"status": "ok",
"remaining": 20,
"limit": 100,
"reset": "2025-03-31T15:00:00Z"
}


2. Reset or Refill Rate API

This API is used to manually reset or refill the rate limit for a specific user or resource, in cases where you want to programmatically adjust limits or reset the counts (e.g., after a specific event or when testing).

 

Endpoint: POST /rate-limiter/reset

 

Description: This API allows a system administrator or automated system to reset the rate limit for a user or resource before the predefined time window expires. This is useful when applying manual changes to rate limits (e.g., during maintenance).

 

Request Parameters:

 

  • user_id (string): The identifier of the user or client for which the limit should be reset.
  • api_key (string): The API key tied to the resource, if needed.
  • resource (string): The API endpoint or service resource to reset the limit for.

 

Response:

 

  • 200 OK: Successfully reset the rate limit.
  • 404 Not Found: If the user or resource doesn’t exist.

 

Example:

 

{
"status": "reset successful",
"user_id": "user123",
"new_limit": 100,
"next_reset_time": "2025-03-31T15:00:00Z"
}


3. Get Rate Limit Status API

This API can be used to retrieve information about the current rate limits, including the total requests made, remaining requests, and when the limit will reset. It’s useful for monitoring and alerting.

 

Endpoint: GET /rate-limiter/status

 

Description: This API provides the current status of the rate limiter for a specific user or client. It helps monitor whether they are close to hitting their limit and when they can make another request.

 

Request Parameters:

 

  • user_id (string): The identifier of the user or client.
  • resource (string): The API endpoint or resource to check the rate limit for.

 

Response:

 

  • 200 OK: Rate limit status is successfully returned.
  • 429 Too Many Requests: If the user has exceeded the limit.

 

Example:

 

{
"status": "ok",
"user_id": "user123",
"remaining_requests": 50,
"reset_time": "2025-03-31T15:00:00Z",
"limit": 100
}


4. Set Custom Rate Limit API

This API allows for dynamically adjusting rate limits per user, API key, or resource. It’s useful for businesses with different needs per client or for temporary promotions or events that require higher rate limits.

 

Endpoint: POST /rate-limiter/set-limit

 

Description: This API allows administrators or automated systems to update the rate limits for a specific user, resource, or client API key.

 

Request Parameters:

 

  • user_id (string): The identifier of the user.
  • resource (string): The resource for which the rate limit should be applied.
  • new_limit (integer): The new limit for requests.
  • time_window (string): The time window for the limit (e.g., 1m, 1h, 1d).

 

Response:

 

  • 200 OK: Successfully set the new rate limit.
  • 400 Bad Request: If invalid parameters are provided.

 

Example:

 

{
"status": "limit set",
"user_id": "user123",
"new_limit": 500,
"time_window": "1h"
}


3. Usage and Example Flow

Let’s look at a typical flow of how these APIs might be used in an application:

 

  1. User makes a request to an API. The API gateway calls the POST /rate-limiter/check API to check if the user has exceeded their rate limit for the given resource (e.g., /login).
  2. If the request is allowed (i.e., the user hasn’t exceeded their limit), the request is processed, and the user is allowed to proceed. If the user exceeds their limit, a 429 Too Many Requests error is returned along with a retry_after time.
  3. If an admin wants to reset the rate limit (e.g., to test something or handle a special case), they can call POST /rate-limiter/reset for the affected user or resource.
  4. If the admin wants to set custom limits for a user or API key (e.g., during a promotion), they can call POST /rate-limiter/set-limit.
  5. The system can also periodically call GET /rate-limiter/status to monitor how close the users are to their limits and handle any issues proactively.

 

Summary of Rate Limiter System APIs

To summarize the Rate Limiter System APIs, here’s a quick recap of their purposes:

 

  1. Check Request API (POST /rate-limiter/check): Verifies if a user is within the allowed rate limit for a resource.
  2. Reset Rate Limit API (POST /rate-limiter/reset): Allows for manual resetting of the rate limit for a user or resource.
  3. Get Rate Limit Status API (GET /rate-limiter/status): Provides the current rate limit status (remaining requests, reset time).
  4. Set Custom Rate Limit API (POST /rate-limiter/set-limit): Sets or updates custom rate limits for specific users, API keys, or resources.
0% Complete
WhatsApp Icon

Hi Instagram Fam!
Get a FREE Cheat Sheet on System Design.

Hi LinkedIn Fam!
Get a FREE Cheat Sheet on System Design

Loved Our YouTube Videos? Get a FREE Cheat Sheet on System Design.