API Gateway Cache for POST Method
The intent of this article is to explain the Amazon API Gateway caching pattern for POST methods that is implemented at one of the largest airline clients.
Join the DZone community and get the full member experience.
Join For FreeObjective
The intent of this article is to explain the Amazon API Gateway caching pattern for POST methods that is implemented at one of the largest airline clients.
Problem Statement
The API Gateway does not directly support caching features based on the method request body for the POST method. Thus, a mechanism and pattern are required to implement caching on a POST method in Amazon API Gateway.
Solution
Amazon API Gateway Cache is a managed service that provides a caching layer for APIs hosted on API Gateway. It helps improve API performance, reduce latency, and lower the load on the backend servers by caching API responses.
When caching is enabled at the stage level, only GET methods have caching enabled by default. To facilitate caching for other methods like POST, the first step is to create an API Gateway API, a resource for the API Gateway endpoint, and a POST method with an integration that calls the backend API (for example, a Lambda function). Subsequently, enable the caching at the stage level and then override the cache settings at the method level. Then, map the request body to a custom header and use this custom header as a cache key. Please follow the below steps in detail for this implementation.
1. Select “Amazon API Gateway” service and click “Create API.”
2. Select Rest and Build
3. Choose the protocol as a “REST”
4. Enter your API name “api-gateway-test-api” (you can also import Swagger)
5. Click on “Create API”
6. Create Resource (testcache) and Method (POST) for resource
7. Go to the Resources tab.
8. To create the method, go to Actions -> Create Method -> Post.
Note: When a request is made to the API Gateway method, the body parameter in the integration request will be populated with the entire request body in JSON format.
9. Click Integration Request (top right on the AWS Gateway POST method execution screen).
10. Configure the AWS API Gateway to work with the existing Lambda function (backend) by adding the Gateway in the adjacent Lambda function box. Choose the Lambda Function Integration type. Select Default Timeout. Please make sure the integration is non-proxy.
11. Inside the method (POST) Integration request under HTTP Header, Add the key as "cachebody
" and the value as "method.request.body.byrequestnumber
". Then tick the Caching box and Save.
Here, a new cache key named cachebody
is added and set to method.request.body.byrequestnumber
. It means the value of the byrequestnumber
in the incoming request of the body will be used to generate a cache key “cachebody
” for the API Gateway method (POST). This can help improve the performance of the API by caching responses based on the value of the byrequestnumber
in the request body.
12. Ensure the method execution page for POST looks like as below.
13. To deploy the API, go to Resources, Actions, and then Deploy API (create a new stage for development)
14. Enable API cache in Stage settings. Inside Cache Settings, Enable Method Cache, Save settings.
15. Just enabling cache on the stage doesn't work alone. Make sure to override the caching settings on the request.
16. API Gateway Cache provides metrics to monitor cache usage, hit rate, and latency. To check the functioning of API G/W Cache, the two metrics to be observed are CacheHitCount
and CacheMissCount
. When API caching is enabled, CacheHitCount
represents the number of requests served from the API cache in a given period, whereas CacheMissCount
displays the number of requests served from the backend in a given period.
17. The URL to invoke the resource (API) testcache
, API ID “m7xyzabcdef7” for Stage apigatewaycache
in region “us-east-1
” can be found as:
https://${MyApiGatewayID}.execute-api.${AWS::Region}.amazonaws.com/${StageName}/${my-resource}
where
MyApiGatewayID: m7xyzabcdef7 AWS::Region: us-east-1 StageName : apigatewaycache my-resource: testcache
The invoke URL formed.
Note: POST requests can have certain limitations, as the request payload might contain sensitive or dynamic data that should not be cached. In these cases, it is advisable to configure API Gateway to exclude specific headers or query parameters from the cache key or use the Vary header to vary the cache entry based on the request headers.
Opinions expressed by DZone contributors are their own.
Comments