BE6: Protecting the API

Lesson 3 of 5 · reading 15–25 minutes; practice and project time is on the module page

Rate limits

🎯You'll be able to

Choose and implement a rate limit that protects the API, return 429 with useful headers, and share counts across instances.

🏠The everyday picture

A ration shop gives each card holder their monthly quota, however many times they come back. The shopkeeper's register, not the customer's word, keeps the count.

📘What it really is

📝A worked example

Input: test customer 3 makes 41 requests to GET /v1/me/orders within one minute; the limit is 40 (set by RATE_LIMIT=40 in the tests).

Steps in the finished api/rate-limit.js: key rl:u:tiffin-web:cust-3:<minute number>; MULTI INCR key; EXPIRE key 65; EXEC; if the count is over 40, reply 429.

Expected output: requests 1 to 40 get 200 with RateLimit-Remaining counting down; request 41 gets 429 with Retry-After: <seconds to the next minute>; customer 2's request at the same moment gets 200. This is Project 1 test 12.

🌍Where it's used

📝Case scenario

An app sends a one-time password by SMS on every "Send OTP" request, with no limit. A bot calls it 200,000 times overnight for random numbers, and the SMS bill arrives in lakhs of rupees. A limit per phone number and per IP, plus a daily cap, would have stopped it after a handful of messages.

🖱️Try it

Against the starter kit, call GET /v1/me/orders?limit=1 50 times in a loop with one token (for i in $(seq 50); do curl ...; done). Count the status codes. Then write which key, window and limit you'd use for this route, the menu route, and a future "Send OTP" route. Success: the starter allows all 50, and your plan gives a different limit for each route with a reason.

⚠️Common mistakes

🛠️For your project

Project 1 test 12: request 41 within a minute gets 429 with Retry-After, and another customer isn't affected.

✅Quick check

Why count requests in Redis rather than in each server's memory?

Show the answer

So all servers share one count; otherwise each server gives the full limit separately.

Should rate limiting fail open or closed when Redis is down?

Show the answer

It depends: usually open for ordinary routes, closed for sensitive ones such as login attempts.

← PreviousAccept only what you expect
ProLearners Assist