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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Spring Boot GoT: Game of Trace!
  • HTTP API: Key Skills for Smooth Integration and Operation, Part 2
  • That Can Not Be Tested!: Spring Cache and Retry
  • The Power of Caching: Boosting API Performance and Scalability

Trending

  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  • Medallion Architecture: Efficient Batch and Stream Processing Data Pipelines With Azure Databricks and Delta Lake
  • Teradata Performance and Skew Prevention Tips
  • The Role of Functional Programming in Modern Software Development
  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
7.5K 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

  • Spring Boot GoT: Game of Trace!
  • HTTP API: Key Skills for Smooth Integration and Operation, Part 2
  • That Can Not Be Tested!: Spring Cache and Retry
  • The Power of Caching: Boosting API Performance and Scalability

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!