Redis · Chapter 11 of 26
Rate limits
By the end of this chapter you can limit how often a fan does something with Redis: a fixed window with INCR and EXPIRE sent together in one MULTI, a retry-after taken from the key's TTL, and a sliding window kept in a sorted set, with time passed in so you can test it without waiting. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Redis from Node.js
- 3 tasks
- Redis
- In the all labs plan
The first 2 chapters of Redis are free. This one comes with the all labs plan.
Why you need this
Ten minutes before the toss, Gurpreet sees thousands of login tries a minute against a handful of fans: a bot guessing passwords. On the same night, someone taps "Resend OTP" again and again, and every OTP is an SMS that Venkat pays for. GullyXI needs a rule for each: at most 5 login tries a minute for a fan, at most 3 OTPs in 10 minutes.
The rule has to hold across every app server. A counter in a Node.js variable counts only the tries that reached that one server, and it resets when the server restarts. You already know the right tool: INCR (chapter 1) counts inside Redis, where every server sees the same number, and a TTL (chapter 3) makes the counter go away on its own. This chapter puts them together, closes the gap between them, and fixes the one trick a fixed window cannot stop.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
login-limit.js: five logins a minute
In login-limit.js, write allowLogin(redis, fanId, now) as a fixed window: at most 5 login tries a minute for each fan. The minute is Math.floor(now / 60000), with now in milliseconds, and the counter's key is limit:login:<fanId>:<minute> (limit:login:u1093:29588520 at the toss).
Each try adds 1 to the counter and gives the key a TTL of 60 seconds (the check accepts 55 to 60), sent together in one MULTI (the check counts the MULTIs Redis ran). Return true for tries 1 to 5 in a minute and false after: seven tries in one minute return true five times, then false twice, and leave the counter at 7. Another fan has a counter of their own, and the next minute starts again from 1.
- Task 2
otp-resend.js: OTP resends, and when to try again
In otp-resend.js, write requestOtp(redis, fanId): at most 3 OTPs in 10 minutes for each fan, counted from the fan's first request. The counter's key is limit:otp:<fanId>. Each request adds 1; the first request gives the key a TTL of 600 seconds (the check accepts 595 to 600), and later requests never start the TTL again.
Return { allowed: true, retryAfter: 0 } for requests 1 to 3 and { allowed: false, retryAfter: <seconds> } after, where the seconds are the key's TTL at that moment.
A server that crashed left limit:otp:u1250 at 3 with no TTL. The next request for u1250 must give the key its 600 seconds (and is refused, with a retryAfter of about 600). Send everything in one MULTI.
- Task 3 · interview question
Interview: login-edge.js, the burst at the minute's edge
A bot got 10 login tries for u1093 in one second, through the limit of 5 a minute, by sending 5 at 7:29:59.5 pm and 5 at 7:30:00.5 pm. In login-edge.js, write allowLogin(redis, fanId, now) as a sliding window: at most 5 tries in any 60 seconds.
Keep the tries in a sorted set under limit:login:<fanId> (no minute in the key): one member for each try, its score the try's time now in milliseconds. Each try forgets the tries older than 60000 ms, counts the rest, and is let through (and kept) only if fewer than 5 remain; a refused try is not kept. Give the key a TTL of 60 seconds (the check accepts 55 to 60). Return true or false.
Tries can come at the same millisecond: five tries at the same now are five members. Five tries at 7:29:59.5 pm pass, five at 7:30:00.5 pm are refused, and five tries just over 60 seconds after the first five pass again.
Farhan will ask: fixed or sliding window, which do you pick for logins, and what does each cost Redis?
What you will learn
- A fixed window: one counter a fan a minute, made with INCR and EXPIRE.
- MULTI from ioredis: sending the two together, so no crash can split them.
- Retry-after: the TTL of the counter is how long to wait.
- EXPIRE NX: give a key a TTL only if it has none.
- A sliding window: the last 60 seconds of tries in a sorted set.
- Time as an argument: now passed in, so a check never waits a minute.
More in Redis from Node.js
Build "Rate limits" yourself
GullyXI, a Pune fantasy cricket app, falls over on match night. You make it fast with Redis. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Redis are free. This one comes with the all labs plan.