An API rate limiter is a crucial component in many web applications and APIs to manage and control the rate at which clients or users can make requests to the API. Its primary purpose is to prevent abuse, ensure fair usage, protect the API server from overload, and maintain a high quality of service for all users. Rate limiting is often implemented to:
- Protect Resources
- Ensure Fairness
- Mitigate DDoS Attacks
- Billing and Monetization
This is a FastAPI implementation of rate limiter
- Python Virtual Environment - Follow this simple guide to create a virtual environment
- Docker Installation - Follow this guide to install docker on your local machine
- Open Docker and run a redis container instance using below command (in a terminal or cmd)
docker run -d --name redis-container -p 6379:6379 redis:latest - verify if the container is up and running using the command
docker ps -a - Create and Enter into your virtual environment
- cd to the root directory of the application
- Install the dependencies using following command
pip install -r requirements.txt - Once the dependencies are installed, we can run the program using following commands.
- To Run the Sliding Window Counter Algorithm based API rate limiter use the following command
uvicorn api:app --reload- To run all the other algorithms, Open the file
api2.py - Choose the algorithm by modifying line no.13 (Example: TokenBucket)
- Once the algorithm is chosen, run the app
uvicorn api2:app --reloadThe Sliding Window Counter Algorithm implementation uses redis cache. As the storage is centralized, this implementation supports scaling and running multiple app servers. You can make API calls to both the servers and see the consistent results. Following are the steps
- Open the venv
- Run the first app in port 8000
uvicorn api:app --reload --port 8000- Open another terminal instance and enter into venv
- Run the second app in port 7000
uvicorn api:app --reload --port 7000