API Gateway is a powerful tool for creating a coherent API out of a set of multiple disconnected remote function providers, and API Gateway with AWS Lambda, when used properly, can give you the capacity to create truly powerful serverless APIs.

However, given that API Gateway is a layer between your requests and the code itself, there are latency concerns that API Gateway Lambdas may face that may not be present when working with other architectures. Below, we’ll look into some of these performance pitfalls, explore their cause and, where possible, suggest alternatives that can help resolve any latency issues with your API Gateway-governed AWS Lambda functions.

A Brief Overview of API Gateway

A common problem faced by developers, particularly those dealing with microservice-oriented architectures, is devising a way to create a coherent API from functions hosted across a range of different machines.

To organize these different machines into coherent APIs with understandable URL patterns, most developers end up using an API gateway, which is a central dispatch point for requests to your various backend functions.

AWS’ API Gateway is an implementation of this pattern with a focus on AWS services. Through the use of API Gateway’s UI, you can tie together EC2 functions, standalone external API servers, and AWS Lambda functions behind a single, coherent API. Furthermore, API Gateway gives you the capacity to secure all of your API calls in an always-available environment, giving you a production-ready application interface that scales with your application’s performance and security needs.

Image source: AWS

Known Performance Issues

Given that API Gateway is a layer inserted between your calling application and your code, you should expect a small amount of latency to be present simply due to the translation needed to route your request to the appropriate resource. Generally these transformations and translations are simple and have minimal impact on your API requests. Several user reports, though, have indicated that this latency can at times be far larger than expected, on the order of hundreds of milliseconds.

There’s a limit to the root causes we can identify in these aberrant latency cases, particularly since we don’t have complete internal details on exactly how API Gateway is implemented in the AWS ecosystem. However, there are a few common issues that can introduce latency to API Gateway Lambda functions that we can watch out for:

Cross-region function calls

API Gateway calls to resources that exist in a different AWS Region tend to have more latency added than calls where API Gateway and the resource are colocated.

Resource-based versus Role-based authentication

API Gateway Lambdas using IAM Roles to authenticate their API endpoints may incur extra costs due to the backend verification needed to confirm a user’s identity and access.

Encryption/Decryption of caches

If you have request caching with encryption enabled, the encryption and decryption of the cache entries can add additional latency to your request.

Cold request starts

This is less of an issue with API Gateway, and more of a general concern when working with AWS Lambda functions, but AWS Lambda’s hot/cold approach to resource availability can increase latency of calls to functions that have been idle.

Performance Issue Workarounds

Luckily, with each of these performance issues native to AWS, there are a few workarounds that can be relied upon to address the issue. Below are a couple suggestions to help reduce the impact of the AWS API Gateway translation, improving the response times of your AWS Lambda functions.

Co-locate your resources

The simplest performance improvement should also be the most obvious – co-locating your application’s resources into the same AWS region can reduce accidental cross-region chatter that increases call latency.

Modify your authentication

AWS API Gateway Resource Policies can let you use resource authentication instead of role authentication on your API endpoints, improving throughput by removing the need to perform role verification.

Disable cache encryption

If your application is properly secured, you can disable cache encryption/decryption and ensure your API request cache fetches are more efficient.

Keep your serverless functions warm

Less of an API Gateway solution and more of a general suggestion for serverless functions, but as AWS Lambda uses a hot-cold architecture, occasionally calling your serverless functions to keep them “warm” will reduce the number of cold starts your application sees, reducing overall request latency.

Alternative Approaches

While the above workarounds should resolve most latency issues to the point where they are far less impactful to your API Gateway calls to AWS Lambda, if performance is a serious consideration you may be stuck looking for ways around an insurmountable delay in your requests. Below are a few alternatives to an AWS API Gateway-driven architecture that would help reduce latency in many scenarios.

Host your functions on EC2

Simply promoting a Lambda function to a standalone microservice on AWS EC2 can give you a dedicated endpoint to hit that completely removes API Gateway from the equation. This would require writing a wrapper function and installing a webserver to handle requests, but will generally be more efficient.

Containerize your serverless functions

Building on the prior solution, making use of Docker containers and a container host can give you a lot of the benefits of a serverless environment without needing to maintain your own EC2 instances.

Evaluate other serverless hosts

Both Google Cloud Compute and Microsoft Azure functions provide a native HTTP interface for their serverless functions, giving you the ability to bypass the translation of an API Gateway by calling the resources directly.

Build your own web server

While the technology driving serverless function technology is impressive and hard to replicate, the functions themselves are very straightforward bits of code that can be called using a clearly-defined set of parameters. By standing up an NGINX or Apache web server and writing a simple wrapper application, you can easily write a microservice application that simply runs your lambda functions on your own hardware – giving you full control over the ecosystem and its availability.

Closing Thoughts

AWS API Gateway provides developers a convenient tool to create serverless applications with a unified API interface. While there are latency issues present with the usage of API Gateway, some of the most common latency-introducing issues have workarounds that can improve the general performance of your serverless requests. With proper configuration, AWS Lambda functions behind API Gateway URLs can be as highly-performant as resource and other trigger-based instantiations of your functions, but ultimately the choice lies in whether or not the latency of a serverless approach is too impactful to your user experience.