Back to blog

Published Updated

API Rate Limiting: Design and Implementation Guide

By Tushar ChoudharyAPI Security • Rate Limiting • Abuse Protection • Backend • Web Apps • 2026

Design API rate limits using identities, scopes, algorithms, quotas, retries, distributed counters, monitoring, exceptions, and abuse-resistant testing.

API Rate Limiting: Design and Implementation Guide

API rate limiting controls how much work a client, user, tenant, key, device, or network can request during a defined period. It protects service capacity, limits accidental loops, reduces abuse, and creates fair usage boundaries. It is not a replacement for authentication, authorization, input validation, fraud controls, or infrastructure scaling.

A useful limit reflects the cost and risk of the operation. Login attempts, search queries, PDF exports, file uploads, bulk imports, payment actions, and webhook retries should not all share one arbitrary number.

OWASP includes unrestricted resource consumption in its API Security Top 10, which is a useful reminder that limits should cover compute, memory, storage, network, third-party spend, and operational impact.

What a rate-limit rule contains

A complete rule needs five parts:

  1. Identity: who or what consumes the allowance.
  2. Scope: which endpoint, operation, tenant, or resource it covers.
  3. Budget: requests, units, bytes, rows, or cost permitted.
  4. Window: the time or refill model.
  5. Response: what happens when the budget is exhausted.

“100 requests per minute” is incomplete until the system states whether that is per IP, account, API key, tenant, endpoint, or global service.

Choose the right identity

Possible identifiers include:

  • source IP before authentication;
  • verified user account;
  • API key or OAuth client;
  • company or tenant;
  • device or installation;
  • destination resource;
  • session;
  • a combination of several identifiers.

IP-only limiting is weak for authenticated business APIs. Many legitimate users can share one corporate or mobile network, while an attacker may rotate addresses. After authentication, use account, key, or tenant identity where possible while retaining network controls for abuse at the edge.

Use multiple limit layers

A production system often needs several simultaneous controls:

LayerExample purpose
GlobalProtect total service capacity during an incident
TenantPrevent one company from consuming shared capacity
User/keyEnforce fair use and contain compromised credentials
EndpointProtect expensive routes such as exports or search
ResourcePrevent repeated actions against one invoice, OTP, or account
ConcurrencyLimit simultaneous expensive jobs
VolumeLimit upload bytes, rows, recipients, or generated documents

The effective allowance can be the strictest applicable rule. Document precedence so support teams can explain blocked requests.

Fixed window

A fixed-window counter tracks requests within a clock period such as one minute. It is simple and inexpensive, but traffic can burst across a boundary: a client may use the full allowance just before and just after the reset.

Use it when:

  • exact smoothness is not important;
  • the operation is inexpensive;
  • implementation simplicity is valuable;
  • boundary bursts remain within service capacity.

Do not rely on it alone for sensitive authentication or high-cost operations.

Sliding window

A sliding-window model considers recent activity relative to the current time. It produces a fairer limit across boundaries but needs more storage or approximation.

Variants include:

  • timestamp log of individual requests;
  • weighted current and previous window counters;
  • rolling buckets.

Choose based on required accuracy, throughput, and storage cost. Exact per-request logs may be excessive for high-volume traffic.

Token bucket

A token bucket refills at a defined rate up to a maximum capacity. Each request consumes one or more tokens. It allows controlled bursts while enforcing a sustainable average.

Example:

  • bucket capacity: 20 tokens;
  • refill: 2 tokens per second;
  • standard read: 1 token;
  • complex export request: 10 tokens.

Weighted tokens are useful when operations have very different cost, but weights must be measured and reviewed.

Leaky bucket and queue control

A leaky-bucket style design processes work at a controlled output rate. It can smooth bursts into a queue but introduces waiting and requires queue-capacity rules.

For background exports or imports, a concurrency limit and queue may be better than rejecting every burst. The system still needs maximum queue size, job expiry, cancellation, status visibility, and tenant fairness.

Endpoint-specific examples

Login

Combine controls by account, IP/network, device signal, and global anomaly. Avoid creating an account-enumeration response. Add progressive delay, risk review, MFA, and alerts where appropriate.

OTP request and verification

Limit sends by destination, account, device, and network. Separately limit verification attempts. Record expiry, resend state, provider response, and lockout recovery. Do not expose whether an unregistered phone or email exists.

Search

Apply user or tenant limits, query complexity controls, maximum page size, and database safeguards. Cache safe repeated results where useful.

File upload

Count request frequency, file size, total storage, processing concurrency, and content-validation cost. Rate limiting does not replace malware scanning, type checks, and authorization.

Reports and PDF generation

Use per-user and per-tenant concurrency, duplicate-job detection, date-range limits, and queue status. Reuse an existing completed artifact when policy allows.

Payment or order action

Protect the endpoint but prioritise idempotency. A client retry should not create a second charge or order. Rate limits are not transaction deduplication.

Return a useful response

When a request exceeds a policy, the API commonly returns HTTP 429 Too Many Requests. The response should be consistent and safe.

Include where appropriate:

  • stable machine-readable error code;
  • human-readable summary without sensitive internals;
  • request or trace identifier;
  • retry guidance;
  • documented limit headers;
  • support path for approved high-volume use.

Clients should use exponential backoff with jitter and respect server guidance. Immediate tight retry loops increase the problem.

Idempotency and retries

Rate limits affect retry behavior. For state-changing requests:

  • accept an idempotency key where appropriate;
  • bind it to user/tenant and request intent;
  • store the result for a documented period;
  • reject reuse with conflicting payload;
  • return the original outcome for safe duplicate requests;
  • ensure background jobs also deduplicate.

The webhook integration guide explains similar duplicate and retry controls for event-driven systems.

Distributed rate limiting

