Skip to content

Lambda Function Concurrency Graph Issue (statistic: SUM)

0

I have a basic Python Lambda function named "demo-python" that I’m using to test Lambda concurrency. I also have another function that invokes "demo-python" multiple times. The code works fine, but I'm having trouble understanding the graph.

My "demo-python" function runs multiple times with a maximum concurrency of 9 within 1 minute (as shown in the attached screenshot). However, when I select "Sum" for a 1-minute interval, the concurrency value jumps to 67 (also shown in the screenshot).

Code:

import boto3
import json
from concurrent.futures import ThreadPoolExecutor, as_completed

lambda_client = boto3.client('lambda', region_name='us-east-1')

def invoke_lambda(payload, index):
    try:
        response = lambda_client.invoke(
            FunctionName='demo-python',
            InvocationType='RequestResponse',
            Payload=json.dumps(payload)
        )
        result = response['Payload'].read().decode('utf-8')
        return f"Invocation {index} succeeded: {result}"
    except Exception as e:
        return f"Invocation {index} failed: {str(e)}"

# Test concurrency
def test_concurrent_invocations(concurrency_level):
    test_payload = {
        "message": "Test from concurrency script"
    }

    with ThreadPoolExecutor(max_workers=concurrency_level) as executor:
        futures = [executor.submit(invoke_lambda, test_payload, i) for i in range(concurrency_level)]

        for future in as_completed(futures):
            print(future.result())

def lambda_handler(event, context):
    concurrency_level = 20
    test_concurrent_invocations(concurrency_level)

demo-python Code:

import json
import time

def lambda_handler(event, context):
    print(f"Received event: {json.dumps(event)}")
    time.sleep(1)
    
    return {
        'statusCode': 200,
        'body': json.dumps({
            'message': 'Lambda executed successfully',
            'input': event
        })
    }

Enter image description here

Enter image description here

asked a year ago156 views
2 Answers
1

Concurrency should always be used with Max. Invocations should be used with Sum usually. Duration should be Average or Max.

AWS
EXPERT
answered a year ago
0

The discrepancy you're observing between the maximum concurrency of 9 and the SUM statistic of 67 for a 1-minute interval is due to how Lambda concurrency metrics are calculated and reported.

The ConcurrentExecutions metric, which is what you're likely looking at, represents the number of function instances that are processing events at a given time. When you're viewing this metric with the MAX statistic, you're seeing the peak number of concurrent executions within the selected time period, which in your case is 9.

However, when you switch to the SUM statistic for a 1-minute interval, you're not seeing the sum of concurrent executions, but rather the sum of all the data points collected within that minute. Lambda typically reports metrics every few seconds, so the SUM statistic is essentially adding up all these individual data points.

For example, if your function maintained a concurrency of 9 for the entire minute, and Lambda reported this value every 5 seconds, you would see a SUM of 108 (9 concurrent executions * 12 data points in a minute). Your observed SUM of 67 suggests that your function didn't maintain peak concurrency for the entire minute, which is expected given the nature of your test.

It's important to note that the SUM statistic for concurrency metrics doesn't provide a meaningful measure of actual concurrent executions. For monitoring concurrency, it's generally more useful to look at the MAX statistic, which shows you the peak concurrency reached during the time period.

If you want to calculate the average concurrency over a period, you could use the AVG statistic instead of SUM. This would give you a more accurate representation of the typical concurrency level over time.

Remember, concurrency in Lambda is calculated as (average requests per second) * (average request duration in seconds). Your function sleeps for 1 second, which contributes to maintaining some level of concurrency during your test run.
Sources
Understanding Lambda function scaling - AWS Lambda
Types of metrics for Lambda functions - AWS Lambda
Monitoring concurrency - AWS Lambda

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.