Optimizing Serverless Architectures on AWS — A Case Study in Reducing Latency

Anastasia Tokareva
Perfsys
Published in
4 min readMar 6, 2024

Serverless architectures on AWS offer scalability and cost-efficiency gains but often face a common challenge: latency. A frequent issue is the latency caused by Lambda cold starts — when a function initializes after a period of inactivity.

In August 2023, Tibica, a serverless application marketplace, reached out to Perfsys with a challenge. Despite leveraging AWS Lambda functions for their scalable and cost-efficient benefits, Tibica’s applications were hindered by slow response times due to Lambda cold starts. This issue delayed application workflows by nearly two seconds, leading to user dissatisfaction.

Case Study Description

The initial project architecture integrated AWS Lambda for each endpoint, supported by a custom Lambda authorizer for REST API management. Although functional, the setup was not optimized for performance, resulting in noticeable API request latency due to Lambda cold starts, a significant obstacle in the serverless application development.

We used AWS X-Ray to trace common user workflows and determine the bottlenecks in the flow. Most of Tibica’s requests were straightforward GET operations, fetching data without any complex logic under the hood. We decided to try switching from a traditional Lambda-based approach to one that uses direct database calls.

Using AWS API Gateway’s VTL (Velocity Template Language) to query the DynamoDB table directly, Tibica observed a nearly threefold increase in productivity. The average response time for GET requests plunged from 1.74 seconds to only 0.61 seconds, and the write operation latency decreased from 2.69 seconds to 1.99 seconds. The average duration of user workflows, a metric indicative of the overall system responsiveness, also saw a substantial decrease from 42.8 seconds to only 20.4 seconds, underscoring the effectiveness of the optimization.

The average response time improved drastically, improving by 49–65% across all user workflows.

Our experience highlights the potential of serverless architectures for optimizing performance. Although Tibica’s initial setup was functional, it was not working to its fullest potential. After examining the workflow and pinpointing performance slowdowns, we improved Tibica’s approach to REST API requests. The key lesson learned is that specificity is crucial. While Lambda functions are versatile, they may not always be the most efficient or cost-effective solution, especially for simple and routine tasks.

Metrics improvement after architecture changes.

Learning from this case, here is how you can optimize your system:

Step 1. Analyze Your Workflow

Begin by mapping your user workflow, focusing on the most frequent API requests, and identifying bottlenecks. Use measuring tools, such as AWS X-Ray, AWS CloudWatch, etc.

Step 2: Reevaluate Your Architecture

Consider whether a Lambda function is necessary for each endpoint. A direct integration approach may be more suitable if your logic focuses on straightforward GET requests.

Step 3: Integrate API Gateway with DynamoDB

The AWS API Gateway can interact directly with other AWS services like DynamoDB using VTL. Here’s how to set it up:

  1. Define a new resource and method in API Gateway.
  2. In the Integration Type, select DynamoDB as the AWS Service.
  3. Use VTL to map your request to DynamoDB’s parameters.
  4. Set up the appropriate IAM roles for API Gateway to access DynamoDB.

Step 4: Test and Measure

Deploy your changes in a controlled environment and compare the response times before and after the optimization. This quantitative analysis will validate the efficiency of the direct call approach.

Step 5: Iterate and Optimize

Continuous improvement is the king. Iterate over your architecture, making further tweaks and optimizations as needed. Monitor the performance and costs to ensure that the solution remains efficient.

Conclusion

Following these steps, we significantly reduced Tibica’s API response times, improving the user experience and reducing costs associated with Lambda invocations.

The serverless journey is about finding the right balance between performance, functionality, and cost. Your architecture should evolve with your application’s needs and the technologies available.

--

--