One in-memory counter works only for one process. Multi-instance deployments need shared or coordinated state.

Design questions include:

  • central store or edge-provider control;
  • atomic counter operation;
  • key expiry;
  • clock behavior;
  • regional consistency;
  • store outage policy;
  • network latency;
  • hot-key handling;
  • privacy of stored identifiers.

Redis is common, but the technology does not choose the policy. Use atomic operations or tested scripts and consider fail-open versus fail-closed per endpoint risk.

Fail-open versus fail-closed

If the counter store is unavailable:

  • fail-open preserves availability but temporarily weakens protection;
  • fail-closed protects the resource but blocks legitimate traffic;
  • local fallback provides a smaller temporary allowance per instance;
  • degraded mode disables expensive operations while preserving essential reads.

Authentication, payments, public reads, and internal reports may need different decisions. Document and test the behavior.

Tenant plans and quotas

SaaS products may offer different usage plans. Keep commercial quotas separate from emergency abuse protection.

For example:

  • subscription quota: 10,000 exports per month;
  • short-term rate: 5 export requests per minute;
  • concurrency: one running export per account;
  • global safety limit: platform-wide threshold.

Customers need usage visibility before a commercial quota is exhausted. Billing enforcement should not depend on an undocumented security limiter.

Database and downstream protection

An API that allows 100 requests per second can still overload a database if each request performs an unbounded query. Add:

  • pagination and maximum page size;
  • query complexity restrictions;
  • indexed filters;
  • maximum date range;
  • timeouts;
  • bulk-operation limits;
  • cache where correctness permits;
  • connection-pool controls;
  • circuit breakers for failing dependencies;
  • asynchronous processing for expensive work.

Use the database indexing guide when slow data access is the underlying bottleneck.

Monitoring and alerts

Track:

  • allowed and blocked requests by rule;
  • top consuming tenants, keys, endpoints, and networks;
  • 429 ratio;
  • request latency and error rate;
  • queue depth and processing age;
  • third-party cost or quota consumption;
  • counter-store latency and errors;
  • support overrides;
  • sudden behavior changes.

Do not log raw secrets, tokens, OTPs, or unnecessary personal data. Use hashed or internal identifiers where operationally suitable.

Safe overrides

Some integrations need higher limits. An override process should include:

  • verified customer and use case;
  • exact scope and duration;
  • new allowance;
  • approver;
  • monitoring threshold;
  • expiry date;
  • rollback condition;
  • audit record.

Permanent unlimited bypasses create hidden risk. For large transfers, offer an asynchronous bulk endpoint instead of raising interactive request limits indefinitely.

Testing strategy

Test more than the first blocked request:

  1. request just below the limit;
  2. exact boundary request;
  3. request above the limit;
  4. refill/reset timing;
  5. burst around a window boundary;
  6. multiple users in one tenant;
  7. one user across multiple instances;
  8. counter-store outage;
  9. duplicate state-changing requests;
  10. clock and expiry behavior;
  11. IPv4/IPv6 and proxy address handling;
  12. override activation and expiry.

Load tests should use a safe non-production environment and agreed capacity. Do not test third-party providers beyond authorised limits.

Our implementation approach

In our implementation work, VASUYASHII defines limits from resource cost and business risk. We map authenticated identity, tenant boundary, endpoint weight, concurrency, retry behavior, and support overrides before choosing a counter algorithm.

Our API integration service also checks provider limits, webhook retries, idempotency, and reconciliation. For a custom web application, rate-limit telemetry is connected to operational monitoring so legitimate customers are not silently blocked.

Common mistakes

  • One global limit for every endpoint.
  • IP-only limits after authentication.
  • Returning 429 without retry guidance.
  • Retrying immediately without jitter.
  • Using rate limits instead of authorization.
  • Ignoring concurrency and response size.
  • Keeping counters only in one app process.
  • Failing open on high-risk actions without review.
  • Permanent unlimited customer overrides.
  • Logging full credentials in limiter keys or errors.

Implementation checklist

  • [ ] Identities and tenant scope are defined.
  • [ ] Endpoint cost and abuse risks are classified.
  • [ ] Short-term rate, burst, concurrency, and quota are separate.
  • [ ] Algorithm behavior is documented.
  • [ ] Distributed counters use atomic operations.
  • [ ] 429 response and client retry contract are consistent.
  • [ ] State-changing calls use idempotency where appropriate.
  • [ ] Counter-store failure behavior is tested.
  • [ ] Overrides expire and are audited.
  • [ ] Dashboards and alerts show blocked and near-limit usage.

FAQs

What is the best API rate limit?

There is no universal number. Measure endpoint cost, normal client behavior, peak volume, tenant fairness, third-party limits, and abuse risk, then load-test a conservative policy.

Should rate limits be per IP or per user?

Use layered identities. IP controls help before authentication, while user, API-key, tenant, and resource limits are usually more accurate after authentication.

Is HTTP 429 required?

It is the standard response for too many requests and makes client behavior clearer. The complete contract should also define error codes, headers, and retry handling.

Can a CDN or API gateway handle all rate limiting?

It can handle valuable edge and global controls, but application-level limits may still need authenticated user, tenant, resource, plan, and business-operation context.

Does rate limiting stop DDoS attacks?

Application rate limiting is one control, not complete DDoS protection. Use provider edge protection, network controls, capacity planning, and incident response as appropriate.

How do mobile apps handle rate limits?

The app should queue or back off safe retries, show a useful state, avoid duplicate actions, and respect server guidance. Do not embed secret bypass keys in the app.

Next step

Inventory the ten most expensive or sensitive endpoints and define identity, cost unit, burst, sustained rate, concurrency, and failure behavior for each. Contact VASUYASHII for an API and integration review tied to real traffic and business risk.