DEV Community

Ayman Aly Mahmoud for AWS Community Builders

Posted on • Updated on

Configuring Lambda functions properly

When developing and evaluating a function, it's essential to define three key configuration parameters: memory allocation, timeout duration, and concurrency level. These settings play a crucial role in measuring the performance of the function. Determining the optimal configuration for memory, timeout, and concurrency involves testing in real-world scenarios and under peak loads. Continuously monitoring your functions allows for adjustments to be made to optimize costs and maintain the desired customer experience within your application.

mtc

First, let's talk about how important it is to configure your memory and timeout values. Following that, we'll discuss the billing considerations associated with these values then exploring concurrency and strategies for optimizing it.

Memory:

Lambda functions allow for allocating up to 10 GB of memory. Lambda allocates CPU and other resources in direct proportion to the amount of memory configured. Scaling up the memory size results in a corresponding increase in available CPU resources for your function. To determine the optimal memory configuration for your functions, consider utilizing the AWS Lambda Power Tuning tool.

AWS Lambda Power Tuning, is a state machine powered by AWS Step Functions, optimizes Lambda functions for cost and performance. It's language agnostic and suggests the best power configuration for your function, based on multiple invocations across various memory settings (128MB to 10GB), aiming to minimize costs or maximize performance.

Since Lambda charges are directly proportional to the configured memory and function duration (measured in GB-seconds), the additional costs incurred by using more memory might be balanced out by reduced duration.

Timeout:

The AWS Lambda timeout value specifies the duration a function can run before Lambda terminates it. Currently capped at 900 seconds, this limit means a Lambda function invocation cannot exceed 15 minutes.

Set the timeout to the maximum only after thorough function testing. Many scenarios require quick failure rather than waiting for the full timeout.

Analyzing function duration aids in identifying issues causing invocations to surpass expected lengths. Load testing helps determine the optimal timeout value.

Billing for Lambda functions is based on runtime in 1-ms increments. Avoid lengthy timeouts to prevent billing for idle time during timeouts.

Lambda Billing:

AWS Lambda charges are based on usage, including the number of requests and duration. Each time your code runs in response to an event or an invoke call counts as a request. Duration is rounded up to the nearest 1 ms, and pricing depends on memory allocation, not actual usage, if you allocate 4 GB but only use 1 GB, you'll be charged for the full 4 GB. That is another reason why testing with various memory allocations is crucial to optimize both function performance and budget.

Increasing memory allocates proportional CPU power and resources. The AWS Lambda Free Tier includes 1 million free requests per month and 400,000 GB-seconds of compute time monthly. Access AWS pricing details and calculators for more information.
For more visibility about the cost please refer to https://calculator.aws/

Concurrency and Scaling:

Concurrency is a crucial configuration that affects your function's performance and scalability. It refers to the number of invocations your function can handle simultaneously. When invoked, Lambda launches an instance to process the event, and upon completion, it can handle additional requests. If new invocations occur while previous ones are still processing, Lambda allocates additional instances, leading to concurrent invocations.

Concurrent Invocations:

Consider a bus's capacity is 50 passengers. Only 50 passengers can occupy seats at any given time. Passengers who arrive when the bus is full must wait until a seat becomes available. With a reservation system, if a group reserves 10 seats, only 40 of the 50 seats remain available for other passengers. Similarly, Lambda functions have a concurrency limit comparable to the bus's capacity, and a reservation system can allocate runtime for specific instances.

Concurrency types

Unreserved concurrency
The amount of concurrency that is not reserved for specific functions. A minimum of 100 unreserved concurrency is guaranteed, ensuring functions without provisioned concurrency can still execute. If all concurrency is provisioned for one or two functions, none remains for others. Maintaining at least 100 available concurrency ensures all functions can run upon invocation.

Reservred concurrency
Reserved concurrency guarantees the maximum number of concurrent instances for a function. Once reserved, this concurrency is exclusively allocated to that function, preventing other functions from utilizing it. There is no charge for configuring reserved concurrency for a function.

Provisioned concurrency
Provisioned concurrency initializes a specified number of runtime environments, ensuring they are ready to promptly respond to your function's invocations. This option is ideal for achieving high performance and low latency.
You're charged for the provisioned concurrency amount and the duration it's configured. For instance, you may elevate provisioned concurrency in anticipation of a traffic surge. To avoid paying for unused warm environments, you can scale back down once the event subsides.

Testing Concurrency:

Ensuring your concurrency, memory, and timeout settings are optimal requires thorough testing against real-world scenarios. Here are some recommendations:

  1. Conduct performance tests mimicking peak invocation levels, monitor metrics to view throttling occurrences during performance peaks.
  2. Assess whether your backend can handle the incoming request velocity.
  3. Test comprehensively; if interfacing with Amazon RDS, verify that your function's concurrency levels align with the database's processing capabilities.
  4. Validate error handling functionality; include tests to push the application beyond concurrency limits to confirm proper error management.

In conclusion, we talked about optimizing AWS Lambda functions for performance, cost-efficiency, and scalability. We've explored key configuration settings such as memory allocation, timeout values, concurrency, and provisioned concurrency.
Additionally, we've discussed strategies for testing concurrency and ensuring that our functions perform effectively under real-world conditions. By implementing these best practices, we aim to enhance the reliability and efficiency of our serverless applications on AWS.

Top comments (0)