Per IP Rate Limiting: Anonymous Traffic Protection
When You Do Not Know Who They Are
Per user limits require authenticated identity. But what about login pages, signup flows, public APIs, or any endpoint that anyone can hit without authenticating? You need a fallback identifier, and the IP address is the primary option for anonymous traffic. Per IP limiting is essential for protecting unauthenticated endpoints from abuse.
How IP Extraction Works
Extract the source IP from the request. For direct connections, use the socket remote address. Behind proxies/load balancers, check X-Forwarded-For header (the leftmost IP is typically the original client) or X-Real-IP if your proxy sets it. Warning: these headers can be spoofed. Only trust them if your infrastructure guarantees they are set correctly by a trusted proxy.
The NAT Problem
Many users share one IP. Corporate offices, universities, coffee shops, mobile carrier NAT can have 10,000+ users behind one IP. If you limit that IP to 100 requests/minute, legitimate users suffer. Set IP limits high enough for shared scenarios (say 1,000/minute) or use IP rate limiting only for abuse scenarios, not primary quota management.
Best Use Cases
Login endpoints: limit to 10/minute per IP to prevent credential stuffing. Signup: limit to 5/hour to prevent account spam. Public search: limit to 100/minute to prevent scraping. Password reset: limit to 3/hour to prevent enumeration. These are protective limits, not usage quotas.
Combining with User Limits
For authenticated endpoints, check both: per user (primary quota) and per IP (abuse protection). A legitimate user hitting per IP limits is unusual and worth investigating. For unauthenticated endpoints, IP is your only option. Implement adaptive limits: start generous, tighten for IPs showing suspicious patterns (failed logins, error rates, unusual request patterns).