Find if the API requests are being served from Cache

Provides CloudWatch log insights queries to find the requests served from Cache

Caching is a feature of API Gateway which stores and reuses API responses for a specified TTL enhancing the responsiveness of the API.
One way to monitor if the requests are being served from cache or no is by using the API Gateway CacheMissCount and CacheHitCount CloudWatch metrics. The periodic pattern in the metric graph display the caching behavior.
Another approach to check specifically which all requests are served from cache or backend, given the access logs or execution logs have been enabled on the API Gateway is using the following CloudWatch log insight queries.

For Access logs: NOTE: The access log format must contain $context.integrationLatency access logging variable which will log the integration latency of the request. Change the requestId and IntegrationLatency filters as per the logging format you have

fields @timestamp, @message
| parse @message '"requestId":"*"' as requestId
| parse @message '"IntegrationLatency":"*"' as IntegrationLatency
| filter ispresent(IntegrationLatency)
| sort by @timestamp desc
| display @timestamp, requestId, IntegrationLatency
                                


For Execution logs: NOTE: Requires Error and Info logs with Data tracing enabled.

fields @timestamp, @message
| filter @message like 'Integration latency'
| parse @message /Integration latency: (?<IntegrationLatency>.*?) ms/
| parse @message /(?<requestID>[0-9a-zA-Z-]{36}).*/
| sort by @timestamp desc
| display @timestamp, requestID, IntegrationLatency
                                


Following query can be used to list the requests which were not served from cache:

fields @timestamp, @message
| filter @message like 'Integration latency'
| parse @message /Integration latency: (?<IntegrationLatency>.*?) ms/
| parse @message /(?<requestID>[0-9a-zA-Z-]{36}).*/
| filter IntegrationLatency > 0
| sort by @timestamp desc
| display timestamp, IntegrationLatency, requestID
                                


Created by:

Pallavi Bhat

Pallavi Bhat

Pallavi is a Cloud Support Enginner focusing on serverless at Amazon Web Services.

Follow on LinkedIn