Skip to content

Lesson 6 of 9 · 23m

Rate Limiting and Abuse Controls

Use rate limits as traffic shaping and abuse containment, with identity-aware buckets, useful response headers, and fail-open/fail-closed decisions made ahead of time.

Rate limiting is not there to punish legitimate users. It is there to keep one credential, one botnet, or one broken retry loop from consuming the capacity everyone else needs. If you design it after the incident, you will discover that your most expensive endpoint is also the easiest to hammer.

Choose the bucket key deliberately

Different endpoints need different identities:

login attempts        IP + account identifier; public reads: IP or token
authenticated calls   user or API key; tenant-scoped APIs: tenant + user

IP-only limiting breaks behind NAT, offices, mobile networks, and shared proxies. User-only limiting fails before authentication and is useless against credential stuffing. Real systems often use layered buckets.

Token bucket is the usual default

A token bucket allows short bursts while enforcing a sustainable average. Symfony RateLimiter can express this directly:

$limiter = $factory->create('api_user_' . $userId);
$limit = $limiter->reserve(1);
if (!$limit->isAccepted()) {
    return $this->problemResponse(429, 'Rate limit exceeded', [
        'Retry-After' => $limit->getRetryAfter()->getTimestamp() - time(),
    ]);
}

A fixed-window counter is simpler but creates thundering herds at window boundaries. Sliding windows and token buckets are kinder to normal traffic.

Return actionable 429s

A rate-limit response should tell the client how to behave:

HTTP/1.1 429 Too Many Requests
Retry-After: 17
RateLimit-Limit: 600
RateLimit-Remaining: 0
RateLimit-Reset: 19

Then clients can back off instead of retrying every second and making the problem worse. Make the same contract apply at the reverse proxy if possible; an application-level 429 after the work is done does not save much capacity.

Limit cost, not just count

Some requests cost 100 times more than others. Password verification is deliberately expensive. Search endpoints scan data. GraphQL selections vary wildly. Use weighted costs or endpoint-specific limits:

  • authentication: strict per-account and per-IP limits; expensive search/reports: low counts and concurrency caps
  • ordinary reads: broader bursts; mutations: tighter limits plus idempotency keys

Concurrency limits matter as much as rate. Ten parallel expensive exports can hurt more than one hundred sequential cheap reads.

Decide what happens when Redis is down

If the limiter backend fails, is the API open or closed? For abuse-sensitive endpoints, fail closed or degrade to stricter local limits. For low-risk reads, fail open with alerting. Write that decision down before the outage; otherwise the incident chooses for you.

Field notes

  • Retry clients should honor Retry-After and jitter exponential backoff; synchronized retries create traffic spikes.
  • Do not rate-limit health checks like user traffic, but protect expensive dependencies. Log bucket metadata and outcome, never raw bearer tokens. Rate limiting is not authorization.

Was this lesson helpful?

Sign in to rate