Protecting Backends with Throttling, Usage Plans and API Keys
This lesson explains how API Gateway protects the backend from traffic spikes and limits individual clients. Throttling and usage plans with API keys both reject excess requests with 429 Too Many Requests.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how the token bucket rate and burst limits work and which limit applies when several are set.
- Describe how usage plans and API keys limit and meter individual clients.
- Explain why API keys identify clients but do not authenticate them.
- Identify the API Gateway features that protect a backend from excessive load.
How throttling works
API Gateway throttles with a token bucket: the rate is the steady number of requests per second, and the burst is how many requests can arrive at once. Limits apply at several levels, and the narrowest applicable limit wins.
The account limit applies to all APIs in a Region together, so one busy API can throttle the others. Developers can set lower stage limits, and lower still method limits for individual routes within a stage, for example to protect an expensive search operation. Usage plans add per-client limits for REST APIs: each client sends an API key in the x-api-key header, and the plan attached to that key sets the client's rate, burst and an optional quota of requests per day, week or month. API keys identify clients for metering; they are not a secure means of authentication and should be combined with an authorizer.
The following table summarises the levels at which limits apply.
| Level | Set by | Default |
|---|---|---|
| Account, per Region | AWS (raise through a quota request) | 10,000 requests per second, burst 5,000, shared by all APIs |
| Stage | Developer | Account limit |
| Method | Developer, per stage | Stage limit |
| Client (API key) | Developer, through a usage plan | None unless a plan is attached |
Throttled requests never reach the integration, so clients should retry them with exponential backoff. Throttling limits are applied on a best-effort basis and are not a security control or an exact quota.
10,000req/s
Default account rate per Region
5,000requests
Default account burst
Which limit applies?
Usage plans and API keys
A usage plan sets a rate, burst and optional quota (requests per day, week or month) and applies them to the API keys associated with it. Clients send their key in the x-api-key header, and each method must have API key required enabled.
- Usage plans and API keys are available on REST APIs only.
- API keys identify and meter clients but do not authenticate them, so always pair them with IAM, Cognito or a Lambda authorizer.
- Usage data per key can be downloaded for billing partners.
Example. A partner's key is placed on a usage plan of 50 requests per second, a burst of 100 and 100,000 requests per month. Requests beyond the rate, the burst or the monthly quota receive 429 until the limit resets.
PLAN_ID=$(aws apigateway create-usage-plan --name partner-standard \
--throttle rateLimit=50,burstLimit=100 --quota limit=100000,period=MONTH \
--api-stages apiId=a1b2c3d4e5,stage=prod --query id --output text)
KEY_ID=$(aws apigateway create-api-key --name partner-acme --enabled --query id --output text)
aws apigateway create-usage-plan-key --usage-plan-id $PLAN_ID --key-id $KEY_ID --key-type API_KEYUsage plans and API keys
Protecting the backend
API Gateway provides several features that protect the backend from excessive load. The following list describes them.
- Stage and method throttling keep a spike from overwhelming a Lambda function or database behind the API.
- Response caching on REST APIs answers repeated requests without calling the backend.
- For per-IP limits against abusive clients, use an AWS WAF rate-based rule rather than throttling, which cannot distinguish one caller from another.
One partner overwhelming an API
A public REST API serves five partners. One partner's batch job sends 3,000 requests per second, the backend database slows down, and every partner sees errors.
| Control | Setting | Effect |
|---|---|---|
| Usage plan per partner tier | Standard: 100 requests/s, burst 200, 1,000,000 per month | The batch job is limited to its plan; excess receives 429 |
| API key per partner | Required on every method | Each partner's traffic is identified and metered |
| Stage throttling | 1,000 requests/s for the whole stage | Protects the backend even if plans are misconfigured |
| Authorizer | Cognito client credentials grant | Authenticates partners; the key alone is not trusted |
The partner's job now retries with backoff when it receives 429, and the other partners are unaffected. Usage data per key also supports billing each partner for its usage.
Why should a client that receives 429 errors from API Gateway retry with exponential backoff rather than immediately?
Show answer
A 429 means the request was throttled before it reached the backend. Retrying at once adds load while the limit is still exceeded; backoff spaces the retries out until capacity is available again.
Exam Tips
Remember for the exam
- "Limit each customer to N requests per day" means usage plans with API keys.
- API keys are for metering and throttling, not security.
- A
429from API Gateway means a stage, method, usage plan or account limit was exceeded. - To block individual abusive IP addresses, use an AWS WAF rate-based rule.
- Usage plans require a REST API; HTTP APIs do not support them.