CodexBloom - Programming Q&A Platform

How to Develop a Robust Rate Limiter in Node.js Using Redis with Sliding Window Algorithm?

👀 Views: 425 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-03
Node.js Redis Rate Limiting JavaScript

I've spent hours debugging this and I'm trying to debug I'm trying to debug I'm working on implementing a rate limiter in a Node.js application using Redis to handle API requests efficiently... I want to use a sliding window algorithm to ensure users can make a limited number of requests within a specific time frame. However, I'm running into issues with calculating the request counts accurately. Currently, I'm using the `ioredis` library to interact with Redis and storing user request counts with timestamps. The main challenge arises when a user makes requests at the edge of the time window. For instance, if a user sends requests just as the window rolls over, my implementation is incorrectly counting their total requests. Here's a simplified version of my current code: ```javascript const Redis = require('ioredis'); const redis = new Redis(); const RATE_LIMIT = 5; // Max requests const WINDOW_SIZE_IN_SECONDS = 60; async function rateLimiter(userId) { const currentTime = Math.floor(Date.now() / 1000); const key = `rate_limit:${userId}`; const userRequests = await redis.lrange(key, 0, -1); // Removing timestamps that are outside the current window await redis.lrem(key, 0, userRequests.filter(timestamp => currentTime - timestamp > WINDOW_SIZE_IN_SECONDS)); // Check the number of requests within the window if (userRequests.length >= RATE_LIMIT) { return false; // Rate limit exceeded } // Add the current request timestamp await redis.rpush(key, currentTime); await redis.expire(key, WINDOW_SIZE_IN_SECONDS); return true; // Request allowed } ``` This code seems to work in some cases, but I get unexpected behavior, especially around the edge cases when the window resets. I also noticed that on high traffic, requests start getting denied even when they shouldn't. I've considered implementing a more efficient data structure in Redis, like sorted sets, to better manage timestamps, but I'm unsure how to integrate that with the sliding window approach. Any suggestions on optimizing this implementation or addressing the edge cases would be greatly appreciated! The stack includes Javascript and several other technologies. I'm using Javascript latest in this project. I'd love to hear your thoughts on this. I'm working on a REST API that needs to handle this.