Search code examples
amazon-web-servicesamazon-ec2aws-lambdaaws-api-gateway

Is significant latency introduced by API Gateway?


I'm trying to figure out where the latency in my calls is coming from, please let me know if any of this information could be presented in a format that is more clear!

Some background: I have two systems--System A and System B. I manually (through Postman) hit an endpoint on System A that invokes an endpoint on System B. System A is hosted on an EC2 instance.

  • When System B is hosted on a Lambda function behind API Gateway, the latency for the call is 125 ms.
  • When System B is hosted on an EC2 instance, the latency for the call is 8 ms.
  • When System B is hosted on an EC2 instance behind API Gateway, the latency for the call is 100 ms.

So, my hypothesis is that API Gateway is the reason for increased latency when it's paired with the Lambda function as well. Can anyone confirm if this is the case, and if so, what is API Gateway doing that increases the latency so much? Is there any way around it? Thank you!


Solution

  • Heard from Amazon support on this:

    With API Gateway it requires going from the client to API Gateway, which means leaving the VPC and going out to the internet, then back to your VPC to go to your other EC2 Instance, then back to API Gateway, which means leaving your VPC again and then back to your first EC2 instance.

    So this additional latency is expected. The only way to lower the latency is to add in API Caching which is only going to be useful is if the content you are requesting is going to be static and not updating constantly. You will still see the longer latency when the item is removed from cache and needs to be fetched from the System, but it will lower most calls.

    So I guess the latency is normal, which is unfortunate, but hopefully not something we'll have to deal with constantly moving forward.