A rate-limiting rule is the right tool when you need to cap the rate of requests matching a pattern — login POSTs, API calls, search queries — without blocking the underlying client outright. This article is for operators using Cloudflare’s Advanced Rate Limiting (the current product, which replaced the legacy per-zone counter). We will cover the four knobs that define a rule — characteristic, period, threshold, mitigation timeout — and the layering pattern we use alongside WAF custom rules.
How to verify
Pull the rate-limiting ruleset attached to a zone:
curl -s -X GET \
"https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets" \
-H "Authorization: Bearer $CF_API_TOKEN" \
| jq '.result[] | select(.phase=="http_ratelimit")'
Then inspect the rules:
curl -s -X GET \
"https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RL_RULESET_ID" \
-H "Authorization: Bearer $CF_API_TOKEN" \
| jq '.result.rules[] | {description, expression, ratelimit, action}'
Each rule in the response carries a ratelimit object with characteristics, period, requests_per_period, and mitigation_timeout — these are the four numbers you are tuning. In the dashboard the same view is Security → WAF → Rate limiting rules.
What’s happening
Advanced Rate Limiting groups inbound requests into buckets defined by a characteristic (what to count by), counts requests inside a rolling period, and triggers an action when a bucket exceeds its threshold. The bucket then stays in the mitigation state for the configured mitigation timeout, during which every additional request from that bucket gets the action even if the rate drops back below the threshold.
The four primitives:
- Characteristic — the key used to bucket counts. Options include
ip.src(per source IP),cf.colo.id(per Cloudflare edge — almost never useful), a header value likehttp.request.headers["x-api-key"], a cookie likehttp.request.cookies["session"], or a JA4 fingerprint. You can also combine characteristics for compound keys (per IP + per path). - Period — the rolling window. Values are 10s, 1m, 10m, or 1h. Shorter windows catch bursts; longer windows catch slow-and-low patterns.
- Threshold — requests per period before mitigation kicks in. The number is per-bucket, not per-zone.
- Mitigation timeout — how long the action stays applied after the threshold trip. Range is 10s up to 24h; we typically start at 1m.
The procedure
-
Define the match expression. Open Security → WAF → Rate limiting rules → Create rule. The expression scopes which requests are counted. A login endpoint rule looks like:
(http.request.method eq "POST" and http.request.uri.path eq "/api/login")Be specific — counting every request to a zone makes the rule useless because legitimate traffic dominates the bucket.
-
Choose the characteristic. Click Choose how to count and pick the bucket key. The common pairings:
- Public endpoint, no auth → IP (
ip.src). - Authenticated API → header (
http.request.headers["authorization"]) or cookie (http.request.cookies["session"]). This counts per-user, which is what you actually care about. - Geographic abuse spike → IP + country as a compound characteristic.
- JA4 fingerprint when the abuser rotates IPs but reuses a TLS stack — characteristic
cf.tls_client_auth.cert_revokedis not it; usecf.bot_management.ja4(Enterprise).
- Public endpoint, no auth → IP (
-
Set period and threshold to match the use case. For login: period 1m, threshold 5–10 attempts. For a search endpoint: period 10s, threshold 30. For a write API: per-API-key, period 1m, threshold matched to the contract. Numbers come from looking at your own logs first, not a generic table.
-
Pick the action and the mitigation timeout. Same five actions as the WAF (Block, Managed Challenge, JS Challenge, Log, Skip). For rate limiting we almost always start with
Managed Challengeand a 60-second mitigation timeout — that gives a human a path to retry while shutting down the script. Move toBlockand longer timeouts only after the rule has run in production for a few days. -
Layer it with the WAF. A custom WAF rule (covered in WAF custom rules) handles the deterministic match — “ASN X, never”. A rate-limit rule handles the volumetric case — “this user is making more login attempts than any real human would”. Put the WAF rules first; rate limiting evaluates after the WAF phase, so requests already blocked never reach the counter.
-
Exclude the paths you do not want to count. A rate-limit rule on
*would count/favicon.icorequests. Either narrow the match expression, or use anotclause to exclude/static/*,/healthz, and the asset paths your CDN already absorbs.
Common pitfalls
- A per-IP characteristic gets noisy behind shared NAT (corporate offices, mobile carriers) —
Managed Challengeis safer thanBlockuntil you confirm bucket distribution. - The period and threshold are not independent — 60 requests per minute is not the same as 1 per second; the per-minute window allows bursts up to the limit.
- The mitigation timeout starts fresh on every new request once the bucket is in mitigation; aggressive scripts can hold a bucket in the action state indefinitely, which is usually what you want but surprises operators who expected the timeout to expire.
- The legacy rate-limiting product (single counter per rule, fixed period) still exists in some older zones — migrate to Advanced Rate Limiting; the matching is more expressive and the characteristic options are the whole point.
- Counters are per Cloudflare data center; an attacker spread across many edges may need a slightly lower threshold than the back-of-envelope math suggests.
When we operate Cloudflare for clients, the rate-limit rules sit in source control next to the WAF rules and ship through the same change process — that is the operating model we run under Managed Operations.