Desired Functionality:
- Implement rate limiting to control the number of requests processed per minute.
- Use Redis to track the request count for a sliding window of X minutes (quota).
- If the current request count exceeds the quota:
Instead of returning a 429 Too Many Requests error, schedule the message for processing after X hours. - After X hours have passed and the scheduled messages are processed, subsequent requests exceeding the quota will receive a 429 error.
What are best strategy to implement this?
One approach to implement rate limiting with Redis involves a sliding window mechanism. Here’s how it works:
Tracking Request Counts: For each incoming request, the system looks back at the past X minutes (sliding window size) to track the number of requests received from a specific IP address. Redis serves as the central data store for maintaining this tally.
Throttle Key: A unique key is used for each IP address (throttle key) to efficiently track their request counts within the sliding window.
Rate Limiting Logic:
-
When the current request count for a specific IP address (CurrentRequestCount) exceeds the allowed limit (AllowedLimit), the system takes action.
-
Scheduled Messages for Overflow: Instead of immediately rejecting the request, the system schedules the message for processing at a later time.
-
The scheduling delay is calculated based on the ratio between the
current request count and the allowed limit, multiplied by the
sliding window size in minutes (CurrentRequestCount / AllowedLimit *
SlidingWindowInMinutes). -
Handling Full Windows: If the next available time slot within the window is already filled, the system checks for subsequent slots until a suitable time is found within the X-hour spread allowed for scheduled messages.
This approach involves multiple calls to redis, updating throttleKey multiple time which can degrade performance and increase latency