Per-tenant API rate limiting without Redis
Use a verified JWT claim to give each SaaS tenant a separate edge rate limit, even when two tenants share the same client IP.
Rate-limit a SaaS API by the tenant in its verified JWT, not by IP. On Gregale, a JWT edge rule verifies the token and a throttle rule puts requests with the same tenant_id in one bucket. Two tenants behind the same NAT get separate limits. Two users from the same tenant share a limit. The app does not need to deploy Redis or run rate-limit middleware for this route.
Choose the identity that should share a limit
| Limit | Use it when |
|---|---|
| Per IP | Requests are anonymous and a rough abuse limit is enough. |
| Per user | Every signed-in user should have a separate bucket. |
| Per tenant | All users and sessions of one workspace should draw from the same quota. |
Use a claim your trusted issuer sets from verified membership. Do not key quotas on a client-supplied tenant header. Keep the application's own permissions checks: a rate limit does not decide which tenant's records a user can read.
Set two rules on the same API path
First, configure a JWT rule for the route. Set the exact issuer, its HTTPS JWKS URL, the expected audience for this API, and the signing algorithm. Match the same host, path and methods in both rules. A missing or invalid token gets 401.
Then add a throttle rule with this action:
{
"key_by": "jwt_claim",
"jwt_claim_name": "tenant_id",
"missing_key_policy": "reject",
"requests_per_second": 0.02,
"burst": 2
}
With the CLI, the two rules look like this. Replace the issuer, JWKS URL, audience, host and path with your own:
gregale edge rule create --app "$APP" --kind jwt --name "Verify API tokens" \
--match-host @app --match-path /api/report --match-method GET --priority 10 \
--jwt-issuer "$ISSUER" --jwt-jwks-url "$ISSUER/.well-known/jwks.json" \
--jwt-algorithm RS256 --jwt-audience "$AUDIENCE"
gregale edge rule create --app "$APP" --kind throttle --name "Per-tenant quota" \
--match-host @app --match-path /api/report --match-method GET --priority 20 \
--throttle-key-by jwt_claim --throttle-jwt-claim tenant_id \
--throttle-missing-key-policy reject \
--throttle-requests-per-second 0.02 --throttle-burst 2
This deliberately tight test policy configures a burst of two and replenishes one token every 50 seconds. Choose values that suit your workload and stay within your plan. missing_key_policy: reject returns 401 if the required identity is unavailable rather than pooling unkeyed callers together. A request whose bucket has no token gets HTTP 429. Inspect the rate-limit response headers to see the policy and refill information.
On an existing app, list its rules first. Gregale evaluates the lowest priority number first, and the first matching rule of each kind applies on that path. If a broad per-IP throttle would also match, give the specific tenant throttle a lower priority number so it takes precedence. Confirm both rules cover the intended host and route. See which rule applies.
Watch bucket cardinality. If a rule reaches its bucket cap, callers beyond the cap share an overflow bucket. The tenant buckets do not reserve independent app capacity or override shared platform limits.
Run it against a disposable Gregale app
Download the stateless Python starter. The JWT rule requires Gregale Hobby or above. You need Bash, curl, jq, OpenSSL, Python 3 and the Gregale CLI, logged in to an isolated test workspace with gregale login. Then run:
bash run-demo.sh
The starter deploys a synthetic report route and a public JWKS endpoint. The report route is public until the JWT rule is installed, so never put real data in this demo. It signs short-lived test tokens locally and uploads only the verifier's public key. All requests come from the same client.
| Request | What the script checks |
|---|---|
| Tenant A, caller 1 | Gets an app 200, then an edge 429 within three requests. |
| Tenant A, fresh caller 2 | Gets an edge 429 on its first request. |
| Tenant B | Still gets an app 200 on its first request. |
The key check is the first request from the second user in tenant A. If each user had an independent bucket, that user would still have quota. The demo doesn't repeat that request until it gets a 429: the first response must show the tenant's already exhausted limit. No Redis, database, or additional runtime dependency is required. Cleanup attempts to delete only the exact app the script created and verifies that it is gone.
We ran the starter against a disposable Gregale app with CLI v0.1.18-rc.390 on December 4, 2026. Tenant A's first caller got two 200s and then an edge 429. A second tenant A caller got 429 on its very first request, and tenant B still got 200. The script then deleted the app and confirmed it was gone.
For an existing API, use your real identity provider rather than the demo signer, match only the intended route, and start with a limit that reflects normal traffic. See edge rules and throttle limits and limiting anonymous traffic per IP.
Questions
- How do I rate-limit each tenant in a SaaS API?
Verify the JWT at the edge and key the throttle bucket by a trusted tenant claim. Users from one tenant share a bucket while other tenants have separate ones.
- Do I need Redis for per-tenant API rate limiting on Gregale?
No. Gregale maintains the throttle buckets at the edge. Your app still needs its own tenant authorization and data access checks.
- What happens if a tenant exceeds its API rate limit?
Gregale returns HTTP 429 for calls whose tenant bucket is empty. Other tenants can keep making requests within their own limits and shared platform ceilings.