DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Bill You Didn't See Coming
  • Debugging Distributed Flight Search: What Logs Alone Won’t Tell You
  • Spring Boot GoT: Game of Trace!
  • HTTP API: Key Skills for Smooth Integration and Operation, Part 2

Trending

  • Observability in Spring Boot 4
  • Retesting Best Practices for Agile Teams: A Quick Guide to Bug Fix Verification
  • The Agent Protocol Stack: MCP vs. A2A vs. AG-UI
  • Why Pass/Fail CI Pipelines Are Insufficient for Enterprise Release Decisions
  1. DZone
  2. Data Engineering
  3. Data
  4. API Gateway Cache for POST Method

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.

By 
Amit Upadhyaya user avatar
Amit Upadhyaya
·
Oct. 27, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
8.9K Views

Join the DZone community and get the full member experience.

Join For Free

Objective

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

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 

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. 

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).

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.
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)  

To deploy the API, go to Resources, Actions, and then Deploy API (create a new stage for development)

create stage

14. Enable API cache in Stage settings. Inside Cache Settings, Enable Method Cache, Save settings. 

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. 

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.

apigatewaycache

cloudwatch

cachemisscount

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.

API Cache (computing) POST (HTTP)

Opinions expressed by DZone contributors are their own.

Related

  • The Bill You Didn't See Coming
  • Debugging Distributed Flight Search: What Logs Alone Won’t Tell You
  • Spring Boot GoT: Game of Trace!
  • HTTP API: Key Skills for Smooth Integration and Operation, Part 2

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook