Why Rate Limiting Matters
Without rate limiting, a single misbehaving client can exhaust your API capacity. Rate limiting is your first line of defense against abuse, runaway automation, and accidental DDoS.
Token Bucket
jhgfvbjhgfhjgf
hgyfd
jhgfA bucket holds N tokens. Each request consumes one token. Tokens refill at a fixed rate. Bursts are allowed up to bucket capacity. Simple, efficient, widely used.
Sliding Window Log
For each user, store a log of request timestamps. On each request, remove timestamps older than the window, count remaining, compare to limit. Precise but memory-intensive.
Redis + Lua
Distributed rate limiting requires atomicity. A Lua script in Redis combines the check-and-decrement into a single atomic operation — no race conditions, no locks.