Prompt
I'm building an API gateway and need abuse throttling
Latest observation
Abuse throttling for an API gateway usually means you want to limit bad or excessive traffic without breaking legitimate clients. The best approach is typically a mix of rate limiting, quotas, concurrency limits, and adaptive abuse detection.
Core controls to implement
1) Rate limiting
Limit requests per time window.
Common variants:
- Fixed window: simple, but can allow bursts at window edges
- Sliding window: smoother, more accurate
- Token bucket: best general-purpose choice for gateways
- Leaky bucket: good for smoothing traffic
Typical dimensions:
- per API key / client ID
- per IP
- per user account
- per route or method
- per tenant
- per region / ASN if relevant
Example policy:
100 req/min per API key1000 req/min per tenant10 req/sec per IP for login endpoints
2) Burst control
Allow short bursts while enforcing sustained limits.
Example:
- steady rate:
50 rps - burst capacity:
200 requests
Token bucket naturally supports this.
3) Concurrency limits
Protect downstream services from too many simultaneous in-flight requests.
Example:
max 20 concurrent requests per tenantmax 5 concurrent requests per login route per IP
4) Quotas
Use longer-term usage caps:
- per day
- per hour
- per month
Useful for API plans and tenant governance.
5) Authentication-aware throttling
Different limits for:
- anonymous traffic
- authenticated users
- trusted service-to-service clients
- premium plans
Anonymous requests should usually have much stricter limits.
6) Endpoint-specific policies
Sensitive or expensive endpoints need tighter controls:
- login
- password reset
- OTP verification
- search
- export/download
- file upload
- expensive aggregation endpoints
7) Adaptive abuse detection
Raise strictness when behavior looks suspicious:
- many 4xx responses
- repeated auth failures
- many distinct usernames from one IP
- high request entropy / rotating user agents
- spikes in traffic
- distributed low-and-slow patterns
Actions can escalate:
- soft limit / slow down
- temporary block
- CAPTCHA / challenge
- require re-authentication
- step-up verification
- denylist IP or token
Recommended architecture
Layer 1: Edge enforcement
At the gateway:
- identify client
- compute rate-limit key
- check counters/limits
- reject early with
429 Too Many Requests - include
Retry-After
Layer 2: Distributed state
For multi-node gateways, use a shared store:
- Redis is common
- use atomic operations or Lua scripts
- avoid race conditions
Layer 3: Observability
Track:
- allowed vs blocked requests
- top offenders
- per-route rates
- latency impact
- false positive rates
Layer 4: Policy config
Keep rules externalized:
- per customer/tenant
- per route
- per environment
- dynamic updates without redeploy
Practical algorithm choice
If you want one default approach:
- use token bucket for rate limiting
- add concurrency caps
- add behavior-based escalation
- store counters in Redis
Token bucket is usually the most practical for API gateways because it:
- supports bursts
- is easy to explain
- works well for both humans and machines
Example policy set
Anonymous:
- 20 req/min per IP
- 5 concurrent requests per IP
- 3 login attempts/min per IP
Authenticated basic:
- 100 req/min per API key
- 20 concurrent requests per user
Premium:
- 1000 req/min per API key
- 100 concurrent requests per tenant
Sensitive endpoints:
- 5 req/min per IP
- stricter thresholds on failures
Response behavior
When throttling:
- return
429 Too Many Requests - include
Retry-After - optionally include a machine-readable error body
Example:
HTTP/1.1 429 Too Many Requests
Retry-After: 30
Content-Type: application/json
{
"error": "rate_limited",
"message": "Too many requests. Please retry later."
}
Important design tips
- Don’t rely on IP alone; NAT/shared networks can cause false positives
- Don’t trust headers like
X-Forwarded-Forunless set by trusted proxies - Separate limits for read vs write operations
- Make limits configurable per route/customer
- Use fail-open vs fail-closed intentionally if the limiter backend is down
- Test against legitimate bursts, mobile networks, retries, and client-side fanout
If you want a simple starting policy
Start with:
- Token bucket per API key
- Fallback per IP for anonymous traffic
- Separate stricter rules for auth and password endpoints
- Redis-backed shared counters
- 429 + Retry-After
- Metrics + alerting on top offenders
If you want, I can also give you:
- a reference design for a production gateway limiter
- Redis Lua script examples
- Nginx/Envoy/Kong/Traefik configuration
- or a sample policy schema for your API gateway.