Skip to content

Few lambda invocations compared to CDN invocations

0

We're using a lambda in all relevant behaviours (majority of the requests hitting a CDN) used at viewer request (all with caching disabled). We've been checking how the lambda has performed during the black friday and we discovered that at a certain point the lambda was invoked about 34k times vs 9 millions of invocations of the CDN.

I also checked throttled requests, but it seems to be 0 all the time.

So how does it really work? Does AWS optimize invocations, based on expected responses?

For example the lambda, for certain subpaths, just returns the original request.

Can AWS cache that stuff internally and save invocations?

1 Answer
2
Accepted Answer

The discrepancy between Lambda invocations and CDN requests you're observing is likely due to how AWS CloudFront (assuming this is the CDN you're using) interacts with Lambda@Edge functions.

When you use a Lambda function at the viewer request stage with caching disabled, it doesn't necessarily mean that the Lambda function will be invoked for every single request. AWS does indeed optimize invocations based on expected responses, especially for Lambda@Edge functions.

In your case, where the Lambda function returns the original request for certain subpaths, AWS can indeed cache this behavior internally. CloudFront has built-in optimizations to reduce the number of Lambda invocations when it determines that the function's output is likely to be the same for similar requests.

This optimization is particularly effective for functions that don't modify the request or only do so based on easily cacheable criteria (like specific URL paths). CloudFront can remember the result of previous Lambda invocations and apply the same modifications without invoking the Lambda function again for a period of time.

The fact that you're not seeing any throttled requests supports this explanation. If every single CDN request was trying to invoke the Lambda function, you'd likely see throttling during high-traffic periods like Black Friday.

It's important to note that while this behavior helps optimize performance and reduce costs, it doesn't affect the actual caching of the content served by the CDN. The content caching is still disabled as per your configuration.

This optimization is a feature of the AWS infrastructure and is not something you can directly control. However, it's generally beneficial as it reduces latency and costs while maintaining the intended functionality of your setup.

If you need the Lambda function to be invoked for every single request without exception, you might need to include some unique identifier or timestamp in each request to force CloudFront to invoke the function each time. However, this would likely increase your Lambda costs and potentially impact performance during high-traffic periods.

Sources
Comparing Lambda invocation modes - AWS Lambda
Comparing performance of interactive and asynchronous workloads - AWS Lambda
Invoke - AWS Lambda

answered 2 years ago

EXPERT

reviewed 2 years 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.