CDN Rate Limiting: Protecting APIs at the Edge

· Priya Sharma · 4 min read

Rate Limiting at the Edge

Rate limiting restricts how many requests a client can make within a time window. Implementing it at the CDN edge stops abusive traffic before it reaches your origin, protecting both your servers and other users from resource exhaustion. For foundational context, read CDN DDoS Protection: How CDNs Absorb Volumetric Attacks — FlyFish NWA.

Rate Limiting Algorithms

Fixed Window

Count requests in fixed time windows (e.g., per minute). Simple but allows bursts at window boundaries.

Performance Comparison: Edge vs Origin Processing
Metric Edge Processing Origin Processing Improvement
Latency (p50)8–15 ms120–300 ms15–20x faster
Cache Hit Ratio85–95%N/AReduces origin load
TLS Handshake1–5 ms (nearby PoP)50–150 ms10–30x faster
Bandwidth Cost$0.01–0.04/GB$0.05–0.12/GB3–5x cheaper
Availability99.99% (multi-PoP)99.9% (single DC)10x fewer outages
CDN Security Architecture — Defense-in-depth protection layersCDN Security Architecture — Defense-in-depth protection layers CDN Security Layers — Defense in Depth DDoS Mitigation Web Application Firewall Bot Detection & Rate Limiting SSL/TLS Encryption Origin Server DDoS ✕ Attack ✕ SQLi ✕ PROTECTED
Figure 1: CDN Security Architecture — Defense-in-depth protection layers
Window: 1 minute, Limit: 100
T=0:58 → 90 requests (allowed)
T=1:02 → 90 requests (allowed — new window)
# 180 requests in 4 seconds across window boundary

Sliding Window

Weighted average of current and previous window counts. Smooths out boundary bursts.

Previous window: 80 requests, Current window: 50 requests
Time into current window: 40%
Weighted count: 80 × 0.6 + 50 = 98
Limit: 100 → Request allowed (98 < 100)

Token Bucket

A bucket holds tokens; each request consumes a token; tokens refill at a constant rate. Allows controlled bursts while maintaining a long-term average rate.

Bucket capacity: 100 tokens
Refill rate: 10 tokens/second
Burst: up to 100 requests instantly
Sustained: 10 requests/second

Rate Limit Dimensions

What identifies a "client" for rate limiting purposes:

DimensionUse CaseConsiderations
Source IPGeneral protectionCGNAT: thousands of users share one IP
API keyAPI access controlRequires API key in request
User sessionAuthenticated usersRequires session cookie parsing
URL pathEndpoint-specific limitsProtects specific resources
CombinationPrecise targetingIP + path: 10 login attempts per IP per minute

Configuration Examples

# API endpoint: 100 requests per minute per API key
Path: /api/*
Key: Header(X-API-Key)
Limit: 100 requests / 60 seconds
Action: Return 429 Too Many Requests

# Login protection: 5 attempts per minute per IP
Path: /auth/login
Key: Source IP
Limit: 5 requests / 60 seconds
Action: Return 429 with Retry-After header

# Global page limit: 60 pages per minute per IP
Path: /*
Key: Source IP
Limit: 60 requests / 60 seconds
Action: Challenge (CAPTCHA)

Response Headers

Communicate rate limits to API consumers via standard headers:

Secure Request Flow — How requests pass through security layersSecure Request Flow — How requests pass through security layers CDN Request Flow — From Browser to Edge CDN Request Lifecycle Step 1 DNS Lookup Step 2 TCP Connect Step 3 TLS Handshake Step 4 Edge Server Step 5 Cache Check Step 6 Response Cache HIT → 5ms response Cache MISS → Fetch origin 0ms ~50ms (cache hit) ~200ms (cache miss)
Figure 2: Secure Request Flow — How requests pass through security layers
HTTP/1.1 200 OK
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 42
X-RateLimit-Reset: 1719100800
Retry-After: 30 (only on 429 responses)

CDN Rate Limiting Features

Edge vs Origin Rate Limiting

Edge rate limiting (CDN) protects your origin from ever receiving excessive traffic. Origin rate limiting (application) handles business logic limits (API quota per customer). Both should exist: edge for infrastructure protection, origin for business rules. The CDN handles the heavy lifting; origin handles the nuance.

Common Mistakes

External References

References & Standards: OWASP CDN Security · Cloudflare documentation