[{"data":1,"prerenderedAt":39},["ShallowReactive",2],{"snippet-apigateway-log-insights-caching":3},{"id":4,"title":5,"contributors":6,"description":8,"extension":9,"gitHub":10,"introBox":13,"languages":19,"meta":21,"resources":22,"services":23,"snippets":25,"stem":35,"tags":36,"type":37,"__hash__":38},"snippets\u002Fsnippets\u002Fapigateway-log-insights-caching.json","Find if the API requests are being served from Cache",[7],"content\u002Fcontributors\u002Fpallavi-bhat.json","Provides CloudWatch log insights queries to find the requests served from Cache","json",{"template":11},{"repoURL":12},"https:\u002F\u002Fgithub.com\u002Faws-samples\u002Fserverless-snippets\u002Ftree\u002Fmain\u002Fapigateway-log-insights-caching",{"headline":14,"text":15},"How it works",[16,17,18],"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.",[20],"css",{},null,[24],"apigw",[26,29,32],{"title":27,"language":20,"code":28},"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\n| parse @message '\"requestId\":\"*\"' as requestId\n| parse @message '\"IntegrationLatency\":\"*\"' as IntegrationLatency\n| filter ispresent(IntegrationLatency)\n| sort by @timestamp desc\n| display @timestamp, requestId, IntegrationLatency",{"title":30,"language":20,"code":31},"For Execution logs: NOTE: Requires Error and Info logs with Data tracing enabled.","fields @timestamp, @message\n| filter @message like 'Integration latency'\n| parse @message \u002FIntegration latency: (?&#60;IntegrationLatency&#62;.*?) ms\u002F\n| parse @message \u002F(?&#60;requestID&#62;[0-9a-zA-Z-]{36}).*\u002F\n| sort by @timestamp desc\n| display @timestamp, requestID, IntegrationLatency",{"title":33,"language":20,"code":34},"Following query can be used to list the requests which were not served from cache:","fields @timestamp, @message\n| filter @message like 'Integration latency'\n| parse @message \u002FIntegration latency: (?&#60;IntegrationLatency&#62;.*?) ms\u002F\n| parse @message \u002F(?&#60;requestID&#62;[0-9a-zA-Z-]{36}).*\u002F\n| filter IntegrationLatency &#62; 0\n| sort by @timestamp desc\n| display timestamp, IntegrationLatency, requestID","snippets\u002Fapigateway-log-insights-caching",[],"Integration","TRV1IZo_Y-KA5aS94zWStAI3xqFulHhr0xR5Jebt38g",1792578863869]