3.4.2. API Performance Optimization
First Principle: Optimizing API performance (especially with API Gateway) involves implementing caching, throttling, and efficient backend integrations, ensuring low latency and high throughput for application interactions.
For developers building APIs with Amazon API Gateway, optimizing performance is crucial for providing a fast and responsive user experience.
- Caching (API Gateway Caching):
- Concept: API Gateway can cache responses from your backend, reducing the number of calls to your backend services (e.g., Lambda functions) and lowering latency for repeat requests.
- Optimization: Enable API Gateway caching for methods that return static or infrequently changing data.
- Throttling & Usage Plans:
- Concept: Control the rate at which API callers can submit requests to your APIs to prevent overload and DDoS attacks.
- Optimization: Set appropriate default throttling limits and use usage plans to define custom rates for different users or applications.
- Efficient Backend Integrations:
- Concept: Ensure your backend services (Lambda, EC2, DynamoDB) are performant. API Gateway can only be as fast as its slowest backend.
- Optimization: Optimize Lambda function performance, use DynamoDB capacity planning, or implement database caching.
- Edge-Optimized Endpoints: (Default type) Leverage Amazon CloudFront's global network to reduce latency for geographically dispersed users.
API Gateway Custom Domains
By default, API Gateway assigns a URL like https://abc123.execute-api.us-east-1.amazonaws.com/prod. For production APIs, you map a custom domain (e.g., api.yourapp.com) using API Gateway's custom domain name feature. This requires an ACM certificate in the same region (for Regional endpoints) or in us-east-1 (for edge-optimized). You can map multiple API stages to path segments under one domain (api.yourapp.com/v1, api.yourapp.com/v2).
CloudFront Caching and Cache Headers
When using CloudFront in front of API Gateway (or any origin), cache behavior depends on HTTP headers. CloudFront uses the Cache-Control and Expires headers from your origin to determine TTL. Developers control caching by setting these headers in their Lambda response:
Cache-Control: max-age=300ā CloudFront caches for 5 minutesCache-Control: no-cacheā CloudFront always revalidates with origin- Cache key configuration: By default, CloudFront ignores query strings and headers when building cache keys. If your API returns different content based on
AuthorizationorAccept-Languageheaders, you must explicitly include those headers in the cache key ā otherwise all users get the same cached response.
Scenario: Your API Gateway endpoint is experiencing high latency, and many requests are hitting the backend Lambda function even for static data. You need to improve responsiveness and protect the backend from overload.
ā ļø Exam Trap: API Gateway caching caches the response, not the request. If a question mentions stale data, the answer is usually to invalidate the cache or reduce the TTL ā not to disable caching entirely.
