A simple API rate limiter built using Node.js and Redis. This rate limiter controls the number of API requests a user can make within a specified time frame. It's useful for protecting your server from being overwhelmed by excessive requests.
- Limit the number of requests based on a specified time window.
- Uses Redis for fast, efficient request counting.
- Supports API key-based rate limiting via request headers.
- Easily configurable rate limits.
git clone https://www.github.com/Aviral0702/API-Rate-Limiter-Redis.git
cd API RATE-LIMITER
npm install
Make sure redis is running on Port:6379. If you are using different configuration for redis, update the settings for your redis in the code.
npm run start
This will run the backend server for the application.
curl -X POST http://localhost:5000/api/testing
-H "x-api-key: your-api-key-here"
for i in {1..100}; do
curl -X POST http://localhost:5000/api/testing
-H "x-api-key: your-api-key-here"
done
Also for the testing purposes I have created a script in the application which is named as testRateLimiter.js
, in this script you can change the number of request and change the apiKey as per your API Key provided when you registered.
Run node ./testRateLimiter.js
to run the file.
Make sure Redis server is up and running.
API-RATE-LIMITER/
├── config
├── dbconfig.js
├── middleware
├── rateLimiter.js
├── models
├── userModel.js
├── routes
├── apiRoutes.js
├── testingAPI.js
├── .env
├── server.js # Main server file
├── package.json # Project dependencies
└── README.md # Project documentation
- Implement different rate limits for different user roles (e.g., free, premium).
- Add support for custom rate limit settings via environment variables.
- Implement IP-based rate limiting as an alternative to API key-based limiting.
- Add a dashboard for monitoring rate limit statistics and usage metrics.
- Optimize Redis configuration for better performance in high-traffic scenarios.
- Add unit tests for the rate limiter to ensure reliability.
- Handle Redis connection failures gracefully with fallback mechanisms.
- Improve logging with a proper logging library like
winston
ormorgan
. - Refactor code to use TypeScript for better type safety and maintainability.
- Fix the issue where expired keys are not being cleared immediately.
- Address cases where rate limiting does not reset properly after the time window.
- Refactor the rate limiter middleware to be more modular.
- Remove any hardcoded values and replace them with configuration options.
- Perform code linting and formatting for consistency.
- Add support for distributed rate limiting across multiple servers.
- Allow dynamic adjustment of rate limits based on server load.
- Integrate with a monitoring tool like Prometheus for tracking metrics.