Maintaining Lambda Function Performance During Peak Traffic
Tags: aws, serverless, lambda, performance
Published: 2023-10-04 16:46:22

[AWS Lambda functions](https://aws.amazon.com/lambda/) are the popular computing option for modern applications, specifically if you use Serverless or event-driven architectures on AWS. And these Lambda functions are invoked billions of times in a production environment.

![A heavy traffic Serverless cloud, AI generated by Adobe Firefly](https://cdn.hashnode.com/res/hashnode/image/upload/v1685097843995/6759b6f9-ada9-47c6-9695-63ef7f5c9bdd.jpeg)

*AI-generated image by* [*Adobe Firefly*](https://firefly.adobe.com/) *- "Heavy traffic in Serverless cloud"*

In this blog, we will understand what are the different sources for Lambda triggers and also understand the factors to ensure performance during peak traffic.

## Lambda trigger sources

AWS Lambda function is well-knit with different sources triggering them either on demand or event-based. Understanding the source of Lambda function triggers would help in addressing performance during high traffic.

![Add trigger to Lambda function via AWS Console](https://cdn.hashnode.com/res/hashnode/image/upload/v1684932877703/08fec44e-1c90-43b5-9f30-3d4ce99cb3e5.png)

### Invoked via HTTP API

Lambda functions would be the core compute of the backend systems which are served over API endpoints generated from [Amazon API Gateway](https://aws.amazon.com/api-gateway/) or [AWS AppSync](https://aws.amazon.com/appsync/) or Lambda Function URLs. These are often triggered by applications programmatically either from a front-end application or backend systems.

### AWS Services invoking Lambda

In event-driven architectures, another AWS Service usually triggers the Lambda function whenever certain events occur.

A typical scenario would be, whenever there is a new object uploaded into a S3 bucket, a Lambda function could be triggered with the details of the event payload so that Lambda can further process it.

## Performance with heavy traffic

When using Lambda functions, being mindful of what it offers and how best it fits into your solutions is essential.

![Busy day in a traffic cop life managing traffic, AI generated by Adobe Firefly](https://cdn.hashnode.com/res/hashnode/image/upload/v1685120400554/804ef2ee-9fc4-49fd-9e36-f1ad85cae926.jpeg)

*AI-generated image by* [*Adobe Firefly*](https://firefly.adobe.com/) *- "busy day in a traffic cop life managing traffic".*

### Load balancers

Load balancers, even from the traditional systems are that piece of architecture that enable smooth traffic distribution. In a serverless system also, load balances help with distributing traffic and also ensuring there isn't a burst of invocations of Lambda functions.

### Enable caching

For frequently accessed data, enabling caching in your architecture at different levels would improve performance which on a high-traffic day pays off!

Allen and me (Jones) authored a blog with Momento about [how caching at every layer improves app performance](https://www.gomomento.com/blog/improve-app-performance-by-caching-at-every-layer).

### Provision and reserved concurrency

With Lambda's provision and reserved concurrency, you can allocate the resources which would be needed when you anticipate a high traffic flow. This ensures the architecture would be able to perform better on a peak traffic day.

### Weighted routing

Each lambda function is assigned weights and the weighted routing would use these weights as the measure to distribute traffic amongst different Lambda functions and in the case of Lambda aliases, it helps with distributing traffic amongst different versions of the Lambda function.

### Triggers with batch

When defining the event triggers for Lambda functions, different services like DynamoDB, SQS, and other messaging services support batch triggers where the service would collectively trigger the Lambda function once as per the `BatchSize` defined.

## Options for Serverless Architecture

### Application Load Balancers (ALBs)

* ALBs support targets as Lambda functions which help in distributing traffic with weights, geographical and round robin way.
    
* Provides endpoints available over the public internet which helps in building APIs that can be backed with ALBs and Lambda functions.
    
* ALBs are complex to configure when compared with other options available.
    
* With the management and configurations, this is not truly a serverless way!
    

### Amazon API Gateway

* Available by design to configure API Gateway integration with Lambda functions. And also seamless to work with API Gateway when compared with ALBs.
    
* API Gateway enables the usage of API key based usage quotas and throttling controls.
    
* Supports multiple authentication and authorization options.
    
* Supports payload validation with models where in a peak traffic scenario, API Gateway will ensure the validated payloads are invoking the Lambda function thus eliminating junk requests.
    
* Supports WAF and CloudFront-based endpoints which has added security for DDoS attacks.
    

### Lambda aliases

* Traffic distribution at the Lambda level when using it with different AWS Services as triggers.
    
* Configuration involves creating multiple versions of the same Lambda function and assigning an alias to one or max two versions.
    
* Supports weighted routing to each alias in a Lambda function.
    
* Not the best fit for Lambda functions which need to be invoked via the public internet as it lacks authentication and throttling.
    

## Wrap up!

Lambda functions have proven scalability and perform well even during peak traffic. Keeping in mind different aspects of Lambda functions or other AWS Services that enrich your traffic management is the key when architecting solutions that are prone to high traffic.

With the different options of ALBs, API Gateway, or even Lambda aliases, each of them has an upside and downside depending on the use case and how they are placed in the serverless architecture.