AWS Builder Center
Protecting Public APIs with AWS Rate Limiting

Protecting Public APIs with AWS Rate Limiting

Learn how to protect public APIs from excessive traffic, abuse, and brute-force attempts using AWS WAF, API Gateway, Cognito, Lambda, and DynamoDB. Explore practical architecture diagrams, real-world problems, HTTP 429 responses, security strategies, and production-ready rate-limiting techniques.

Java Backend Developer | Spring Boot | REST APIs | MySQL | AWS Cloud Learner
Protecting Public APIs with AWS Rate Limiting 🔐
How to Prevent API Abuse, Control Traffic, and Build More Reliable Backend Systems
Imagine launching a new application. Everything works perfectly during testing.
Suddenly, your API starts receiving thousands of requests within seconds.
Your database becomes overloaded, response times increase, infrastructure costs rise, and legitimate users struggle to access your application.
What went wrong?
The problem might not be your application code. It could be that your APIs are accepting more requests than your system can safely handle.
This is where API Rate Limiting becomes an important part of backend engineering and cloud security.
In this article, we will explore how to protect public APIs using AWS services, understand different rate-limiting strategies, and design a practical architecture with real-world examples.
---
1. What Is API Rate Limiting?
API Rate Limiting is a technique used to control how many requests a client can send to an API within a specific period.
For example:
Imagine an application that allows a free user to make 60 API requests per minute.
If the same user sends 100 requests within that minute, the system can temporarily reject requests beyond the configured limit.
Instead of allowing unlimited traffic, the backend controls incoming requests.
Simple Example
Client Application
|
v
API Request
|
v
Check Request Limit
|
+----+----+
\| |
v v
Within Limit
Limit Exceeded
\| |
v v
Process API HTTP 429
Request Too Many Requests
|
v
Return Response
The main objective is to protect backend resources while ensuring fair access for legitimate users.
2. Why Do Applications Need Rate Limiting?
Let's understand some common problems.
Problem| What happens? API abuse| A client sends excessive requests Brute-force attacks| Attackers repeatedly try login credentials Web scraping| Bots collect data through repeated API calls Traffic spikes| Too many requests overload the backend High infrastructure costs| Uncontrolled requests increase resource usage Database overload| Repeated requests create unnecessary database queries
Without proper traffic controls, even a well-designed backend can become unreliable.
3. Proposed AWS Architecture 🏗️
AWS provides multiple services that can work together to protect APIs.
Our proposed architecture uses:
- Amazon CloudFront - AWS WAF - Amazon API Gateway - Amazon Cognito - AWS Lambda - Amazon DynamoDB - Amazon CloudWatch
High-Level Architecture Diagram
flowchart TD A["Users / Mobile / Web Applications"] B["Amazon CloudFront"] C["AWS WAF"] D["Amazon API Gateway"] E["Amazon Cognito / JWT Authorizer"] F["API Throttling"] G["Backend Service - Lambda / ECS"] H["Rate Limit Service"] I[("Amazon DynamoDB")] J["Amazon CloudWatch"]
A --> B
B --> C
C --> D
D --> E
E --> F
F --> G
G --> H
H --> I
G --> J
H --> J
Understanding the AWS Services
1. Amazon CloudFront
Acts as an entry point for application traffic and can distribute requests through AWS edge locations.
2. AWS WAF
Helps protect applications by filtering unwanted traffic and applying rate-based rules against excessive requests.
3. Amazon API Gateway
Provides a managed API entry point. It supports request throttling at different levels.
4. Amazon Cognito
Handles user authentication and provides identity tokens that can be used to identify authenticated API users.
5. AWS Lambda
Can execute backend business logic and custom rate-limiting checks without managing servers.
6. Amazon DynamoDB
Can store request counters and user-specific rate-limit information.
7. Amazon CloudWatch
Helps monitor API requests, errors, throttling activity, and application performance.
4. Understanding the Three Layers of Rate Limiting
An effective API protection strategy should not depend on only one layer.
Layer 1: IP-Based Rate Limiting
Problem: A single IP address sends too many requests.
Solution: AWS WAF rate-based rules can identify and limit excessive traffic based on IP addresses or configured aggregation criteria.
Example:
Incoming Requests | v AWS WAF | v Check IP Request Rate | +---+---+ | | Normal Excessive Traffic Traffic | | v v Allow Block / Challenge
This helps reduce unwanted traffic before it reaches the backend.
Layer 2: API Gateway Throttling
Problem: Too many requests reach the API backend, even when they come from different users.
Solution: Configure throttling limits at the API or stage level.
API Gateway uses the Token Bucket algorithm to manage request rates and bursts.
Example configuration:
Setting| Illustrative value Request rate| 100 requests/second Burst capacity| 200 requests Excessive requests| HTTP 429
These values are examples, not universal recommendations.
API Gateway throttling is best-effort, not a guaranteed hard request ceiling.
Layer 3: User-Based Rate Limiting
Problem: Different users have different usage requirements.
For example, a free user should not necessarily receive the same API request allowance as a premium customer.
Solution: Implement application-level rate limiting using authenticated user identity and a shared data store.
Example policy:
User Type| Example Limit Free User| 60 requests/minute Premium User| 300 requests/minute Internal Service| Custom limit
The backend can identify the user through authenticated token claims and check their request counter before processing the API request.
Amazon DynamoDB or Amazon ElastiCache can support shared rate-limit state.
5. Real-World Problem and Solution Examples 💡
Example 1: Protecting a Login API
Problem:
An attacker repeatedly sends login requests using different passwords.
This can lead to brute-force attempts and unnecessary backend processing.
Solution:
- Apply AWS WAF rate-based protection. - Configure application-level limits for login attempts. - Use Amazon Cognito where appropriate for authentication. - Introduce temporary account protection and additional verification. - Monitor repeated failures using CloudWatch.
Result: Excessive login attempts can be controlled while protecting legitimate users.
Example 2: Preventing API Abuse
Problem:
A client sends thousands of requests to a product API to collect application data.
Solution:
- Apply IP-based rules through AWS WAF. - Configure API Gateway throttling. - Introduce user-specific limits. - Monitor unusual request patterns.
Result: Uncontrolled API usage becomes easier to detect and restrict.
Example 3: Protecting Infrastructure Costs
Problem:
A public API receives unexpected traffic, increasing Lambda executions and database requests.
Solution:
- Set appropriate API Gateway throttling targets. - Configure AWS WAF protections. - Apply per-user quotas where necessary. - Monitor spending through AWS Budgets. - Configure CloudWatch alarms for unusual traffic.
Result: Traffic becomes more controlled, and unexpected usage is easier to identify.
6. What Happens When a User Exceeds the Limit? 🔄
Let's understand the request lifecycle.
sequenceDiagram autonumber actor User participant WAF as AWS WAF participant API as API Gateway participant Auth as Cognito participant Lambda as AWS Lambda participant DB as DynamoDB
User->>WAF: Send API Request
WAF->>WAF: Evaluate Traffic Rules
WAF->>API: Forward Allowed Request
API->>Auth: Validate User Token
Auth-->>API: Authentication Result
API->>Lambda: Invoke Backend
Lambda->>DB: Check User Request Limit

