Anna Naden
Anna Naden

Reputation: 207

What is the best way to analyze slow response from a Lambda function that reads from Dynamodb?

I had a web page that called an AWS API gateway, which invoked a lambda function and returns a geographic map in geojson format. It took five seconds to load in the browser. I was pretty sure that the bottleneck is the lambda function. For one thing, when I ran the code locally it was under one second.

However, I did not have a way to systematically investigate the issue. Finally, I found that I had inadequate RAM. However, it could have been other issues, as the answers below indicate.

Upvotes: 1

Views: 999

Answers (3)

Traycho Ivanov
Traycho Ivanov

Reputation: 3207

General how to increase lambda performance.

  1. Package size - reduce package size to be as small as possible, remove big libraries if only a fraction of it is used, target your lambdas as max zip package of 20 MB , it could be achieved even for java ones.
  2. Use provision concurrency https://aws.amazon.com/blogs/compute/new-for-aws-lambda-predictable-start-up-times-with-provisioned-concurrency/, you don't need to introduce extra warmers, they are pretty much obsolete.
  3. Increase memory size, usually this is a bottleneck for cold starts, experiment increasing it to 256MB, 512MB, 1024MB and see difference it initial billing.

Upvotes: 0

jarmod
jarmod

Reputation: 78573

You can use the browser developer tools (e.g. network tab) to measure the latency of the various requests that your page makes. That's better than guessing what's happening.

If it's genuinely the Lambda function execution then try to understand what's taking time in your Lambda function - you can use simple logging to CloudWatch Logs to check timestamps at various points in your code.

A few obvious candidates here that could contribute to latency are:

  1. Lambda cold start
  2. your table scan
  3. low configured RAM size for the Lambda function

If cold start is your problem then there are various ways to address this (search the web) including Provisioned Concurrency. If you don't need to scan the entire DynamoDB table then don't - use a query instead. On the RAM front, increase the configured RAM size to understand what difference it makes to the Lambda runtime (more RAM => proportionally more CPU => quicker).

Upvotes: 4

Anna Naden
Anna Naden

Reputation: 207

I needed to increase the memory in the settings for the Lambda function. The default is 128 Mb.

Upvotes: 0

Related Questions