f you're running an API or backend service, one of the most important things you need is rate limiting. It protects your server from spam, DDoS-like bursts, and accidental abuse. Without it, even a simple endpoint can get overloaded and slow down your entire system.
In this guide, we’ll walk through:
- What rate limiting actually is
- Why Redis is perfect for it
- Simple real-world code examples using Node.js
- Best practices for production
Let’s make it super easy to understand.
What is rate limiting? (In simple words)
Rate limiting means:
👉 “You can’t send unlimited requests. You’re allowed only X requests in a given time frame.”
Example:
- 100 requests per minute
- 5 login attempts every 10 minutes
- 20 API calls per second
This keeps your system safe from:
- Bots
- Brute-force attacks
- Accidentally buggy code
- Sudden traffic spikes
Why use Redis for rate limiting?
Redis is:
- Super fast (in-memory)
- Can handle millions of operations per second
- Supports atomic operations (no race conditions)
- Works across multiple Node.js servers
This makes it perfect for production-level rate limiting.
Rate Limiting Methods (Explained Like You're 18)
1. Fixed Window
You count how many requests happen in a fixed block of time.
Example: 100 requests in 1 minute.
Pros: Easy and fast
Cons: Users can break limits at the edges of windows
2. Sliding Window
You look at the last exact X seconds instead of a fixed block.
Pros: More accurate, smoother
Cons: Slightly heavier in Redis
3. Token Bucket
Imagine a bucket that slowly fills with tokens.
Each request takes 1 token. If bucket empty → request blocked.
Pros: Best for production, allows small bursts
Example 1 — Easy Rate Limiting (Fixed Window)
Perfect for simple APIs, IP-based limits, or small projects.
Usage:
Example 2 — Sliding Window (More Accurate)
This stores timestamps of each request in a Redis sorted set.
Example 3 — Token Bucket (Best for Production)
This works smoothly even when users send burst traffic.
We use a Redis Lua script so everything is executed safely and atomically.
👉 But don’t worry, you can just copy-paste this code and it works.
Where Should You Use Rate Limiting?
✔ Login routes
✔ OTP routes
✔ Public APIs
✔ Expensive database queries
✔ Upload endpoints
✔ Comment endpoints
✔ Chat message endpoints
Basically anywhere users can spam.
Best Practices for Production
✅ Use userId instead of IP when possible
Because one office can share IPs.
✅ Always set TTL so old keys auto-clean
Redis memory stays low.
✅ Fail safely
If Redis goes down:
- You can block all requests (fail closed)
- Or allow all requests (fail open) → recommended
✅ Use multiple layers
- Cloudflare / AWS ALB rate limiting
- Redis rate limiting
- App-level validation
✅ Return helpful headers
✅ Monitor
- How many 429 errors users get
- Redis memory
- Unusual spikes
✅ Use different limits per route
Example:
- Login route: 5 attempts/min
- Public API: 100 req/min
- Chat: 20 msgs/min
Rate limiting is not optional anymore — it’s a required part of any real production backend.
Redis makes it:
- Fast
- Accurate
- Safe
- Scalable
Start with the simple fixed-window method.
If your app grows, upgrade to sliding window or token bucket.

