Key points
- Rate-limit per customer account or API key, not just per IP address.
- A per-window counter in Redis covers the vast majority of needs.
- Answer with a 429, a
Retry-Afterheader and quota headers so integrations know to back off.
Open an API to your customers and sooner or later someone writes a script that hammers it: a sync running every second, an error handler that retries instantly, an import firing ten thousand calls at once. In a multi-tenant SaaS, that one customer eats resources everyone else pays for. We add rate limiting from the first version of an API, and here’s the simplest way to get it right.
What to count
First decide what the limit is keyed on. For an authenticated API, count per customer account, or per API key if one account can hold several. That’s the entity with a contract, a plan and a quota. IP addresses are a poor fit here: several customers may share one integration platform, and one customer may spread calls across many servers.
IP-based limits still make sense on unauthenticated routes, login and password reset above all. Be careful there: behind a proxy or CDN, REMOTE_ADDR is the proxy. Only trust X-Forwarded-For when the request actually came through a proxy you control, otherwise any caller can pick the IP you’ll blame.
A per-window counter in Redis
The simplest approach is a fixed window: one counter per customer per minute that expires on its own. With the phpredis extension:
function rate_limit(Redis $redis, string $key, int $limit, int $window): array
{
$bucket = intdiv(time(), $window);
$redisKey = "rl:{$key}:{$bucket}";
// INCR and EXPIRE in one transaction, so the key can never outlive its window
[$count] = $redis->multi()
->incr($redisKey)
->expire($redisKey, $window)
->exec();
return [
'allowed' => $count <= $limit,
'remaining' => max(0, $limit - $count),
'reset' => ($bucket + 1) * $window,
];
}
Every app instance talks to the same Redis, so the limit holds across multiple servers, which an in-memory or session counter never would.
The known weakness: a customer can burn their quota at the end of one minute and again at the start of the next, briefly doubling the rate. For protecting infrastructure that rarely matters. If you need a genuinely smooth rate, move to a sliding window or token bucket, but check that you actually need it first.
Tell the client how to slow down
A limit only helps if the developer on the other side can see what’s going on. We always send quota headers, plus a 429 with Retry-After once the limit is hit:
$limit = $tenant->plan()->requestsPerMinute();
$r = rate_limit($redis, 'tenant:' . $tenant->id(), $limit, 60);
header('X-RateLimit-Limit: ' . $limit);
header('X-RateLimit-Remaining: ' . $r['remaining']);
header('X-RateLimit-Reset: ' . $r['reset']);
if (!$r['allowed']) {
$wait = max(1, $r['reset'] - time());
http_response_code(429);
header('Retry-After: ' . $wait);
header('Content-Type: application/json');
echo json_encode(['error' => 'rate_limited', 'retry_after' => $wait]);
exit;
}
Document these headers in your API reference. Any decent HTTP client can honour Retry-After before retrying, which ends most runaway loops without a single support ticket.
Tie limits to plans and routes
The per-minute limit is a natural place to differentiate pricing tiers: read it from the customer’s plan, alongside their other entitlements. Add separate limits for expensive routes too, such as exports or PDF generation, with their own key (tenant:42:export) and a much lower ceiling.
Finally, log every time a limit is hit. A customer who hits it daily either has an integration to fix or needs a bigger plan, and both are conversations worth having.
FAQ
What if Redis goes down?
Decide ahead of time. We usually fail open, letting requests through and logging the incident, rather than taking the whole API down because of the limiter. For the login route, failing closed can be the better call.
Shouldn’t this live in the web server or CDN instead?
Use both. A CDN or Nginx stops crude per-IP abuse before it reaches your app. Per-customer, plan-aware limits can only live in the application, because only the app knows who’s calling.
Opening an API on your SaaS and want to protect it without frustrating customers? We design APIs that are robust, well documented and ready to scale.



