amazon-web-servicesaws-lambdaaws-api-gateway

API Gateway latency vs integration latency


For AWS' API Gateway, my understanding is that Integration Latency should always be less than Latency (see Amazon API Gateway dimensions and metrics).

However, when I compare the two, I am always getting the opposite order. This is true whether I look at the API Gateway Dashboard or view the metrics in Amazon CloudWatch. Some examples (I have little enough traffic to be able to compare individual data points)

 1. 2024-01-23 18:15 UTC: Integration Latency = 1235ms vs Latency = 628ms
 2. 2024-01-23 14:20 UTC: Integration Latency = 1582ms vs Latency = 802ms
 3. 2024-01-23  8:35 UTC: Integration Latency = 1207ms vs Latency = 614ms

Since the gap is large and consistently in the 'wrong' order, I have to assume my understanding of API Gateway metrics is incorrect. But how?

In all 3 of the above examples my Lambda function took ~450ms (+/- 35ms). Via lambda function's Monitor > Duration graph, as well as its CloudWatch logs.


Solution

  • So basically there is no problem with the actual metrics, but with how they are displayed.

    The issue is there are two API calls when a request comes in: OPTIONS and POST. Only the latter has Integration Latency, and the former has near-zero Latency.

    To make this concrete, say a request comes in and OPTIONS takes 10ms (total latency, no integration latency) then POST takes 1000ms (both for latency and integration latency; latency is more but the difference is negligible).

    The API Gateway > Dashboard graphs shows the two API calls averaged together. So you see (10ms + 1000ms) / 2 = 505ms Latency but only 1000ms / 1 = 1000ms Integration Latency in the graphs. And thus it looks like Integration Latency is greater (roughly double), even though this is not the case.

    If you want to confirm this, navigate to those API Gateway Dashboard graphs and click on "View in CloudWatch". Then change from "Line" to "Data Table". At this point the data has the same averaging issue, even if you look at the value of the "Max" column. You still need to change the "Statistic" from "Average" to "Maximum". Only then will the "Max" column show that latency is indeed greater or equal to integration latency.

    Hope that helps anyone with the same confusion!

    (FYI I only managed to understand this by opening support case on AWS. Kudos to them for breaking this down.)