alt Limit Available
DB-->>Lambda: Request Allowed
Lambda-->>API: HTTP 200 OK
API-->>User: Return API Response
else Limit Exceeded
DB-->>Lambda: Request Rejected
Lambda-->>API: HTTP 429
API-->>User: Too Many Requests
end
This demonstrates how multiple layers work together to protect an API.
7. Understanding HTTP 429: Too Many Requests
HTTP 429 is the standard status code used when a client sends too many requests within a given period.
Example response:
HTTP/1.1 429 Too Many Requests Retry-After: 10 Content-Type: application/json
Response body:
{ "status": 429, "message": "Too many requests. Please try again later.", "retryAfter": 10 }
The Retry-After header tells the client how long to wait before retrying, when the server provides that guidance.
A good client should use exponential backoff with random jitter instead of immediately sending repeated requests.
This prevents additional traffic from making the problem worse.
8. Important Security Considerations 🛡️
Rate limiting alone does not make an API secure.
A production-ready backend should also consider:
Authentication: Validate user identity before allowing access to protected endpoints.
Authorization: Ensure users can access only the resources they are permitted to use.
HTTPS: Protect data during transmission.
IAM Policies: Follow the principle of least privilege when granting AWS service permissions.
Input Validation: Reject invalid or unexpected request data.
Monitoring: Track unusual traffic, failed requests, and throttling events.
Distributed Rate Limiting: Use a shared data store so that multiple backend instances follow consistent user-level limits.
One important distinction: API keys in API Gateway usage plans are useful for usage tracking and metering, but AWS does not recommend using them as a replacement for authentication or authorization.
9. Monitoring and Troubleshooting 📊
Implementing rate limiting is only the beginning. Developers must continuously monitor how it behaves.
Useful monitoring practices include:
Metric| Why it matters Total API Requests| Understand traffic volume HTTP 429 Responses| Identify throttling activity HTTP 5XX Errors| Detect backend failures API Latency| Monitor response performance Lambda Errors| Identify execution failures DynamoDB Throttling| Detect database capacity issues
Amazon CloudWatch can help visualize these metrics and configure alarms.
For example, if HTTP 429 responses suddenly increase, developers can investigate whether the traffic is malicious or whether legitimate users are hitting limits that are too restrictive.
10. Common Mistakes Developers Should Avoid
1. Applying the same request limit to every endpoint. 2. Treating API keys as authentication credentials. 3. Depending only on IP-based protection. 4. Ignoring legitimate users behind shared networks. 5. Retrying failed requests immediately. 6. Using separate in-memory counters across multiple backend instances. 7. Ignoring monitoring and application logs. 8. Setting arbitrary limits without load testing.
Also remember that AWS WAF rate-based rules are approximate controls, not precise quota enforcement. API Gateway throttling and usage-plan quotas are also best-effort.
For strict per-user business quotas, enforce the rule in your application using an appropriate atomic shared-state mechanism.
11. Practical Implementation Roadmap 🚀
If you want to implement this architecture in a real backend project, follow these steps:
Step 1: Build REST APIs using Java and Spring Boot.
Step 2: Configure Amazon API Gateway as the API entry point.
Step 3: Integrate Amazon Cognito for authentication.
Step 4: Configure AWS WAF rate-based rules.
Step 5: Set API Gateway throttling targets.
Step 6: Implement user-level rate limiting using DynamoDB or Redis.
Step 7: Return proper HTTP 429 responses when limits are exceeded.
Step 8: Configure CloudWatch dashboards and alarms.
Step 9: Perform load testing to identify suitable limits.
Step 10: Review security, performance, and cost before production deployment.
---
12. Final Thoughts 🎯
API security is not only about preventing unauthorized access. It is also about ensuring that applications remain available and responsive when traffic increases unexpectedly.
AWS provides useful building blocks such as AWS WAF, Amazon API Gateway, Amazon Cognito, Lambda, DynamoDB, and CloudWatch to help developers implement different layers of API protection.
However, the most important lesson is that no single service solves every API security problem.
My Key Takeaway:
A well-designed API should not blindly accept every request. It should understand who is making the request, how frequently the API is being accessed, and whether the system can safely process that traffic.
Good backend engineering is not just about handling more requests. It is about handling requests intelligently.
Let's Discuss 👇
If you were building a public REST API for a real-world application, which protection would you implement first?
1. AWS WAF – To block unwanted traffic. 2. API Gateway – To control request rates. 3. Application-Level Rate Limiting – To enforce user-specific limits.
Which approach would you choose, and why?
I'd love to learn from your experience and perspectives!
#AWS #APIGateway #Cybersecurity #BackendDevelopment #CloudComputing
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article