Back to Blog
Lesson 37 of the AWS: AWS Core Services for Developers course
Cloud NativeAugust 14, 20264 min read

Implementing API Throttling: Managing Traffic in API Gateway

Learn to protect your serverless backend from abuse with API Gateway throttling. Master usage plans, rate limits, and burst limits for stable traffic management.

AWSAPI GatewayThrottlingInfrastructure as CodeCDKServerless
Detailed macro photograph of a honeybee resting on wheat against a black background.

Previously in this course, we covered Securing APIs with API Keys. While API keys help identify who is calling your service, they don't prevent a single user or a malicious actor from overwhelming your backend with thousands of requests per second. In this lesson, we add the final layer of traffic control: Throttling.

Throttling is the practice of limiting the number of requests a user can make to your API within a specific timeframe. Without it, a sudden surge in traffic—or a simple bug in a client application—can exhaust your Lambda concurrency limits, drive up costs, and cause cascading failures across your infrastructure.

Understanding Rate Limits and Burst Limits

API Gateway uses a token bucket algorithm to manage traffic. This allows you to define two distinct but complementary limits:

  1. Rate Limit (Steady State): The average number of requests per second (RPS) the API allows over a sustained period.
  2. Burst Limit: The maximum number of concurrent requests the API can handle at any single moment, allowing for temporary spikes in traffic that exceed the steady-state rate.

Think of it like a faucet. The Rate Limit is the steady flow of water, while the Burst Limit is the size of the bucket under the tap. If the tap stays on too long, the bucket fills up, and the extra water spills over (this is when your users receive an HTTP 429 Too Many Requests error).

Implementing Usage Plans in CDK

To implement throttling, we must group our API stages into a Usage Plan. A usage plan acts as a container for your throttling settings and can be associated with specific API keys.

In our running web app project, we'll extend the existing API Gateway setup. If you've been following along with the Project Integration: Exposing the API with AWS CDK lesson, your infrastructure is already defined. We now add the UsagePlan construct to that stack.

TYPESCRIPT
import { UsagePlan, ThrottlingSettings } from CE9178">'aws-cdk-lib/aws-apigateway';

const myUsagePlan = new UsagePlan(this, CE9178">'MyUsagePlan', {
  name: CE9178">'StandardUsagePlan',
  apiStages: [{
    api: myApi,
    stage: myApi.deploymentStage,
  }],
  throttle: {
    rateLimit: 100, // Average 100 requests per second
    burstLimit: 200  // Allow a temporary spike up to 200 requests
  }
});

Hands-on Exercise: Configure Your Throttling

Your task is to protect your project's backend. Update your CDK stack to enforce a strict limit on your development environment to ensure you don't accidentally exceed your account limits during testing.

  1. Open your API infrastructure stack file.
  2. Define a UsagePlan with a rateLimit of 5 and a burstLimit of 10.
  3. Deploy your updated stack using cdk deploy.
  4. Use a tool like ab (Apache Benchmark) or a simple script to send 15 rapid-fire requests to your endpoint.
  5. Observe the 429 Too Many Requests error once you exceed your defined limits.

Common Pitfalls

  • Global vs. Per-Key Throttling: If you define settings on the API Gateway stage itself, it applies to all traffic globally. If you want to limit individual users differently, you must assign those throttles to a UsagePlan and associate it with specific API keys.
  • Forgetting the Burst Limit: If you set your rateLimit and burstLimit to the same low number, your API will be extremely brittle. Always keep your burstLimit slightly higher than your rateLimit to accommodate natural jitter in network traffic.
  • Ignoring Account-Level Limits: Remember that your API Gateway settings are constrained by your AWS Account-level limits. If your Account limit is 1,000 RPS, setting an API-level throttle to 5,000 RPS will not actually allow 5,000 requests.

Frequently Asked Questions (FAQ)

Q: What happens when a request is throttled? A: API Gateway immediately returns an HTTP 429 status code. Your backend Lambda is never invoked, which saves you from execution costs and protects your downstream databases.

Q: Can I set different limits for different customers? A: Yes. You create multiple Usage Plans (e.g., "Free Tier," "Premium Tier") with different rateLimit and burstLimit configurations and associate the corresponding API keys with the appropriate plan.

Q: Does throttling affect my CloudWatch metrics? A: Absolutely. API Gateway logs these events, and you can see them in your CloudWatch metrics under 4xxError counts. This is a great signal to set up an alarm for potential abuse.

Recap

We've moved from simply exposing an API to actively managing its consumption. By defining UsagePlans, configuring rateLimits for steady traffic, and setting burstLimits for spikes, you ensure your backend remains stable even under unexpected load. While Redis Rate Limiting is a powerful pattern for custom application-level logic, API Gateway's native throttling handles the heavy lifting at the edge.

Up next: We will explore Handling Asynchronous Events to decouple our services further using SQS.

Similar Posts