October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Rate Limit an API: Algorithms, 429 Responses, and Abuse Protection

A practical guide to API rate limits: how to choose identities and algorithms, apply stricter controls to costly routes, handle HTTP 429 retries, and understand what a limiter cannot prevent.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Rate limiting controls how often a defined client or traffic key can call an API within a period. It helps contain excessive use and protect backend capacity, but it is only one layer of protection: it does not cap the cost of each individual request or replace authentication and broader availability defenses.

How do I rate limit an API?

Start by deciding what you need to protect, who should share an allowance, and what clients should experience when it is exhausted. Then choose an algorithm, apply stricter policies to riskier routes, and define how clients recover from a rejection.

As an Amazon Associate I earn from qualifying purchases.

  1. Identify the risk. Distinguish ordinary high-volume use from credential abuse, expensive operations, or attempts to overload the service.
  2. Choose a key and scope. Decide whether requests share a limit by IP address, authenticated user, account, credential, or another traffic identity. A policy can also be scoped to a route or method.
  3. Set the allowance and burst behavior. Define the sustained request rate and, where the algorithm supports it, how much short-term traffic can pass at once.
  4. Select an algorithm. Compare how it handles bursts, recovery, interval boundaries, distributed state, and operational complexity.
  5. Apply policies by route risk. Authentication, account recovery, costly search, exports, and bulk operations may need distinct limits or additional workload controls.
  6. Reject excess requests consistently. Return HTTP 429 Too Many Requests and provide useful retry guidance where the service can determine it.
  7. Observe and adjust. Check how the policy behaves under legitimate bursts and abuse patterns; gateway settings may be targets rather than exact ceilings.

Which rate-limiting algorithm should you use?

Algorithms differ in the shape of traffic they allow. The right choice depends on whether short bursts are acceptable, whether the policy must follow a rolling interval, and whether excess work should be rejected or queued and smoothed.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Approach Burst and recovery behavior Boundary behavior Practical trade-off
Token bucket Tokens refill at a configured rate; accumulated tokens allow a bounded burst. Not based on a single fixed interval boundary. Useful when steady throughput with controlled bursts is desired. Actual gateway throttling may not form a perfect hard wall.
Sliding window Can express a rolling-window allowance, so capacity changes as requests age out of the window. A rolling policy avoids the concentrated reset boundary of a fixed window. Can match policies such as a maximum number of requests during any rolling period; implementation and state requirements vary.
Fixed-window counter Counts requests within discrete intervals, then resets at the next interval. Requests can concentrate on both sides of a reset and exceed the intended rate over a short span. Simple to understand and operate, but boundary bursts are a drawback. OWASP cautions against fixed windows in its bot-management guidance.
Leaky-bucket-style shaping Can smooth outbound work by processing it at a steadier pace. Behavior depends on the implementation and queue policy. Useful when smoothing is preferable to passing bursts through immediately; a hard-rejection policy instead refuses requests after its allowance is spent.

These are conceptual patterns, not guarantees about every product implementation. AWS describes API Gateway token-bucket throttling as tokens added at a configured rate and consumed by requests; it also documents burst capacity and cautions that throttles are best-effort targets rather than guaranteed ceilings. AWS API Gateway HTTP API throttling and AWS API Gateway REST API throttling describe those product-specific behaviors.

Should I rate limit by IP or API key?

Choose the key to match the abuse pattern and fairness policy. No single key works well in every situation.

  • IP address: Can constrain traffic from one source, including unauthenticated requests. It can also penalize many legitimate users behind a shared address, and distributed sources can evade a per-IP threshold.
  • User, account, API key, or access token: Can support consumer-specific quotas after identification. Credentials may be shared or stolen, however, and unauthenticated traffic has no such identity yet.
  • Independent controls for authentication: For login and similar flows, consider both username-targeted and IP-based limits. OWASP bot-management guidance recommends independent username and IP controls so one addresses targeted-account abuse while the other constrains high-volume source behavior; do not rely only on a composite IP-plus-username counter.

API keys are useful for identifying clients, but they are not a substitute for authorization or stronger protections on sensitive resources. OWASP advises: “Do not rely exclusively on API keys to protect sensitive, critical or high-value resources.” See the OWASP REST Security Cheat Sheet and OWASP automated threats guidance.

Where should limits be stricter?

A global request count treats every call as equally costly. That can leave a costly endpoint exposed even when ordinary routes are well protected. Assess policies by route and workload, especially for authentication and account-recovery endpoints, expensive searches, exports, and bulk operations.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Request frequency alone does not bound resource consumption. A single request may ask for a very large result set, carry a large payload, or trigger expensive processing. Add controls suited to the work, such as input-size and result-count limits, and consider concurrency or workload caps for operations whose cost varies substantially per request. OWASP discusses these risks in its API4: Unrestricted Resource Consumption guidance and API Security project materials.

What should I return when an API rate limit is exceeded?

Return HTTP 429 Too Many Requests when rejecting a request because the client exceeded a rate limit. Give legitimate clients enough documented information to recover, without exposing sensitive internal enforcement details.

A Retry-After header can tell clients when to try again if the service can provide a useful delay. Header conventions differ across providers. For example, Cloudflare documents Ratelimit, Ratelimit-Policy, and retry-after; in its documented behavior, Retry-After is the number of seconds, rounded up, until more capacity is available. Those names and semantics are Cloudflare-specific, not a guarantee for every API. See Cloudflare API limits.

Rank #4
API Security in Action
  • API Security in Action
  • Manning Publications
  • ABIS BOOK

Clients should honor a supplied retry delay when applicable and avoid immediate retry loops. After repeated throttling failures, increasing backoff intervals reduce unnecessary retry traffic; AWS recommends controlling retry calls and increasing backoff for repeated failures in its Well-Architected guidance on limiting retries.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Can an API gateway enforce the policy?

Gateways can be a practical enforcement point, with available scopes depending on the product and configuration. AWS API Gateway, for example, documents token-bucket throttling and configuration at account, route, method, and usage-plan scopes across its HTTP and REST API products. Consult the relevant product documentation because these scopes and controls are not interchangeable across gateway types.

A configured throttle should not automatically be treated as a hard capacity ceiling. AWS states in its HTTP API documentation: “Throttles are applied on a best-effort basis and should be thought of as targets rather than guaranteed request ceilings.” Verify the behavior and limits of the gateway you actually use, and keep backend and upstream protections appropriate to your service.

Does a rate limit stop denial-of-service attacks?

No. A limiter can contain traffic at the point where it is enforced, but a single API-level policy does not guarantee protection against distributed attacks or replace upstream availability defenses. An IP limit may be evaded by distributed sources, while an account or credential limit cannot help with all unauthenticated traffic. Treat rate limiting as one layer alongside authentication, input and workload controls, monitoring, and infrastructure-level protections.

Quick Recap

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.