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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • GET, POST, PUT, DELETE: Choosing HTTP Verbs in a Real .NET Core Web API, Not Just by the Textbook
  • How Cache Invalidation Caused Memory Pressure and API Failures at Scale
  • Prompt Caching: Overriding Tokenization for Faster and More Cost-Effective AI
  • The Bill You Didn't See Coming

Trending

  • Member Spotlight: Srinivas Chippagiri
  • The Self-Hosting Math Changed While We Were Still Arguing About Sovereignty
  • 5 Mistakes Companies Make When Working With Staff Engineers
  • PWA on iOS, Part 3: Chrome on iPhone Is WebKit Too. How One Engine Sets the Rules for PWAs on iOS

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Oct. 27, 23 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • GET, POST, PUT, DELETE: Choosing HTTP Verbs in a Real .NET Core Web API, Not Just by the Textbook
  • How Cache Invalidation Caused Memory Pressure and API Failures at Scale
  • Prompt Caching: Overriding Tokenization for Faster and More Cost-Effective AI
  • The Bill You Didn't See Coming

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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